diff --git a/lib/phony_rails.rb b/lib/phony_rails.rb index 4b956f3..b4833d6 100644 --- a/lib/phony_rails.rb +++ b/lib/phony_rails.rb @@ -67,8 +67,8 @@ def phony_normalize(*attributes) options.assert_valid_keys :country_code, :default_country_code, :as if options[:as].present? raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if attributes.size > 1 - raise ArgumentError, "'#{options[:as]}' is not an attribute on #{self.name}. You might want to use 'phony_normalized_method :#{attributes.first}' (PhonyRails)" if not self.attribute_method?(options[:as]) end + # Add before validation that saves a normalized version of the phone number self.before_validation do set_phony_normalized_numbers(attributes, options) diff --git a/spec/lib/phony_rails_spec.rb b/spec/lib/phony_rails_spec.rb index 7165357..3afb282 100644 --- a/spec/lib/phony_rails_spec.rb +++ b/spec/lib/phony_rails_spec.rb @@ -187,16 +187,16 @@ }.should raise_error(ArgumentError) end - it "should not accept :as option with unexisting attribute name" do + it "should accept :as option with non existing attribute name" do lambda { - model_klass.phony_normalize(:non_existing_attribute, :as => 'non_existing_attribute') - }.should raise_error(ArgumentError) + dummy_klass.phony_normalize(:non_existing_attribute, :as => 'non_existing_attribute') + }.should_not raise_error end - it "should not accept :as option with single non existing attribute name" do + it "should accept :as option with single non existing attribute name" do lambda { - model_klass.phony_normalize(:phone_number, :as => 'something_else') - }.should raise_error(ArgumentError) + dummy_klass.phony_normalize(:phone_number, :as => 'something_else') + }.should_not raise_error end it "should accept :as option with single existing attribute name" do @@ -294,6 +294,14 @@ dummy.valid? }.should raise_error(RuntimeError) end + + it "should raise a RuntimeError at validation if the :as option attribute doesn't exist" do + dummy_klass.phony_normalize :phone_number, :as => :non_existing_attribute + dummy = dummy_klass.new + lambda { + dummy.valid? + }.should raise_error(RuntimeError) + end end end