Skip to content
Browse files

Fix to work on Ruby 1.9.3, example and changelog improvements

  • Loading branch information...
1 parent e3e7786 commit 6963e8959f67029903e305d29c2856a481418065 @carlosantoniodasilva carlosantoniodasilva committed Nov 2, 2013
View
48 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*
View
10 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)
View
6 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]
View
4 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

1 comment on commit 6963e89

@dhh
Ruby on Rails member
dhh commented on 6963e89 Nov 2, 2013

Thanks for the cleanup! :+1:

Please sign in to comment.
Something went wrong with that request. Please try again.