Reputation: 3337
I have an app I'm working on with Rails 3.2.x and Bootstrap 2. I have a view helper which returns different text based on a call's status and attributes.
def status(call)
if call.call_status == "open" && call.transfer_date > Time.zone.now + 15.minutes
"Scheduled"
elsif call.wait_return == "yes" && call.call_status == "open"
"Active/Wait and Return"
elsif call.call_status == "close"
"Closed Call"
elsif call.call_status == "cancel"
"Cancelled Call"
else
"Active"
end
end
I'd like to refactor this to where each condition's text returns a bootstrap badge with the text inside of it. I looked at the API docs for content_tag
and I think this is what I need but I'm not 100% sure on how to make this work.
Any help or refactoring advice would be greatly appreciated.
Upvotes: 0
Views: 141
Reputation: 1735
I would start by splitting that large method into small ones, something like this:
class Call
def scheduled?
call_status == "open" && call.transfer_date > Time.zone.now + 15.minutes
end
def closed?
call_status == "close"
end
...
end
Then your status
method will look like this:
def status(call)
text = case
when call.scheduled?
"Scheduled"
when call.cancelled?
"Active/Wait and Return"
when call.closed?
"Closed Call"
end
content_tag(:span, text, class: "badge")
end
Hope it helps you to refactor your code.
Upvotes: 2