diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2013-11-02 18:29:22 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2013-11-02 18:29:22 -0700 |
commit | 0342335473ec1b9977e26089c28d7135ce98d254 (patch) | |
tree | 3aea60f2f3f6707a315fe340579b334ea3cdbfdf /activerecord/lib | |
parent | f950b2699f97749ef706c6939a84dfc85f0b05f2 (diff) | |
parent | 7caa09c5e1833ccc6b209a71588a44fb91606246 (diff) | |
download | rails-0342335473ec1b9977e26089c28d7135ce98d254.tar.gz rails-0342335473ec1b9977e26089c28d7135ce98d254.tar.bz2 rails-0342335473ec1b9977e26089c28d7135ce98d254.zip |
Merge pull request #12747 from yury/explicit-enum-values
Explicit mapping for enum
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 60af6b4178..ef7ccb89bd 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -3,6 +3,9 @@ module ActiveRecord # # class Conversation < ActiveRecord::Base # enum status: [:active, :archived] + # + # # same but with explicit mapping + # enum status: {active: 0, archived: 1} # end # # Conversation::STATUS # => { active: 0, archived: 1 } @@ -41,7 +44,8 @@ module ActiveRecord # def direction() DIRECTION.key self[:direction] end class_eval "def #{name}() #{const_name}.key self[:#{name}] end" - values.each_with_index do |value, i| + pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index + pairs.each do |value, i| # DIRECTION[:incoming] = 0 const_get(const_name)[value] = i |