Reputation: 3749
Project.status
can be [1,2,3]
.
Is there a better way of defining the following logic (color depends on status
), than doing it in the view?
- if project.status == '1'
%td.bg-success= project.status
- elsif project.status == '2'
%td.bg-warning= project.status
- else
%td.bg-danger= project.status
Upvotes: 0
Views: 150
Reputation: 3749
application_helper.rb:
module ApplicationHelper
def plan_label(plan)
plan_span_generator(plan)
end
private
def status_span_generator(status)
case status
when 'active'
content_tag(:span, status.titleize, class: 'badge badge-success')
when 'inactive'
content_tag(:span, status.titleize, class: 'badge badge-secondary')
when 'planned'
content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-primary')
when 'confirmed'
content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-success')
when 'member_cancelled'
content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-danger')
when 'client_cancelled'
content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-danger')
when 'no_show'
content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-danger')
when 'no_show_refunded'
content_tag(:span, I18n.t(status, scope: [:activerecord, :attributes, :event, :statuses]), class: 'badge badge-success')
end
end
end
View:
Let's say event.status == 'confirmed'
. Instead of = event.status
in the view, I put = status_label(event.status)
and the status is given a class like "badge badge-success".
Works!
Upvotes: 1
Reputation: 882
color_code = {"1" => "success", "2" => "warning", "3" => "danger"}`
color_code["#{project.status}"]
Upvotes: 0
Reputation: 1415
You can achieve it in following way. In your view:
- color_class = {'1': 'success', '2': 'warning'}
- default_class = 'bg_danger'
%td{class: (color_class[project.status] || default_class)}= project.status
I didn't get - what else
means - is it any other status or just third. If any other - then default_class
is suitable, else, just add another key-value to color_class
Also take a look at ActiveRecord enums for statuses.
Upvotes: 1