diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 48 | ||||
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/enum_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/models/book.rb | 4 |
4 files changed, 34 insertions, 34 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 5716cc4b5d..16376b0b89 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,27 +1,27 @@ -* Added ActiveRecord::Base#enum for declaring enum attributes where the values map to integers in the database, but can be queried by name. - - Example: - class Conversation < ActiveRecord::Base - enum status: %i( active archived ) - end - - Conversation::STATUS # => { active: 0, archived: 1 } - - # conversation.update! status: 0 - conversation.active! - conversation.active? # => true - conversation.status # => :active - - # conversation.update! status: 1 - conversation.archived! - conversation.archived? # => true - conversation.status # => :archived - - # conversation.update! status: 1 - conversation.status = :archived - - *DHH* +* Added ActiveRecord::Base#enum for declaring enum attributes where the values map to integers in the database, but can be queried by name. + Example: + + class Conversation < ActiveRecord::Base + enum status: [:active, :archived] + end + + Conversation::STATUS # => { active: 0, archived: 1 } + + # conversation.update! status: 0 + conversation.active! + conversation.active? # => true + conversation.status # => :active + + # conversation.update! status: 1 + conversation.archived! + conversation.archived? # => true + conversation.status # => :archived + + # conversation.update! status: 1 + conversation.status = :archived + + *DHH* * ActiveRecord::Base#attribute_for_inspect now truncates long arrays (more than 10 elements) @@ -35,6 +35,7 @@ Fixed bug when providing `includes` in through association scope, and fetching targets. Example: + class Vendor < ActiveRecord::Base has_many :relationships, -> { includes(:user) } has_many :users, through: :relationships @@ -50,7 +51,6 @@ vendor.users.to_a # => No exception is raised - Fixes #12242, #9517, #10240. *Paul Nikitochkin* diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 13b843ff4f..60af6b4178 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -2,7 +2,7 @@ module ActiveRecord # 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: %i( active archived ) + # enum status: [:active, :archived] # end # # Conversation::STATUS # => { active: 0, archived: 1 } @@ -11,21 +11,21 @@ module ActiveRecord # conversation.active! # conversation.active? # => true # conversation.status # => :active - # + # # # conversation.update! status: 1 # conversation.archived! # conversation.archived? # => true # conversation.status # => :archived - # + # # # conversation.update! status: 1 # conversation.status = :archived # # You can set the default value from the database declaration, like: # - # create_table :conversation do + # create_table :conversations do |t| # t.column :status, :integer, default: 0 # end - # + # # Good practice is to let the first declared status be the default. module Enum def enum(definitions) diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 9855261e4d..7f5ddc92d4 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -13,7 +13,7 @@ class StoreTest < ActiveRecord::TestCase assert_not @book.written? assert_not @book.published? end - + test "query state with symbol" do assert_equal :proposed, @book.status end @@ -22,12 +22,12 @@ class StoreTest < ActiveRecord::TestCase @book.written! assert @book.written? end - + test "update by setter" do @book.update! status: :written assert @book.written? end - + test "constant" do assert_equal 0, Book::STATUS[:proposed] assert_equal 1, Book::STATUS[:written] diff --git a/activerecord/test/models/book.rb b/activerecord/test/models/book.rb index 997c088176..a527e41a8a 100644 --- a/activerecord/test/models/book.rb +++ b/activerecord/test/models/book.rb @@ -6,6 +6,6 @@ class Book < ActiveRecord::Base has_many :subscriptions has_many :subscribers, through: :subscriptions - - enum status: %i( proposed written published ) + + enum status: [:proposed, :written, :published] end |