Skip to content

Commit 9c74b30

Browse files
Update deprecated usage of attribute_changed? in callbacks (#2929)
Co-authored-by: Aleksandar N. Kostadinov <akostadi@redhat.com>
1 parent b2e3e66 commit 9c74b30

26 files changed

Lines changed: 119 additions & 81 deletions

app/events/oidc/proxy_changed_event.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def self.create(proxy)
2121
def self.valid?(proxy)
2222
service = proxy.try(:service)
2323
return unless service
24-
service.backend_version.oauth? || service.backend_version_change&.include?('oauth')
24+
25+
# TODO: second assertion is probably useless, or not. Revisit this in the future once we've seen this is safe. See https://github.com/3scale/porta/pull/2929/files#r846345372
26+
service.backend_version.oauth? || service.backend_version_change_to_be_saved&.include?('oauth') || service.saved_change_to_backend_version&.include?('oauth')
2527
end
2628
end

app/lib/authentication/by_password.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def transparently_migrate_password(unencrypted_password)
3939
end
4040

4141
def just_changed_password?
42-
previous_changes.key?('password_digest') || super
42+
saved_change_to_password_digest? || super
4343
end
4444

4545
private
@@ -113,7 +113,7 @@ def password_required?
113113
end
114114

115115
def just_changed_password?
116-
previous_changes.key?('crypted_password')
116+
saved_change_to_crypted_password?
117117
end
118118

119119
private

app/lib/backend/model_extensions/cinstance.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def delete_backend_application
4444
end
4545

4646
def update_backend_user_key_to_application_id_mapping
47-
user_key_was, current_user_key = previous_changes[:user_key]
47+
user_key_was, current_user_key = saved_change_to_user_key
4848

49-
if previously_changed?(:user_key) && service.id.present? && user_key_was.present?
49+
if saved_change_to_user_key? && service.id.present? && user_key_was.present?
5050
ThreeScale::Core::Application.delete_id_by_key(service.backend_id, user_key_was)
5151
end
5252

@@ -78,8 +78,8 @@ def set_application_id
7878
end
7979

8080
def update_provider_backend_service_if_user_key_changed
81-
if previously_changed?(:user_key)
82-
user_key_was, user_key = previous_changes[:user_key]
81+
if saved_change_to_user_key?
82+
user_key_was, user_key = saved_change_to_user_key
8383

8484
if user_account && user_account.provider? && user_key_was.present? && user_account.services.present?
8585
ThreeScale::Core::Service.change_provider_key!(user_key_was, user_key)

app/lib/backend/model_extensions/provider.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ module Provider
44
extend ActiveSupport::Concern
55

66
included do
7-
after_commit :update_backend_default_service_id, :if => :provider?, :unless => :master?
7+
after_commit :update_backend_default_service_id, if: -> { provider? && saved_change_to_default_service_id? }, unless: :master?
88
end
99

1010
def update_backend_default_service_id
1111
return if destroyed?
12-
return unless previously_changed?(:default_service_id)
1312
services.default.update_backend_service
1413
end
1514
end

app/lib/backend/model_extensions/usage_limit.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def update_backend_usage_limit
2727

2828
def delete_backend_usage_limit
2929
if plan_and_service?
30-
original_period = previously_changed?(:period) ? period_previous_change.compact.first : period
30+
original_period = saved_change_to_period? ? saved_change_to_period.compact.first : period
3131
ThreeScale::Core::UsageLimit.delete(service.backend_id, plan.backend_id, metric_id, original_period)
3232
end
3333

app/lib/permalink_fu.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def permalink(attr_name, options = {})
2525
private
2626

2727
def create_unique_permalink
28-
return if permalink.present? && !permalink_changed?
28+
return if permalink.present? && !will_save_change_to_permalink?
29+
2930
base_permalink = build_permalink_from_attribute
3031
count = where_match_permalink_with_conditions(base_permalink).count
3132
self.permalink = count.positive? ? "#{base_permalink}-#{count + 1}" : base_permalink

app/lib/redhat_customer_portal_support.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ def redhat_customer_authentication_provider
1414
end
1515

1616
def redhat_account_recently_verified?
17-
extra_fields_change = previous_changes['extra_fields']
17+
extra_fields_change = saved_change_to_extra_fields
1818

1919
return false unless extra_fields_change
2020

21-
verified_by_was = extra_fields_change.first['red_hat_account_verified_by']
22-
verified_by = extra_fields_change.last['red_hat_account_verified_by']
21+
verified_by_was = extra_fields_change.first[:red_hat_account_verified_by]
22+
verified_by = extra_fields_change.last[:red_hat_account_verified_by]
2323

2424
verified_by_was.blank? && verified_by.present?
2525
end
2626

2727
def recently_suspended?
28-
previous_changes['state'] && suspended?
28+
saved_change_to_attribute(:state) && suspended?
2929
end
3030

3131
private

app/models/account/billing.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Account::Billing
55

66
included do
77
has_many :invoices, :foreign_key => 'buyer_account_id'
8-
after_save :update_invoices_vat_rates
8+
after_save :update_invoices_vat_rates, if: :saved_change_to_vat_rate?
99
before_destroy :check_unresolved_invoices
1010
end
1111

@@ -33,9 +33,7 @@ def billable_contracts_with_trial_period_expired(now)
3333
protected
3434

3535
def update_invoices_vat_rates
36-
if previously_changed?(:vat_rate) || changes.key?(:vat_rate)
37-
self.invoices.not_frozen.reorder('').update_all(:vat_rate => self.vat_rate)
38-
end
36+
invoices.not_frozen.reorder('').update_all(vat_rate: vat_rate)
3937
end
4038

4139
# Will prevent the buyer from destroying if there are unresolved

app/models/account/credit_card.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# frozen_string_literal: true
2+
13
# TODO: will become a CreditCard model by itself soon
2-
module Account::CreditCard
4+
module Account::CreditCard # rubocop:disable Metrics/ModuleLength(RuboCop)
35
extend ActiveSupport::Concern
46

57
included do
@@ -113,9 +115,9 @@ def unstore_credit_card!
113115
end
114116

115117
def notify_credit_card_change
116-
credit_card_changes = previous_changes.slice(credit_card_stored_attribute,
117-
:credit_card_partial_number,
118-
:credit_card_expires_on)
118+
credit_card_changes = saved_changes.slice(credit_card_stored_attribute,
119+
:credit_card_partial_number,
120+
:credit_card_expires_on)
119121

120122
return unless credit_card_changes.present?
121123

app/models/account/provider_domains.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# frozen_string_literal: true
22

3-
module Account::ProviderDomains
3+
module Account::ProviderDomains # rubocop:disable Metrics/ModuleLength
44
extend ActiveSupport::Concern
55

6-
included do
6+
included do # rubocop:disable Metrics/BlockLength
77
include ThreeScale::DomainSubstitution::Account
88

99
with_options :if => :validate_domains? do |provider|
@@ -78,7 +78,7 @@ def same_domain(domain)
7878
end
7979

8080
def domains_changed?
81-
attribute_changed?(:domain) || attribute_changed?(:self_domain)
81+
saved_change_to_domain? || saved_change_to_self_domain?
8282
end
8383

8484
def publish_domain_events

0 commit comments

Comments
 (0)