0

I'm using Active Admin addons gem to create pretty tags for my enums. https://github.com/platanus/activeadmin_addons

In the Order model I have:

enum status: %i[pending not_started in_progress completed]

And in the ActiveAdmin model I have:

tag_column(:status, class: :colored_status)

However, I can't find anyway to translate the status to other language. I'm so over this that the solution doesn't even have to involve locales. I just want to change the tag label to something else.

I figured out I could do the following to change the text of the tag, but now the style is gone:

tag_column(:status, class: :status) do |delivery|
  I18n.t("activerecord.enums.delivery.statuses.#{delivery.status}")
end

This approach is also not the best because I have to translate it everywhere in the app.

Leticia Esperon
  • 2,499
  • 1
  • 18
  • 40

1 Answers1

1

I know it's way too late but what I do in those cases is to use a draper decorator that translates the enum options. So my admin file looks like this:

ActiveAdmin.register Car do
  decorate_with CarDecorator

  index do
    tag_column :human_status
  end
end

and in the decorator I define the human_status as follows:

class CarDecorator < Draper::Decorator
  delegate_all

  def human_status
    I18n.t(status, scope: 'activerecord.attributes.car.statuses')
  end
end

Additionally in the yml for the translations (es.yml in my case), you need to add the statuses as entries under statuses:

es:
  activerecord:
    attributes:
      car:
        statuses:
          approved: Aprobado
          declined: Rechazado
          pending: Pendiente

This way, the column uses the new :human_status instead of :status, which is translated.

You can skip the decorator and implement the method directly in the model, but decorators are the way to go in my opinion, because otherwise models would get a lot of presentation-related logic which doesn't belong there.

Now, regarding the color issue, in ActiveAdmin Addons, the tagged columns automatically render with two css classes, the status_tag one and one per value as <option> (for example status_tag approved). An example is shown here, so you can customize those clases to get your desired colors. However, with tag_column(:human_status, class: :colored_status) it should also work.

rjherrera
  • 11
  • 1