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 runtime errors not argument errors when :as attr undefined #61

Merged
merged 1 commit into from Aug 29, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 14 additions & 6 deletions spec/lib/phony_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down