Skip to content

Commit

Permalink
Fix: update_attributes is deprecated and will be removed from Rails 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
khiav223577 committed Sep 23, 2019
1 parent 6ae1b73 commit fd9cc72
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 32 deletions.
6 changes: 3 additions & 3 deletions test/cache_active_user_count_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_update_unrelated_column
assert_queries(0){ assert_equal 2, User.cacher.active_count }
assert_cache('active_model_cachers_User_at_active_count' => 2)
ensure
user.update_attributes(name: 'John4')
user.update(name: 'John4')
end

def test_update
Expand All @@ -89,14 +89,14 @@ def test_update
assert_queries(0){ assert_equal 2, User.cacher.active_count }
assert_cache('active_model_cachers_User_at_active_count' => 2)

assert_queries(1){ user.update_attributes(last_login_at: Time.now) }
assert_queries(1){ user.update(last_login_at: Time.now) }
assert_cache({})

assert_queries(1){ assert_equal 3, User.cacher.active_count }
assert_queries(0){ assert_equal 3, User.cacher.active_count }
assert_cache('active_model_cachers_User_at_active_count' => 3)
ensure
user.update_attributes(last_login_at: nil)
user.update(last_login_at: nil)
end

# ----------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions test/cache_all_skills_power_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_update_unrelated_column
assert_queries(0){ assert_equal 80, Skill.cacher.atk_powers[3] }
assert_cache('active_model_cachers_Skill_at_atk_powers' => {1 => 120, 2 => 40, 3 => 80, 4 => 60, 5 => 75, 6 => 70, -1 => 90})

assert_queries(1){ skill.update_attributes(name: 'Crystal Blast') }
assert_queries(1){ skill.update(name: 'Crystal Blast') }
assert_cache('active_model_cachers_Skill_at_atk_powers' => {1 => 120, 2 => 40, 3 => 80, 4 => 60, 5 => 75, 6 => 70, -1 => 90})

assert_queries(0){ assert_equal 90, Skill.cacher.atk_powers[-1] }
Expand All @@ -83,7 +83,7 @@ def test_update
assert_queries(0){ assert_equal 80, Skill.cacher.atk_powers[3] }
assert_cache('active_model_cachers_Skill_at_atk_powers' => {1 => 120, 2 => 40, 3 => 80, 4 => 60, 5 => 75, 6 => 70, -1 => 90})

assert_queries(1){ skill.update_attributes(atk_power: 77) }
assert_queries(1){ skill.update(atk_power: 77) }
assert_cache({})

assert_queries(1){ assert_equal 77, Skill.cacher.atk_powers[-1] }
Expand Down
4 changes: 2 additions & 2 deletions test/cache_at_attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ def test_update
assert_queries(0){ assert_equal 30, Profile.cacher_at(profile.id).point }
assert_cache('active_model_cachers_Profile_at_point_2' => 30)

assert_queries(1){ profile.update_attributes(point: 32) }
assert_queries(1){ profile.update(point: 32) }
assert_cache({})

assert_queries(1){ assert_equal 32, Profile.cacher_at(profile.id).point }
assert_queries(0){ assert_equal 32, Profile.cacher_at(profile.id).point }
assert_cache('active_model_cachers_Profile_at_point_2' => 32)
ensure
profile.update_attributes(point: 30)
profile.update(point: 30)
end

# ----------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions test/cache_at_belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ def test_create
assert_queries(1){ language = Language.create(id: -1, name: 'ko') }
assert_cache('active_model_cachers_User_at_language_id_4' => ActiveModelCachers::NilObject)

user.update_attributes(language: language) # save language_id
user.update(language: language) # save language_id
assert_cache({})

assert_queries(2){ assert_equal 'ko', User.cacher_at(user.id).language.name }
assert_queries(0){ assert_equal 'ko', User.cacher_at(user.id).language.name }
assert_cache('active_model_cachers_User_at_language_id_4' => -1, 'active_model_cachers_Language_-1' => language)
ensure
user.update_attributes(language_id: nil)
user.update(language_id: nil)
language.delete if language
end

Expand Down Expand Up @@ -153,13 +153,13 @@ def test_update
assert_queries(0){ assert_equal 'zh-tw', User.cacher_at(user.id).language.name }
assert_cache('active_model_cachers_User_at_language_id_1' => 2, 'active_model_cachers_Language_2' => language)

assert_queries(1){ language.update_attributes(name: 'ko') }
assert_queries(1){ language.update(name: 'ko') }
assert_cache("active_model_cachers_User_at_language_id_1" => 2)

