Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise only an exception at validation for non-existing attributes (#19) #20

Merged
merged 1 commit into from Apr 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/phony_rails.rb
Expand Up @@ -49,6 +49,7 @@ def set_phony_normalized_numbers(attributes, options = {})
options[:country_code] ||= self.country_code if self.respond_to?(:country_code)
attributes.each do |attribute|
attribute_name = options[:as] || attribute
raise RuntimeError, "No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)" if not self.respond_to?(attribute_name)
write_attribute(attribute_name, PhonyRails.normalize_number(read_attribute(attribute), options))
end
end
Expand All @@ -69,12 +70,9 @@ def phony_normalize(*attributes)
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
attributes.each do |attribute|
raise ArgumentError, "No attribute #{attribute} found on #{self.name} (PhonyRails)" if not self.attribute_method?(attribute)
# Add before validation that saves a normalized version of the phone number
self.before_validation do
set_phony_normalized_numbers(attributes, options)
end
# Add before validation that saves a normalized version of the phone number
self.before_validation do
set_phony_normalized_numbers(attributes, options)
end
end

Expand Down
17 changes: 16 additions & 1 deletion spec/lib/phony_rails_spec.rb
Expand Up @@ -14,7 +14,7 @@
s.phony_formatted!(:normalize => :NL, :format => :international).should eql('+31 10 1234123')
s.should eql("+31 10 1234123")
end

end

describe 'with normalize option' do
Expand Down Expand Up @@ -143,6 +143,12 @@
Home.phony_normalize(:phone_number, :as => 'phone_number_as_normalized')
}.should_not raise_error(ArgumentError)
end

it "should accept a non existing attribute name" do
lambda {
Dummy.phony_normalize(:non_existing_attribute)
}.should_not raise_error
end
end

describe 'using ActiveRecord#phony_normalized_method' do
Expand Down Expand Up @@ -219,5 +225,14 @@
home.valid?.should be_true
home.phone_number_as_normalized.should eql('31101234123')
end

it "should raise a RuntimeError at validation if the attribute doesn't exist" do
Dummy.phony_normalize :non_existing_attribute

dummy = Dummy.new
lambda {
dummy.valid?
}.should raise_error(RuntimeError)
end
end
end
5 changes: 4 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -26,6 +26,9 @@ class Home < ActiveRecord::Base
phony_normalize :phone_number # normalized on validation
end

class Dummy < Home
end

RSpec.configure do |config|
# some (optional) config here
# some (optional) config here
end