Skip to content

Commit

Permalink
bring back constant to expose the enum mapping as HWIA.
Browse files Browse the repository at this point in the history
  • Loading branch information
senny committed Nov 6, 2013
1 parent 02b6757 commit aeaf3a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 13 additions & 4 deletions activerecord/lib/active_record/enum.rb
Expand Up @@ -31,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
Expand All @@ -53,7 +62,7 @@ def enum(definitions)

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 }
Expand All @@ -62,7 +71,7 @@ def enum(definitions)
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
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/enum_test.rb
Expand Up @@ -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

0 comments on commit aeaf3a9

Please sign in to comment.