Skip to content

Commit

Permalink
Fix to work on Ruby 1.9.3, example and changelog improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosantoniodasilva committed Nov 2, 2013
1 parent e3e7786 commit 6963e89
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
48 changes: 24 additions & 24 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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
Expand All @@ -50,7 +51,6 @@

vendor.users.to_a # => No exception is raised


Fixes #12242, #9517, #10240.

*Paul Nikitochkin*
Expand Down
10 changes: 5 additions & 5 deletions activerecord/lib/active_record/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/enum_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

@dhh dhh commented on 6963e89 Nov 2, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the cleanup! 👍

Please sign in to comment.