assert_queries(1){ assert_equal 'ko', User.cacher_at(user.id).language.name }
assert_cache('active_model_cachers_User_at_language_id_1' => 2, 'active_model_cachers_Language_2' => language)
ensure
language.update_attributes(name: 'zh-tw')
language.update(name: 'zh-tw')
end

# ----------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions test/cache_at_has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_update_title
assert_queries(0){ assert_equal [post], User.cacher_at(user.id).posts }
assert_cache('active_model_cachers_User_at_posts_4' => [post])

assert_queries(1){ post.update_attributes(title: '學生退出校園') }
assert_queries(1){ post.update(title: '學生退出校園') }
assert_cache({})

assert_queries(1){ assert_equal [post], User.cacher_at(user.id).posts }
Expand All @@ -174,7 +174,7 @@ def test_update_others_post_title
assert_queries(0){ assert_equal [], User.cacher_at(user1.id).posts }
assert_cache('active_model_cachers_User_at_posts_4' => [])

assert_queries(1){ post.update_attributes(title: '學生退出校園') }
assert_queries(1){ post.update(title: '學生退出校園') }
assert_cache('active_model_cachers_User_at_posts_4' => [])

assert_queries(0){ assert_equal [], User.cacher_at(user1.id).posts }
Expand All @@ -192,7 +192,7 @@ def test_update_title_without_select_foreign_key
assert_queries(0){ assert_equal [post], User.cacher_at(user.id).posts }
assert_cache('active_model_cachers_User_at_posts_4' => [post])

assert_queries(2){ post_with_only_title.update_attributes(title: '學生退出校園') } # send extra query to select user_id
assert_queries(2){ post_with_only_title.update(title: '學生退出校園') } # send extra query to select user_id
assert_cache({})

assert_queries(1){ assert_equal [post], User.cacher_at(user.id).posts }
Expand Down
8 changes: 4 additions & 4 deletions test/cache_at_has_one_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ def test_update
assert_queries(0){ assert_equal 10, User.cacher_at(2).profile.point }
assert_cache('active_model_cachers_Profile_by_user_id_2' => profile)

assert_queries(1){ profile.update_attributes(point: 12) }
assert_queries(1){ profile.update(point: 12) }
assert_cache({})

assert_queries(1){ assert_equal 12, User.cacher_at(2).profile.point }
assert_queries(0){ assert_equal 12, User.cacher_at(2).profile.point }
assert_cache('active_model_cachers_Profile_by_user_id_2' => profile)
ensure
profile.update_attributes(point: 10)
profile.update(point: 10)
end

def test_update_target_which_doesnt_have_cacher
Expand All @@ -161,14 +161,14 @@ def test_update_target_which_doesnt_have_cacher
assert_queries(0){ assert_equal '12345', User.cacher_at(1).contact.phone }
assert_cache('active_model_cachers_Contact_by_user_id_1' => contact)

assert_queries(1){ contact.update_attributes(phone: '12346') }
assert_queries(1){ contact.update(phone: '12346') }
assert_cache({})

assert_queries(1){ assert_equal '12346', User.cacher_at(1).contact.phone }
assert_queries(0){ assert_equal '12346', User.cacher_at(1).contact.phone }
assert_cache('active_model_cachers_Contact_by_user_id_1' => contact)
ensure
contact.update_attributes(phone: '12345')
contact.update(phone: '12345')
end

# ----------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions test/cache_email_valid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_update
assert_queries(0){ assert_equal true, user.cacher.email_valid? }
assert_cache('[email protected]' => true)

assert_queries(1){ user.update_attributes(email: '[email protected]') }
assert_queries(1){ user.update(email: '[email protected]') }
assert_cache('[email protected]' => true)

assert_queries(1){ assert_equal false, user.cacher.email_valid? }
Expand All @@ -107,7 +107,7 @@ def test_update
'[email protected]' => ActiveModelCachers::FalseObject,
)
ensure
user.update_attributes(email: '[email protected]')
user.update(email: '[email protected]')
end

# ----------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions test/cache_self_by_other_column_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ def test_update
assert_queries(0){ assert_equal 10, Profile.cacher.find_by(token: 'tt9wav').point }
assert_cache('active_model_cachers_Profile_by_token_tt9wav' => profile)

assert_queries(1){ profile.update_attributes(point: 12) }
assert_queries(1){ profile.update(point: 12) }
assert_cache({})

