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

Fixes #55 - Validation fails if record country code does not match code ... #56

Merged
merged 2 commits into from
Jan 21, 2015
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/.rvmrc
/.idea/
.ruby-gemset
.ruby-version
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ or correct country code:

validates_plausible_phone :phone_number, :country_code => 'AU'

#### Allowing records country codes to not match phone number country codes

You may have a record specifying one country (via a `country_code` attribute) but using a phone number from another country. For example, your record may be from Japan but have a phone number from the Philippines. By default, `phony_rails` will consider your record's `country_code` as part of the validation. If that country doesn't match the country code in the phone number, validation will fail.

If you want to allow records from one country to have phone numbers from a different one, there are a couple of options you can use: `ignore_record_country_number` and `ignore_record_country_code`. Use them like so:

validates :phone_number, :phony_plausible => { :ignore_record_country_code => true, :ignore_record_country_number => true}

Obviously, you don't have to use both, and you may not need or want to set either. They only matter if your model `responds_to?` `:country_code` or `:country_number`.

### Display / Views

In your views use:
Expand Down
4 changes: 2 additions & 2 deletions lib/validators/phony_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def country_number
end

def record_country_number
@record.country_number if @record.respond_to?(:country_number)
@record.country_number if @record.respond_to?(:country_number) && !options[:ignore_record_country_number]
end

def country_number_from_country_code
Expand All @@ -34,7 +34,7 @@ def country_code
end

def record_country_code
@record.country_code if @record.respond_to?(:country_code)
@record.country_code if @record.respond_to?(:country_code) && !options[:ignore_record_country_code]
end

end
Expand Down
23 changes: 23 additions & 0 deletions spec/lib/validators/phony_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
create_table :polish_helpful_homes do |table|
table.column :phone_number, :string
end

create_table :mismatched_helpful_homes do |table|
table.column :phone_number, :string
end
end

#--------------------
Expand Down Expand Up @@ -98,6 +102,11 @@ class BigHelpfulHome < ActiveRecord::Base
validates_plausible_phone :phone_number, :presence => true, :with => /^\+\d+/, :country_number => "33"
end

#--------------------
class MismatchedHelpfulHome < ActiveRecord::Base
attr_accessor :phone_number, :country_code
validates :phone_number, :phony_plausible => {:ignore_record_country_code => true}
end
#-----------------------------------------------------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------------------------------------------------
Expand All @@ -110,6 +119,7 @@ class BigHelpfulHome < ActiveRecord::Base
FRENCH_NUMBER_WITH_COUNTRY_CODE = '33627899541'
FORMATTED_FRENCH_NUMBER_WITH_COUNTRY_CODE = '+33 627899541'
INVALID_NUMBER = '123456789 123456789 123456789 123456789'
JAPAN_COUNTRY = 'jp'

#-----------------------------------------------------------------------------------------------------------------------
describe PhonyPlausibleValidator do
Expand Down Expand Up @@ -385,6 +395,19 @@ class BigHelpfulHome < ActiveRecord::Base

end

#--------------------
context 'when a phone number does not match the records country' do
before(:each) do
@home = MismatchedHelpfulHome.new
@home.country_code = JAPAN_COUNTRY
@home.phone_number = FRENCH_NUMBER_WITH_COUNTRY_CODE
end

it "should allow this number" do
@home.should be_valid
end
end

end

end