The premise is a little off. The if
statement works, but it's not producing the results that you expect. It's basically doing the equivalent of this:
shop.details['company_role'] == 'CEO' # is this true? then stop
| "founder" # is this true? yes, it's always truthy
The solutions provided will all work. Your choice mostly depends on your code style. I'd go with the second option because I personally like the test with an emphasis on the tested data.
Two further suggestions. First, the test itself could be extracted to a method. This would improve readability of your code.
def shop_type
if executive?
'Other'
else
shop.details['customer_kind']
end
# alternate: executive? ? 'Other' : shop.details['customer_kind']
end
def executive?
shop.details['company_role'].in?('CEO','founder')
end
Lastly, it strikes me that the decision about whether or not a Shop
includes an "executive?" seems to be useful information in a number of contexts. As such, it looks like the executive?
method would be better placed inside the Shop
model so that knowledge of the internal organization of the Shop
does not leak all over your system.
shop.executive? ? 'Other' : shop.details['customer_kind']
class Shop
...
def executive?
shop.details['company_role'].in?('CEO','founder')
end
end