Permalink
Browse files

define enum methods inside a `Module` to make them overwritable.

  • Loading branch information...
1 parent 5a01140 commit 813c8c0751b18389431912328f9c90933f26d819 @senny senny committed Nov 4, 2013
Showing with 34 additions and 13 deletions.
  1. +24 −13 activerecord/lib/active_record/enum.rb
  2. +5 −0 activerecord/test/cases/enum_test.rb
  3. +5 −0 activerecord/test/models/book.rb
View
37 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
View
5 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
View
5 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

0 comments on commit 813c8c0

Please sign in to comment.