Skip to content

Instantly share code, notes, and snippets.

@dmilisic
Last active January 21, 2022 10:50
Show Gist options
  • Select an option

  • Save dmilisic/38fcd407044ace7514df to your computer and use it in GitHub Desktop.

Select an option

Save dmilisic/38fcd407044ace7514df to your computer and use it in GitHub Desktop.
Initializer for handling ActiveRecord (4.1+) enums by RailsAdmin (0.6.2)
module ActiveRecord
module RailsAdminEnum
def enum(definitions)
super
definitions.each do |name, values|
define_method("#{ name }_enum") { self.class.send(name.to_s.pluralize).to_a }
define_method("#{ name }=") do |value|
if value.kind_of?(String) and value.to_i.to_s == value
super value.to_i
else
super value
end
end
end
end
end
end
ActiveRecord::Base.send(:extend, ActiveRecord::RailsAdminEnum)
@chris-roerig
Copy link
Copy Markdown

It should be noted that this must be loaded before config/initializers/rails_admin.rb, otherwise enum options will not work.

@vedant1811
Copy link
Copy Markdown

Works like a charm. thanks a lot

@Sacristan
Copy link
Copy Markdown

Thank You - saved my day!

@vedant1811
Copy link
Copy Markdown

Don't forget to prevent filtering of the enum fields by

  rails_admin do
    configure :status do
      searchable false
    end
  end

otherwise search/filter will throw errors

@almozavr
Copy link
Copy Markdown

Note: to add localization (default attribute path translation) support, change getter method definition to

define_method("#{ name }_enum") {
  self.class.send(name.to_s.pluralize).to_a.map do |k, v|
    translated = I18n.t(k, scope: [:activerecord, :attributes, self.class.to_s.downcase, name.to_s.pluralize])
    processed_key = translated.include?('translation missing') ? k : translated
    [processed_key, v]
  end
}

@dmilisic
Copy link
Copy Markdown
Author

thanks @chris-roerig - filename updated

@arthurbryant
Copy link
Copy Markdown

Thank you. @dmilisic Works like a charm

Copy link
Copy Markdown

ghost commented Dec 3, 2015

Please help me!. I have a model LocationMaster which consists of all locations. I want to display the locations as enum in rails admin. I want to store the id of the location selected by the user in location_master_id filed. I cant get it. Please help me!

@hexinpeter
Copy link
Copy Markdown

Strange enough, my filtering does seem to work with enum fields. The only things I did were to add the patch file and add the lines of field :category.

@Joseph-N
Copy link
Copy Markdown

Joseph-N commented Sep 6, 2016

Thanks! worked like a charm

@james-purr
Copy link
Copy Markdown

bit late on this but thanks so much - worked perfectly straight off the bat and saved me a lot of headache time..

@jjercx
Copy link
Copy Markdown

jjercx commented Nov 24, 2017

thanks!

@jefflab
Copy link
Copy Markdown

jefflab commented May 11, 2018

To make this work in Rails 5, I had to change:

self.class.send(name.to_s.pluralize).to_a

to

self.class.send(name.to_s.pluralize).keys.to_a

The documentation for supported formats is here:

https://github.com/sferik/rails_admin/wiki/Enumeration

@iyush1993
Copy link
Copy Markdown

iyush1993 commented Jan 10, 2019

Before adding this, the filter did not work. Now the filter works fine.
But, now my enum field in the form (rails_admin form) does not get populated with the stored value. It defaults to nil.

Update

It is fixed now. I tried the suggestion by @jefflab (right above my comment) and it works.

@dmitriy-sqrt
Copy link
Copy Markdown

Also had the issue with form value not populated with stored value. (and fix from @jefflab broke the filtering for me)
I was able to fix it with adding mentioned initializer + :

          configure :state, :enum do
            def form_value
              bindings[:object].state_before_type_cast
            end
          end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment