From 6c720d18a2770299433fae82c1cab25d0bd4033e Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Mon, 4 Nov 2013 19:54:46 +0100 Subject: direct enum assignment rasies ArgumentError for unknown values. --- activerecord/lib/active_record/enum.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index efea9560ee..6bfdf75a2d 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -42,7 +42,12 @@ module ActiveRecord _enum_methods_module.module_eval do # def direction=(value) self[:direction] = DIRECTION[value] end - define_method("#{name}=") { |value| self[name] = enum_values[value] } + define_method("#{name}=") { |value| + unless enum_values.has_key?(value) + raise ArgumentError, "'#{value}' is not a valid #{name}" + end + self[name] = enum_values[value] + } # def direction() DIRECTION.key self[:direction] end define_method(name) { enum_values.key self[name] } -- cgit v1.2.3