-
-
Save dmilisic/38fcd407044ace7514df to your computer and use it in GitHub Desktop.
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) |
bit late on this but thanks so much - worked perfectly straight off the bat and saved me a lot of headache time..
thanks!
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:
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.
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
Thanks! worked like a charm