- Read Tutorial
- Watch Guide Video
Our audit log dashboard is coming along well, and now, we are going to implement our status enum. Right now, the status is showing a value of 0 which means nothing to the user, so we have to make it more meaningful.
Go to audit_log.rb
model file, and here, we'll declare an enum, just like how we did it in our post model file. However, we need only two here, and we'll also rename them to pending and confirmed.
# app/models/audit_log.rb class AuditLog < ActiveRecord::Base enum status: { pending: 0, confirmed: 1 }
If you refresh the browser, you'll see this:
Now, go to rails console and update one of the logs to confirmed. Refresh the browser, and that's displayed too.
Next, we'll work on our label. For that, open posts_helper.rb
, and move all the code to application_helper.rb
, as we're going to use it for our audit log functionality too. Let's now make a small change here as we don't have "submitted", "approved" or "rejected" in audit log. All that we have to do is just extend it to add "pending" and "confirmed." Since we want our "pending" to be the same as "submitted" and "confirmed" as "approved", just copy-paste the code. The final code should look like this:
# app/helpers/application_helper.rb module ApplicationHelper def active?(path) "active" if current_page?(path) end def status_label status status_span_generator status end private def status_span_generator status case status when 'submitted' content_tag(:span, status.titleize, class: 'label label-primary') when 'approved' content_tag(:span, status.titleize, class: 'label label-success') when 'rejected' content_tag(:span, status.titleize, class: 'label label-danger') when 'pending' content_tag(:span, status.titleize, class: 'label label-primary') when 'confirmed' content_tag(:span, status.titleize, class: 'label label-success') end end end
Now add status_label
to the _audit_log.html.erb
partial.
<%= status_label audit_log.status %>
The final page should look good.