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

Read message value directly from options #141

Merged
merged 1 commit into from May 1, 2016
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
12 changes: 6 additions & 6 deletions lib/validators/phony_validator.rb
Expand Up @@ -15,7 +15,7 @@ def validate_each(record, attribute, value)
private

def error_message
options_value(:message) || :improbable_phone
options[:message] || :improbable_phone
end

def country_number
Expand Down Expand Up @@ -43,11 +43,11 @@ def normalized_country_code
end

def options_value(option)
if options[option].is_a?(Symbol)
@record.send(options[option])
else
options[option]
end
option_value = options[option]

return option_value unless option_value.is_a?(Symbol)

@record.send(option_value)
end
end

Expand Down
54 changes: 54 additions & 0 deletions spec/lib/validators/phony_validator_spec.rb
Expand Up @@ -143,6 +143,28 @@ class SymbolizableHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number, :phone_number_country_code
validates_plausible_phone :phone_number, country_code: :phone_number_country_code
end

#--------------------
class NoModelMethod < HelpfulHome
attr_accessor :phone_number
validates_plausible_phone :phone_number, country_code: :nonexistent_method
end

#--------------------
class MessageOptionUndefinedInModel < HelpfulHome
attr_accessor :phone_number
validates_plausible_phone :phone_number, message: :email
end

#--------------------
class MessageOptionSameAsModelMethod < HelpfulHome
attr_accessor :phone_number
validates_plausible_phone :phone_number, message: :email

def email
'user@example.com'
end
end
#-----------------------------------------------------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -544,5 +566,37 @@ class SymbolizableHelpfulHome < ActiveRecord::Base
expect(@home.errors.messages).to include(phone_number: ['is an invalid number'])
end
end

#--------------------
context 'when a nonexistent method is passed as a symbol to an option other than message' do
it 'raises NoMethodError' do
@home = NoModelMethod.new
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE

expect { @home.save }.to raise_error(NoMethodError)
end
end

#--------------------
context 'when a nonexistent method is passed as a symbol to the message option' do
it 'does not raise an error' do
@home = MessageOptionUndefinedInModel.new
@home.phone_number = INVALID_NUMBER

expect { @home.save }.to_not raise_error
end
end

#--------------------
context 'when an existing Model method is passed as a symbol to the message option' do
it 'does not use the Model method' do
@home = MessageOptionSameAsModelMethod.new
@home.phone_number = INVALID_NUMBER

expect(@home).to_not receive(:email)

@home.save
end
end
end
end