Skip to content

Commit

Permalink
Fixes #55 - Validation fails if record country code does not match co…
Browse files Browse the repository at this point in the history
…de in phone

number

Allow for ignoring the record's country code and country number even if
the record responds to them.
  • Loading branch information
Ethan Garofolo committed Apr 23, 2014
1 parent 1f9e68e commit 1e8a51e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
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 county (via a `county_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 `county_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

0 comments on commit 1e8a51e

Please sign in to comment.