Permalink
Browse files

Merge pull request #8705 from amparo-luna/change_update_attributes_to…

…_update

Rename update_attributes method to update
  • Loading branch information...
2 parents b4a23ed + d826f14 commit cb2bd4aa619d9329c42aaf6d9f8eacc616ce53f4 @guilleiguaran guilleiguaran committed Jan 3, 2013
Showing with 181 additions and 132 deletions.
  1. +2 −2 actionpack/lib/action_controller/metal/strong_parameters.rb
  2. +4 −4 actionpack/lib/action_view/helpers/form_helper.rb
  3. +1 −1 actionpack/lib/action_view/helpers/form_options_helper.rb
  4. +1 −1 actionpack/lib/action_view/record_identifier.rb
  5. +7 −0 activerecord/CHANGELOG.md
  6. +1 −1 activerecord/examples/performance.rb
  7. +1 −1 activerecord/lib/active_record/associations/has_one_through_association.rb
  8. +2 −2 activerecord/lib/active_record/attribute_methods/dirty.rb
  9. +2 −2 activerecord/lib/active_record/callbacks.rb
  10. +1 −1 activerecord/lib/active_record/locking/optimistic.rb
  11. +1 −1 activerecord/lib/active_record/nested_attributes.rb
  12. +10 −6 activerecord/lib/active_record/persistence.rb
  13. +1 −1 activerecord/lib/active_record/relation.rb
  14. +2 −2 activerecord/lib/active_record/timestamp.rb
  15. +4 −4 activerecord/test/cases/associations/belongs_to_associations_test.rb
  16. +4 −3 activerecord/test/cases/associations/eager_test.rb
  17. +1 −1 activerecord/test/cases/associations/has_many_through_associations_test.rb
  18. +4 −4 activerecord/test/cases/autosave_association_test.rb
  19. +2 −2 activerecord/test/cases/base_test.rb
  20. +1 −1 activerecord/test/cases/dirty_test.rb
  21. +1 −1 activerecord/test/cases/locking_test.rb
  22. +54 −54 activerecord/test/cases/nested_attributes_test.rb
  23. +42 −5 activerecord/test/cases/persistence_test.rb
  24. +1 −1 activerecord/test/cases/transaction_isolation_test.rb
  25. +4 −4 activerecord/test/cases/transactions_test.rb
  26. +1 −1 activerecord/test/models/reference.rb
  27. +1 −1 activesupport/lib/active_support/core_ext/hash/except.rb
  28. +1 −1 guides/code/getting_started/app/controllers/posts_controller.rb
  29. +3 −3 guides/source/active_record_basics.md
  30. +2 −2 guides/source/active_record_callbacks.md
  31. +3 −3 guides/source/active_record_validations.md
  32. +3 −3 guides/source/form_helpers.md
  33. +4 −4 guides/source/getting_started.md
  34. +3 −3 guides/source/layouts_and_rendering.md
  35. +2 −2 railties/lib/rails/generators/active_model.rb
  36. +1 −1 railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
  37. +1 −1 railties/test/generators/scaffold_controller_generator_test.rb
  38. +2 −2 railties/test/generators/scaffold_generator_test.rb
