aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-11-04 19:36:22 +0100
committerYves Senn <yves.senn@gmail.com>2013-11-05 16:45:34 +0100
commit813c8c0751b18389431912328f9c90933f26d819 (patch)
tree16b3425632fbc729fecdf163eacd9e7d7eb04ad4
parent5a011409a46811a9bd46400cc0147d4a85fa02e9 (diff)
downloadrails-813c8c0751b18389431912328f9c90933f26d819.tar.gz
rails-813c8c0751b18389431912328f9c90933f26d819.tar.bz2
rails-813c8c0751b18389431912328f9c90933f26d819.zip
define enum methods inside a `Module` to make them overwritable.
-rw-r--r--activerecord/lib/active_record/enum.rb37
-rw-r--r--activerecord/test/cases/enum_test.rb5
-rw-r--r--activerecord/test/models/book.rb5
3 files changed, 34 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb
index da6bc87950..efea9560ee 100644
--- a/activerecord/lib/active_record/enum.rb
+++ b/activerecord/lib/active_record/enum.rb
@@ -35,30 +35,41 @@ module ActiveRecord
# end
module Enum
def enum(definitions)
+ klass = self
definitions.each do |name, values|
enum_values = {}
name = name.to_sym
- # def direction=(value) self[:direction] = DIRECTION[value] end
- define_method("#{name}=") { |value| self[name] = enum_values[value] }
+ _enum_methods_module.module_eval do
+ # def direction=(value) self[:direction] = DIRECTION[value] end
+ define_method("#{name}=") { |value| self[name] = enum_values[value] }
- # def direction() DIRECTION.key self[:direction] end
- define_method(name) { enum_values.key self[name] }
+ # def direction() DIRECTION.key self[:direction] end
+ define_method(name) { enum_values.key self[name] }
- pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
- pairs.each do |value, i|
- enum_values[value] = i
+ pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
+ pairs.each do |value, i|
+ enum_values[value] = i
- # scope :incoming, -> { where direction: 0 }
- scope value, -> { where name => i }
+ # scope :incoming, -> { where direction: 0 }
+ klass.scope value, -> { klass.where name => i }
- # def incoming?() direction == 0 end
- define_method("#{value}?") { self[name] == i }
+ # def incoming?() direction == 0 end
+ define_method("#{value}?") { self[name] == i }
- # def incoming! update! direction: :incoming end
- define_method("#{value}!") { update! name => value.to_sym }
+ # def incoming! update! direction: :incoming end
+ define_method("#{value}!") { update! name => value.to_sym }
+ end
end
end
end
+
+ def _enum_methods_module
+ @_enum_methods_module ||= begin
+ mod = Module.new
+ include mod
+ mod
+ end
+ end
end
end
diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb
index 8eb82ead3c..bb9faccf1b 100644
--- a/activerecord/test/cases/enum_test.rb
+++ b/activerecord/test/cases/enum_test.rb
@@ -35,4 +35,9 @@ class EnumTest < ActiveRecord::TestCase
@book.update! status: :written
assert @book.written?
end
+
+ test "enum methods are overwritable" do
+ assert_equal "do publish work...", @book.published!
+ assert @book.published?
+ end
end
diff --git a/activerecord/test/models/book.rb b/activerecord/test/models/book.rb
index 781f67557b..4cb2c7606b 100644
--- a/activerecord/test/models/book.rb
+++ b/activerecord/test/models/book.rb
@@ -9,4 +9,9 @@ class Book < ActiveRecord::Base
enum status: [:proposed, :written, :published]
enum read_status: {unread: 0, reading: 2, read: 3}
+
+ def published!
+ super
+ "do publish work..."
+ end
end