In my rails application I have a model called JobPosting. A job posting has a status, it can be either:
- Waiting Approval
- Draft
- Open
- Interviews Scheduled
- Closed
I implemented these statuses using ActiveRecord::Enum like so:
class JobPosting < ApplicationRecord
enum status: [:waiting_approval, :draft, :open, :interviews_scheduled, :closed]
end
Now I would like to display a different user interface element that is dependant on the status of the job posting. i.e.
For the waiting approval status I want:
<div class="label label-warning">pending approval</div>
And for the open status I want:
<div class="label label-success">open</div>
Note that there is different text and the class is different as the element is styled differently for the different cases. In my index.html.erb, where this styling needs to happen, I could just do a bunch of embedded ruby if statements and check the status of the posting and display the desired element, like so:
<% if posting.waiting_approval? %>
<div class="label label-warning">pending approval</div>
<% elsif posting.open? %>
<div class="label label-success">open</div>
<% elsif posting.closed> %>
etc...
<% end %>
I feel as if that is not very DRY, is there a better way?
Alternatively, I could create partial and have the logic stored in that and just render the partial, but again is that how it is done?