diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2013-11-02 12:01:31 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2013-11-02 12:01:31 -0700 |
commit | db41eb8a6ea88b854bf5cd11070ea4245e1639c5 (patch) | |
tree | 7ce7308e9b1a46bb61ece82f43af1338eb64d501 /activerecord/test | |
parent | deaf285824fa7d2aaf24619eefdb0893c0069bc0 (diff) | |
download | rails-db41eb8a6ea88b854bf5cd11070ea4245e1639c5.tar.gz rails-db41eb8a6ea88b854bf5cd11070ea4245e1639c5.tar.bz2 rails-db41eb8a6ea88b854bf5cd11070ea4245e1639c5.zip |
Added ActiveRecord::Base#enum for declaring enum attributes where the values map to integers in the database, but can be queried by name
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/enum_test.rb | 36 | ||||
-rw-r--r-- | activerecord/test/models/book.rb | 6 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 1 |
3 files changed, 41 insertions, 2 deletions
diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb new file mode 100644 index 0000000000..9855261e4d --- /dev/null +++ b/activerecord/test/cases/enum_test.rb @@ -0,0 +1,36 @@ +require 'cases/helper' +require 'models/book' + +class StoreTest < ActiveRecord::TestCase + fixtures :books + + setup do + @book = Book.create! name: 'REMOTE' + end + + test "query state by predicate" do + assert @book.proposed? + assert_not @book.written? + assert_not @book.published? + end + + test "query state with symbol" do + assert_equal :proposed, @book.status + end + + test "update by declaration" do + @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] + assert_equal 2, Book::STATUS[:published] + end +end diff --git a/activerecord/test/models/book.rb b/activerecord/test/models/book.rb index 5458a28cc9..997c088176 100644 --- a/activerecord/test/models/book.rb +++ b/activerecord/test/models/book.rb @@ -2,8 +2,10 @@ class Book < ActiveRecord::Base has_many :authors has_many :citations, :foreign_key => 'book1_id' - has_many :references, -> { distinct }, :through => :citations, :source => :reference_of + has_many :references, -> { distinct }, through: :citations, source: :reference_of has_many :subscriptions - has_many :subscribers, :through => :subscriptions + has_many :subscribers, through: :subscriptions + + enum status: %i( proposed written published ) end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 88a686d436..5f7ce2c15c 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -94,6 +94,7 @@ ActiveRecord::Schema.define do create_table :books, :force => true do |t| t.integer :author_id t.column :name, :string + t.column :status, :integer, default: 0 end create_table :booleans, :force => true do |t| |