diff options
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record.rb | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/base.rb | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 60 |
3 files changed, 62 insertions, 0 deletions
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index f19f5ecdf9..7a2c5c8bf2 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -37,6 +37,7 @@ module ActiveRecord autoload :ConnectionHandling autoload :CounterCache autoload :DynamicMatchers + autoload :Enum autoload :Explain autoload :Inheritance autoload :Integration diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 04e3dd49e7..69a9eabefb 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -291,6 +291,7 @@ module ActiveRecord #:nodoc: extend Translation extend DynamicMatchers extend Explain + extend Enum extend Delegation::DelegateCache include Persistence diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb new file mode 100644 index 0000000000..13b843ff4f --- /dev/null +++ b/activerecord/lib/active_record/enum.rb @@ -0,0 +1,60 @@ +module ActiveRecord + # Declare an enum attribute where the values map to integers in the database, but can be queried by name. Example: + # + # class Conversation < ActiveRecord::Base + # enum status: %i( active archived ) + # end + # + # Conversation::STATUS # => { active: 0, archived: 1 } + # + # # conversation.update! status: 0 + # conversation.active! + # conversation.active? # => true + # conversation.status # => :active + # + # # conversation.update! status: 1 + # conversation.archived! + # conversation.archived? # => true + # conversation.status # => :archived + # + # # conversation.update! status: 1 + # conversation.status = :archived + # + # You can set the default value from the database declaration, like: + # + # create_table :conversation do + # t.column :status, :integer, default: 0 + # end + # + # Good practice is to let the first declared status be the default. + module Enum + def enum(definitions) + definitions.each do |name, values| + const_name = name.to_s.upcase + + # DIRECTION = { } + const_set const_name, {} + + # def direction=(value) self[:direction] = DIRECTION[value] end + class_eval "def #{name}=(value) self[:#{name}] = #{const_name}[value] end" + + # def direction() DIRECTION.key self[:direction] end + class_eval "def #{name}() #{const_name}.key self[:#{name}] end" + + values.each_with_index do |value, i| + # DIRECTION[:incoming] = 0 + const_get(const_name)[value] = i + + # scope :incoming, -> { where direction: 0 } + scope value, -> { where name => i } + + # def incoming?() direction == 0 end + class_eval "def #{value}?() self[:#{name}] == #{i} end" + + # def incoming! update! direction: :incoming end + class_eval "def #{value}!() update! #{name}: :#{value} end" + end + end + end + end +end |