aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-01-01 19:17:59 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-01-01 19:17:59 -0200
commit358abe4524ca0ccaa35d255fb090a269b1df35f0 (patch)
tree6751f6cba1ed4a6ce80f1fd5ef8e56ce1546619a
parentf141919974806568f480cf2c670f990322308044 (diff)
parent50060e969b422e6cd8f90181ac3d92f915a96f69 (diff)
downloadrails-358abe4524ca0ccaa35d255fb090a269b1df35f0.tar.gz
rails-358abe4524ca0ccaa35d255fb090a269b1df35f0.tar.bz2
rails-358abe4524ca0ccaa35d255fb090a269b1df35f0.zip
Merge pull request #13542 from robin850/issue-13530
Fix enum writers when using integers
-rw-r--r--activerecord/lib/active_record/enum.rb9
-rw-r--r--activerecord/test/cases/enum_test.rb11
-rw-r--r--activerecord/test/schema/schema.rb4
3 files changed, 21 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb
index 1d484f7c15..c042a2f232 100644
--- a/activerecord/lib/active_record/enum.rb
+++ b/activerecord/lib/active_record/enum.rb
@@ -1,5 +1,6 @@
module ActiveRecord
- # Declare an enum attribute where the values map to integers in the database, but can be queried by name. Example:
+ # Declare an enum attribute where the values map to integers in the database,
+ # but can be queried by name. Example:
#
# class Conversation < ActiveRecord::Base
# enum status: [ :active, :archived ]
@@ -23,6 +24,10 @@ module ActiveRecord
# conversation.status.nil? # => true
# conversation.status # => nil
#
+ # Scopes based on the allowed values of the enum field will be provided
+ # as well. With the above example, it will create an +active+ and +archived+
+ # scope.
+ #
# You can set the default value from the database declaration, like:
#
# create_table :conversations do |t|
@@ -67,7 +72,7 @@ module ActiveRecord
_enum_methods_module.module_eval do
# def status=(value) self[:status] = STATUS[value] end
define_method("#{name}=") { |value|
- unless enum_values.has_key?(value) || value.blank?
+ unless enum_values.has_key?(value) || enum_values.has_value?(value) || value.blank?
raise ArgumentError, "'#{value}' is not a valid #{name}"
end
self[name] = enum_values[value]
diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb
index 017edcb194..47e3dfc3ad 100644
--- a/activerecord/test/cases/enum_test.rb
+++ b/activerecord/test/cases/enum_test.rb
@@ -16,7 +16,7 @@ class EnumTest < ActiveRecord::TestCase
assert @book.unread?
end
- test "query state with symbol" do
+ test "query state with strings" do
assert_equal "proposed", @book.status
assert_equal "unread", @book.read_status
end
@@ -78,4 +78,13 @@ class EnumTest < ActiveRecord::TestCase
assert_equal 1, Book::STATUS["written"]
assert_equal 2, Book::STATUS[:published]
end
+
+ test "first_or_initialize with enums' scopes" do
+ class Issue < ActiveRecord::Base
+ enum status: [:open, :closed]
+ end
+
+ assert Issue.open.empty?
+ assert Issue.open.first_or_initialize
+ end
end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index ac546fc296..9f504801af 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -327,6 +327,10 @@ ActiveRecord::Schema.define do
t.string :color
end
+ create_table :issues, force: true do |t|
+ t.integer :status
+ end
+
create_table :items, force: true do |t|
t.column :name, :string
end