@@ -41,7 +41,7 @@ def initialize(param) # :nodoc:
# permitted.class # => ActionController::Parameters
# permitted.permitted? # => true
#
- # Person.first.update_attributes!(permitted)
+ # Person.first.update!(permitted)
# # => #<Person id: 1, name: "Francesco", age: 22, role: "user">
#
# It provides a +permit_all_parameters+ option that controls the top-level
@@ -329,7 +329,7 @@ def each_element(object)
# # into a 400 Bad Request reply.
# def update
# redirect_to current_account.people.find(params[:id]).tap { |person|
- # person.update_attributes!(person_params)
+ # person.update!(person_params)
# }
# end
#
@@ -101,9 +101,9 @@ module Helpers
# and generate HTML accordingly.
#
# The controller would receive the form data again in <tt>params[:person]</tt>, ready to be
- # passed to <tt>Person#update_attributes</tt>:
+ # passed to <tt>Person#update</tt>:
#
- # if @person.update_attributes(params[:person])
+ # if @person.update(params[:person])
# # success
# else
# # error handling
@@ -895,7 +895,7 @@ def text_area(object_name, method, options = {})
# invoice the user unchecks its check box, no +paid+ parameter is sent. So,
# any mass-assignment idiom like
#
- # @invoice.update_attributes(params[:invoice])
+ # @invoice.update(params[:invoice])
#
# wouldn't update the flag.
#
@@ -1573,7 +1573,7 @@ def label(method, text = nil, options = {}, &block)
# invoice the user unchecks its check box, no +paid+ parameter is sent. So,
# any mass-assignment idiom like
#
- # @invoice.update_attributes(params[:invoice])
+ # @invoice.update(params[:invoice])
#
# wouldn't update the flag.
#
@@ -130,7 +130,7 @@ module FormOptionsHelper
# the user deselects all roles from +role_ids+ multiple select box, no +role_ids+ parameter is sent. So,
# any mass-assignment idiom like
#
- # @user.update_attributes(params[:user])
+ # @user.update(params[:user])
#
# wouldn't update roles.
#
@@ -17,7 +17,7 @@ module ActionView
# # controller
# def update
# post = Post.find(params[:id])
- # post.update_attributes(params[:post])
+ # post.update(params[:post])
#
# redirect_to(post) # Calls polymorphic_url(post) which in turn calls post_url(post)
# end
@@ -1,5 +1,12 @@
## Rails 4.0.0 (unreleased) ##
+* Rename `update_attributes` to `update`, keep `update_attributes` as an alias for `update` method.
+ This is a soft-deprecation for `update_attributes`, although it will still work without any
+ deprecation message in 4.0 is recommended to start using `update` since `update_attributes` will be
+ deprecated and removed in future versions of Rails.
+
+ *Amparo Luna + Guillermo Iguaran*
+
* `after_commit` and `after_rollback` now validate the `:on` option and raise an `ArgumentError`
if it is not one of `:create`, `:destroy` or ``:update`
@@ -143,7 +143,7 @@ def self.email
end
x.report 'Resource#update' do
- Exhibit.first.update_attributes(:name => 'bob')
+ Exhibit.first.update(name: 'bob')
end
x.report 'Resource#destroy' do
@@ -23,7 +23,7 @@ def create_through_record(record)
attributes = construct_join_attributes(record)
if through_record
- through_record.update_attributes(attributes)
+ through_record.update(attributes)
elsif owner.new_record?
through_proxy.build(attributes)
else
@@ -71,11 +71,11 @@ def write_attribute(attr, value)
super(attr, value)
end
- def update(*)
+ def update_record(*)
partial_writes? ? super(keys_for_partial_write) : super
end
- def create(*)
+ def create_record(*)
partial_writes? ? super(keys_for_partial_write) : super
end
@@ -299,11 +299,11 @@ def create_or_update #:nodoc:
run_callbacks(:save) { super }
end
- def create #:nodoc:
+ def create_record #:nodoc:
run_callbacks(:create) { super }
end
- def update(*) #:nodoc:
+ def update_record(*) #:nodoc:
run_callbacks(:update) { super }
end
end
@@ -66,7 +66,7 @@ def increment_lock
send(lock_col + '=', previous_lock_value + 1)
end
- def update(attribute_names = @attributes.keys) #:nodoc:
+ def update_record(attribute_names = @attributes.keys) #:nodoc:
return super unless locking_enabled?
return 0 if attribute_names.empty?
@@ -58,7 +58,7 @@ class TooManyRecords < ActiveRecordError
# It also allows you to update the avatar through the member:
#
# params = { member: { avatar_attributes: { id: '2', icon: 'sad' } } }
- # member.update_attributes params[:member]
+ # member.update params[:member]
# member.avatar.icon # => 'sad'
#
# By default you will only be able to set and update attributes on the
@@ -212,25 +212,29 @@ def update_attribute(name, value)
# Updates the attributes of the model from the passed-in hash and saves the
# record, all wrapped in a transaction. If the object is invalid, the saving
# will fail and false will be returned.
- def update_attributes(attributes)
+ def update(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
assign_attributes(attributes)
save
end
end
+
+ alias update_attributes update
- # Updates its receiver just like +update_attributes+ but calls <tt>save!</tt> instead
+ # Updates its receiver just like +update+ but calls <tt>save!</tt> instead
# of +save+, so an exception is raised if the record is invalid.
- def update_attributes!(attributes)
+ def update!(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
assign_attributes(attributes)
save!
end
end
+
+ alias update_attributes! update!
# Updates a single attribute of an object, without having to explicitly call save on that object.
#
@@ -406,13 +410,13 @@ def relation_for_destroy
def create_or_update
raise ReadOnlyRecord if readonly?
- result = new_record? ? create : update
+ result = new_record? ? create_record : update_record
result != false
end
# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
- def update(attribute_names = @attributes.keys)
+ def update_record(attribute_names = @attributes.keys)
attributes_with_values = arel_attributes_with_values_for_update(attribute_names)
if attributes_with_values.empty?
@@ -426,7 +430,7 @@ def update(attribute_names = @attributes.keys)
# Creates a record with values matching those of the instance attributes
# and returns its id.
- def create(attribute_names = @attributes.keys)
+ def create_record(attribute_names = @attributes.keys)
attributes_values = arel_attributes_with_values_for_create(attribute_names)
new_id = self.class.unscoped.insert attributes_values
@@ -308,7 +308,7 @@ def update(id, attributes)
id.map.with_index { |one_id, idx| update(one_id, attributes[idx]) }
else
object = find(id)
- object.update_attributes(attributes)
+ object.update(attributes)
object
end
end
@@ -43,7 +43,7 @@ def initialize_dup(other) # :nodoc:
private
- def create
+ def create_record
if self.record_timestamps
current_time = current_time_from_proper_timezone
@@ -57,7 +57,7 @@ def create
super
end
- def update(*args)
+ def update_record(*args)
if should_record_timestamps?
current_time = current_time_from_proper_timezone
@@ -317,12 +317,12 @@ def test_belongs_to_counter_after_save
assert_equal 1, Topic.find(topic.id)[:replies_count]
end
- def test_belongs_to_counter_after_update_attributes
- topic = Topic.create!(:title => "37s")
- topic.replies.create!(:title => "re: 37s", :content => "rails")
+ def test_belongs_to_counter_after_update
+ topic = Topic.create!(title: "37s")
+ topic.replies.create!(title: "re: 37s", content: "rails")
assert_equal 1, Topic.find(topic.id)[:replies_count]
- topic.update_attributes(:title => "37signals")
+ topic.update(title: "37signals")
assert_equal 1, Topic.find(topic.id)[:replies_count]
end
@@ -212,16 +212,17 @@ def test_finding_with_includes_on_belongs_to_association_with_same_include_inclu
def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
post = posts(:welcome)
- post.update_attributes!(:author => nil)
- post = assert_queries(1) { Post.all.merge!(:includes => {:author_with_address => :author_address}).find(post.id) } # find the post, then find the author which is null so no query for the author or address
+ post.update!(author: nil)
+ post = assert_queries(1) { Post.all.merge!(includes: {author_with_address: :author_address}).find(post.id) }
+ # find the post, then find the author which is null so no query for the author or address
assert_no_queries do
assert_equal nil, post.author_with_address
end
end
def test_finding_with_includes_on_null_belongs_to_polymorphic_association
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
- sponsor.update_attributes!(:sponsorable => nil)
+ sponsor.update!(sponsorable: nil)
sponsor = assert_queries(1) { Sponsor.all.merge!(:includes => :sponsorable).find(sponsor.id) }
assert_no_queries do
assert_equal nil, sponsor.sponsorable
@@ -706,7 +706,7 @@ def test_through_association_readonly_should_be_false
def test_can_update_through_association
assert_nothing_raised do
- people(:michael).posts.first.update_attributes!(:title => "Can write")
+ people(:michael).posts.first.update!(title: "Can write")
end
end
@@ -161,16 +161,16 @@ def test_callbacks_firing_order_on_create
end
def test_callbacks_firing_order_on_update
- eye = Eye.create(:iris_attributes => {:color => 'honey'})
- eye.update_attributes(:iris_attributes => {:color => 'green'})
+ eye = Eye.create(iris_attributes: {color: 'honey'})
+ eye.update(iris_attributes: {color: 'green'})
assert_equal [true, false], eye.after_update_callbacks_stack
end
def test_callbacks_firing_order_on_save
- eye = Eye.create(:iris_attributes => {:color => 'honey'})
+ eye = Eye.create(iris_attributes: {color: 'honey'})
assert_equal [false, false], eye.after_save_callbacks_stack
- eye.update_attributes(:iris_attributes => {:color => 'blue'})
+ eye.update(iris_attributes: {color: 'blue'})
assert_equal [false, false, false, false], eye.after_save_callbacks_stack
end
end
@@ -593,7 +593,7 @@ def test_readonly_attributes
post.reload
assert_equal "cannot change this", post.title
- post.update_attributes(:title => "try to change", :body => "changed")
+ post.update(title: "try to change", body: "changed")
post.reload
assert_equal "cannot change this", post.title
assert_equal "changed", post.body
@@ -1001,7 +1001,7 @@ def test_reload
def test_reload_with_exclusive_scope
dev = DeveloperCalledDavid.first
- dev.update_attributes!( :name => "NotDavid" )
+ dev.update!(name: "NotDavid" )
assert_equal dev, dev.reload
end
@@ -517,7 +517,7 @@ def test_previous_changes
assert !pirate.previous_changes.key?('created_on')
pirate = Pirate.find_by_catchphrase("Thar She Blows!")
- pirate.update_attributes(:catchphrase => "Ahoy!")
+ pirate.update(catchphrase: "Ahoy!")
assert_equal 2, pirate.previous_changes.size
assert_equal ["Thar She Blows!", "Ahoy!"], pirate.previous_changes['catchphrase']
@@ -207,7 +207,7 @@ def test_readonly_attributes
s.reload
assert_equal "unchangeable name", s.name
- s.update_attributes(:name => "changed name")
+ s.update(name: "changed name")
s.reload
assert_equal "unchangeable name", s.name
end
Oops, something went wrong.

0 comments on commit cb2bd4a

Please sign in to comment.