diff options
author | Eileen M. Uchitelle <eileencodes@gmail.com> | 2016-08-06 10:30:15 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-06 10:30:15 -0400 |
commit | 6510f9980d93abc50b7673b0a73e02d066fab26c (patch) | |
tree | 799eb26d2891757d6c4981e71788292112831237 /activerecord/lib | |
parent | 78a877408b8a253d49fba931e2aed68217c2d18a (diff) | |
parent | 83f08647d659b3b3e3052215fa0c42324ec81fb7 (diff) | |
download | rails-6510f9980d93abc50b7673b0a73e02d066fab26c.tar.gz rails-6510f9980d93abc50b7673b0a73e02d066fab26c.tar.bz2 rails-6510f9980d93abc50b7673b0a73e02d066fab26c.zip |
Merge pull request #25998 from kamipo/enum_with_alias_attribute
Fix `enum` with `alias_attribute`
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index b884edf920..5d401beb53 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -161,8 +161,9 @@ module ActiveRecord detect_enum_conflict!(name, name) detect_enum_conflict!(name, "#{name}=") - decorate_attribute_type(name, :enum) do |subtype| - EnumType.new(name, enum_values, subtype) + attr = attribute_alias?(name) ? attribute_alias(name) : name + decorate_attribute_type(attr, :enum) do |subtype| + EnumType.new(attr, enum_values, subtype) end _enum_methods_module.module_eval do @@ -184,15 +185,15 @@ module ActiveRecord # def active?() status == 0 end klass.send(:detect_enum_conflict!, name, "#{value_method_name}?") - define_method("#{value_method_name}?") { self[name] == value.to_s } + define_method("#{value_method_name}?") { self[attr] == value.to_s } # def active!() update! status: :active end klass.send(:detect_enum_conflict!, name, "#{value_method_name}!") - define_method("#{value_method_name}!") { update! name => value } + define_method("#{value_method_name}!") { update!(attr => value) } # scope :active, -> { where status: 0 } klass.send(:detect_enum_conflict!, name, value_method_name, true) - klass.scope value_method_name, -> { where(name => value) } + klass.scope value_method_name, -> { where(attr => value) } end end defined_enums[name.to_s] = enum_values |