assert_queries(1){ assert_equal 12, Profile.cacher.find_by(token: 'tt9wav').point }
assert_queries(0){ assert_equal 12, Profile.cacher.find_by(token: 'tt9wav').point }
assert_cache('active_model_cachers_Profile_by_token_tt9wav' => profile)
ensure
profile.update_attributes(point: 10)
profile.update(point: 10)
end

# ----------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions test/cache_self_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ def test_update
assert_queries(0){ assert_equal 10, Profile.cacher.find_by(id: profile.id).point }
assert_cache('active_model_cachers_Profile_1' => profile)

assert_queries(1){ profile.update_attributes(point: 12) }
assert_queries(1){ profile.update(point: 12) }
assert_cache({})

assert_queries(1){ assert_equal 12, Profile.cacher.find_by(id: profile.id).point }
assert_queries(0){ assert_equal 12, Profile.cacher.find_by(id: profile.id).point }
assert_cache('active_model_cachers_Profile_1' => profile)
ensure
profile.update_attributes(point: 10)
profile.update(point: 10)
end

def test_touch
Expand Down
4 changes: 2 additions & 2 deletions test/cache_user_count_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ def test_update
assert_queries(0){ assert_equal 4, User.cacher.count }
assert_cache('active_model_cachers_User_at_count' => 4)

assert_queries(1){ user.update_attributes(name: '??') }
assert_queries(1){ user.update(name: '??') }
assert_cache('active_model_cachers_User_at_count' => 4)

assert_queries(0){ assert_equal 4, User.cacher.count }
assert_cache('active_model_cachers_User_at_count' => 4)
ensure
user.update_attributes(name: 'John2')
user.update(name: 'John2')
end

# ----------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions test/eager_loaded_class_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_update
assert_cache('active_model_cachers_EagerLoaded::Profile_by_user_id_1' => user.profile)

assert_cache_queries(2) do # Delete cache at self and at self_by_user_id
assert_queries(1){ user.profile.update_attributes(point: 12) }
assert_queries(1){ user.profile.update(point: 12) }
end
assert_cache({})

Expand All @@ -19,7 +19,7 @@ def test_update
assert_queries(0){ assert_equal 12, user.cacher.profile.point }
assert_cache('active_model_cachers_EagerLoaded::Profile_by_user_id_1' => user.profile)
ensure
user.profile.update_attributes(point: 19)
user.profile.update(point: 19)
end

def test_update_belongs_to_association
Expand All @@ -30,12 +30,12 @@ def test_update_belongs_to_association
assert_queries(0){ assert_equal 'zh-tw', EagerLoaded::User.cacher_at(user.id).language.name }
assert_cache('active_model_cachers_EagerLoaded::User_at_language_id_1' => 2, 'active_model_cachers_EagerLoaded::Language_2' => language)

assert_queries(1){ language.update_attributes(name: 'ko') }
assert_queries(1){ language.update(name: 'ko') }
assert_cache("active_model_cachers_EagerLoaded::User_at_language_id_1" => 2)

assert_queries(1){ assert_equal 'ko', EagerLoaded::User.cacher_at(user.id).language.name }
assert_cache('active_model_cachers_EagerLoaded::User_at_language_id_1' => 2, 'active_model_cachers_EagerLoaded::Language_2' => language)
ensure
language.update_attributes(name: 'zh-tw')
language.update(name: 'zh-tw')
end
end
10 changes: 10 additions & 0 deletions test/lib/patches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class ActiveRecord::Base
if not public_method_defined?(:update)
alias origin_rails_update update
def update(*args) # For Rails 3
args.any? ? update_attributes(*args) : origin_rails_update
end
end
end
4 changes: 2 additions & 2 deletions test/shared_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_update
assert_cache('active_model_cachers_SharedCache::Profile_by_user_id_1' => user.profile)

assert_cache_queries(2) do # Delete cache at self and at self_by_user_id
assert_queries(1){ user.profile.update_attributes(point: 12) }
assert_queries(1){ user.profile.update(point: 12) }
end
assert_cache({})

Expand All @@ -19,6 +19,6 @@ def test_update
assert_queries(0){ assert_equal 12, user.cacher.profile.point }
assert_cache('active_model_cachers_SharedCache::Profile_by_user_id_1' => user.profile)
ensure
user.profile.update_attributes(point: 19)
user.profile.update(point: 19)
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

require 'lib/rails_cache'
require 'lib/patches'
require 'lib/seeds'

def user_destroy_dependents_count
Expand Down

0 comments on commit fd9cc72

Please sign in to comment.