diff options
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 25 | ||||
-rw-r--r-- | activerecord/test/cases/enum_test.rb | 6 |
2 files changed, 22 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index a70638f3df..5fcc0382d8 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -5,20 +5,18 @@ module ActiveRecord # enum status: [ :active, :archived ] # end # - # Conversation::STATUS # => { active: 0, archived: 1 } - # # # conversation.update! status: 0 # conversation.active! # conversation.active? # => true - # conversation.status # => :active + # conversation.status # => "active" # # # conversation.update! status: 1 # conversation.archived! # conversation.archived? # => true - # conversation.status # => :archived + # conversation.status # => "archived" # # # conversation.update! status: 1 - # conversation.status = :archived + # conversation.status = "archived" # # You can set the default value from the database declaration, like: # @@ -33,17 +31,26 @@ module ActiveRecord # class Conversation < ActiveRecord::Base # enum status: { active: 0, archived: 1 } # end + # + # In rare circumstances you might need to access the mapping directly. + # The mappings are exposed through a constant with the attributes name: + # + # Conversation::STATUS # => { "active" => 0, "archived" => 1 } + # + # Use that constant when you need to know the ordinal value of an enum: + # + # Conversation.where("status <> ?", Conversation::STATUS[:archived]) module Enum def enum(definitions) klass = self definitions.each do |name, values| - enum_values = {} + # DIRECTION = { } + enum_values = _enum_methods_module.const_set name.to_s.upcase, ActiveSupport::HashWithIndifferentAccess.new name = name.to_sym _enum_methods_module.module_eval do # def direction=(value) self[:direction] = DIRECTION[value] end define_method("#{name}=") { |value| - value = value.to_s unless enum_values.has_key?(value) raise ArgumentError, "'#{value}' is not a valid #{name}" end @@ -55,7 +62,7 @@ module ActiveRecord pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index pairs.each do |value, i| - enum_values[value.to_s] = i + enum_values[value] = i # scope :incoming, -> { where direction: 0 } klass.scope value, -> { klass.where name => i } @@ -64,7 +71,7 @@ module ActiveRecord define_method("#{value}?") { self[name] == i } # def incoming! update! direction: :incoming end - define_method("#{value}!") { update! name => value.to_sym } + define_method("#{value}!") { update! name => value } end end end diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 54cc60913a..35e8a98156 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -57,4 +57,10 @@ class EnumTest < ActiveRecord::TestCase end assert_equal "'unknown' is not a valid status", e.message end + + test "constant to access the mapping" do + assert_equal 0, Book::STATUS[:proposed] + assert_equal 1, Book::STATUS["written"] + assert_equal 2, Book::STATUS[:published] + end end |