Skip to content
Browse files

The enum value constant isn't used, so rm it for now.

  • Loading branch information...
1 parent 238ee10 commit 9f8762f1d5e84472b1cae16b063fc79d1de15963 @tenderlove tenderlove committed
Showing with 7 additions and 27 deletions.
  1. +7 −17 activerecord/lib/active_record/enum.rb
  2. +0 −10 activerecord/test/cases/enum_test.rb
View
24 activerecord/lib/active_record/enum.rb
@@ -36,37 +36,27 @@ module ActiveRecord
module Enum
def enum(definitions)
definitions.each do |name, values|
- # DIRECTION = { }
- const = const_set name.to_s.upcase, {}
- name = name.to_sym
+ enum_values = {}
+ name = name.to_sym
# def direction=(value) self[:direction] = DIRECTION[value] end
- define_method "#{name}=" do |value|
- self[name] = const[value]
- end
+ define_method("#{name}=") { |value| self[name] = enum_values[value] }
# def direction() DIRECTION.key self[:direction] end
- define_method name do
- const.key self[name]
- 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|
- # DIRECTION[:incoming] = 0
- const[value] = i
+ enum_values[value] = i
# scope :incoming, -> { where direction: 0 }
scope value, -> { where name => i }
# def incoming?() direction == 0 end
- define_method "#{value}?" do
- self[name] == i
- end
+ define_method("#{value}?") { self[name] == i }
# def incoming! update! direction: :incoming end
- define_method "#{value}!" do
- update! name => value.to_sym
- end
+ define_method("#{value}!") { update! name => value.to_sym }
end
end
end
View
10 activerecord/test/cases/enum_test.rb
@@ -35,14 +35,4 @@ class EnumTest < ActiveRecord::TestCase
@book.update! status: :written
assert @book.written?
end
-
- test "constant" do
- assert_equal 0, Book::STATUS[:proposed]
- assert_equal 1, Book::STATUS[:written]
- assert_equal 2, Book::STATUS[:published]
-
- assert_equal 0, Book::READ_STATUS[:unread]
- assert_equal 2, Book::READ_STATUS[:reading]
- assert_equal 3, Book::READ_STATUS[:read]
- end
end

0 comments on commit 9f8762f

Please sign in to comment.
Something went wrong with that request. Please try again.