Last active
January 21, 2022 10:50
-
-
Save dmilisic/38fcd407044ace7514df to your computer and use it in GitHub Desktop.
Initializer for handling ActiveRecord (4.1+) enums by RailsAdmin (0.6.2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 + :