Skip to content

Commit

Permalink
Additional tests, corrected formatting for rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryclinesmith committed Apr 2, 2016
1 parent 6465ad6 commit ce7576c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ task :console do
require 'phony_rails'
ARGV.clear
IRB.start
end
end
7 changes: 5 additions & 2 deletions lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'yaml'

module PhonyRails

def self.default_country_code
@default_country_code ||= nil
end
Expand Down Expand Up @@ -55,7 +54,7 @@ def self.normalize_number(number, options = {})
if !Phony.plausible?(number) || _country_number != country_code_from_number(number)
number = "#{_country_number}#{number}"
end
elsif _default_country_number = options[:default_country_number] || country_number_for(options[:default_country_code]) || default_country_number
elsif _default_country_number = extract_default_country_number(options)
options[:add_plus] = true if options[:add_plus].nil?
# We try to add the default country number and see if it is a
# correct phone number. See https://github.com/joost/phony_rails/issues/87#issuecomment-89324426
Expand All @@ -78,6 +77,10 @@ def self.normalize_number(number, options = {})
original_number # If all goes wrong .. we still return the original input.
end

def self.extract_default_country_number(options = {})
options[:default_country_number] || country_number_for(options[:default_country_code]) || default_country_number
end

def self.country_code_from_number(number)
return nil unless Phony.plausible?(number)
Phony.split(Phony.normalize(number)).first
Expand Down
19 changes: 16 additions & 3 deletions spec/lib/phony_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ class NormalHome < ActiveRecord::Base
end

context 'with default_country_code set' do

before { PhonyRails.default_country_code = 'NL' }
after { PhonyRails.default_country_code = nil }

Expand All @@ -343,7 +342,6 @@ class NormalHome < ActiveRecord::Base
expect(PhonyRails.normalize_number('+32 3 226 94 97', default_country_code: 'BE')).to eql('+3232269497')
expect(PhonyRails.normalize_number('0450 764 000', default_country_code: 'AU')).to eql('+61450764000')
end

end
end

Expand Down Expand Up @@ -394,7 +392,6 @@ class NormalHome < ActiveRecord::Base
end

context 'with default_country_code set' do

before { PhonyRails.default_country_code = 'FR' }
after { PhonyRails.default_country_code = nil }

Expand All @@ -407,9 +404,25 @@ class NormalHome < ActiveRecord::Base
is_expected.not_to be_plausible_number empty_number, country_code: 'US'
is_expected.not_to be_plausible_number nil_number, country_code: 'US'
end
end
end

describe 'PhonyRails.default_country' do
before { PhonyRails.default_country_code = 'US' }
after { PhonyRails.default_country_code = nil }

it 'can set a global default country code' do
expect(PhonyRails.default_country_code). to eq 'US'
end

it 'can set a global default country code' do
PhonyRails.default_country_number = '1'
expect(PhonyRails.default_country_number).to eq '1'
end

it 'default country code affects default country number' do
expect(PhonyRails.default_country_number).to eq '1'
end
end

shared_examples_for 'model with PhonyRails' do
Expand Down

0 comments on commit ce7576c

Please sign in to comment.