1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

This commit is contained in:
Aaron Patterson 2013-11-04 15:55:29 -08:00
parent 238ee10c41
commit 9f8762f1d5
2 changed files with 7 additions and 27 deletions

View file

@ -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 file

@ -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