Skip to content

Commit

Permalink
Fixing joost#195 - Original value should be cached in correct instanc…
Browse files Browse the repository at this point in the history
…e var

Previously was cached in `@original` and would collide if more than 1
definition existed for a class.
  • Loading branch information
dlikhten committed Jun 26, 2019
1 parent 1347bd2 commit 7b36af7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ def assign_values_for_phony_symbol_options(options)
end

def cache_original_attribute(current_instance, attribute)
current_instance.define_singleton_method("#{attribute}_original=") { |value| @original = value }
current_instance.define_singleton_method("#{attribute}_original") { @original }
attribute_name = "#{attribute}_original"
current_instance.define_singleton_method("#{attribute_name}=") { |value| instance_variable_set("@#{attribute_name}", value) }
current_instance.define_singleton_method(attribute_name) { instance_variable_get("@#{attribute_name}") }
current_instance.public_send("#{attribute}_original=", current_instance.public_send(attribute.to_s))
end

Expand Down
21 changes: 20 additions & 1 deletion spec/lib/validators/phony_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ def email
end
#--------------------
class NormalizabledPhoneHome < ActiveRecord::Base
attr_accessor :phone_number, :country_code
attr_accessor :phone_number, :phone_number2, :country_code
validates_plausible_phone :phone_number
validates_plausible_phone :phone_number2
phony_normalize :phone_number, country_code: 'PL', normalize_when_valid: true
phony_normalize :phone_number2, country_code: 'PL', normalize_when_valid: true
end

#-----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -667,6 +669,23 @@ class NormalizabledPhoneHome < ActiveRecord::Base
expect(@home).to be_valid
expect(@home.phone_number).to eql('+48799449595')
end

it 'does not normalize code after validation with multiple attributes' do
@home = NormalizabledPhoneHome.new
@home.phone_number = '+44 799 449 595'
@home.phone_number2 = '+44 222 111 333'
@home.country_code = 'PL'

expect(@home).to_not be_valid
expect(@home.phone_number).to eql('+44 799 449 595')
expect(@home.phone_number2).to eql('+44 222 111 333')

@home.phone_number = '+48 799 449 595'
@home.phone_number2 = '+48 222 111 333'
expect(@home).to be_valid
expect(@home.phone_number).to eql('+48799449595')
expect(@home.phone_number2).to eql('+48222111333')
end
end
end
end

0 comments on commit 7b36af7

Please sign in to comment.