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

add lambda support for default country code #181

Merged
merged 1 commit into from
Sep 5, 2018
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
3 changes: 3 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ Style/Documentation:
- 'lib/phony_rails.rb'
- 'lib/phony_rails/string_extensions.rb'
- 'lib/validators/phony_validator.rb'

Style/RescueModifier:
Enabled: false
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: ruby
rvm:
- 2.2.5
- 2.3.1
- 2.3.7
- 2.4.4
- 2.5.1
script:
- bundle exec rspec spec
- bundle exec rubocop
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
phony_rails (0.14.7)
phony_rails (0.14.8)
activesupport (>= 3.0)
phony (> 2.15)

Expand Down Expand Up @@ -74,7 +74,7 @@ GEM
parallel (1.12.0)
parser (2.4.0.2)
ast (~> 2.3)
phony (2.16.3)
phony (2.16.5)
powerpack (0.1.1)
pry (0.11.3)
coderay (~> 1.1.0)
Expand Down Expand Up @@ -140,4 +140,4 @@ DEPENDENCIES
sqlite3

BUNDLED WITH
1.16.1
1.16.4
19 changes: 12 additions & 7 deletions lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def self.country_codes_hash
# :extension => Include the extension. (default: true)
# This idea came from:
# http://www.redguava.com.au/2011/06/rails-convert-phone-numbers-to-international-format-for-sms/
def self.normalize_number(number, options = {})
def self.normalize_number(number, options = {}, current_instance = nil)
return if number.nil?
original_number = number
number = number.dup # Just to be sure, we don't want to change the original.
Expand All @@ -58,7 +58,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 = extract_default_country_number(options)
elsif _default_country_number = extract_default_country_number(options, current_instance)
options[:add_plus] = true if options[:add_plus].nil?
number = normalize_number_default_country(number, _default_country_number)
end
Expand Down Expand Up @@ -88,8 +88,13 @@ def self.normalize_number_default_country(number, default_country_number)
number
end

def self.extract_default_country_number(options = {})
options[:default_country_number] || country_number_for(options[:default_country_code]) || default_country_number
def self.extract_default_country_number(options = {}, current_instance = nil)
country_code = if current_instance.present? && options[:default_country_code].respond_to?(:call)
options[:default_country_code].call(current_instance) rescue nil
else
options[:default_country_code]
end
options[:default_country_number] || country_number_for(country_code) || default_country_number
end

def self.country_code_from_number(number)
Expand Down Expand Up @@ -136,7 +141,7 @@ module Extension

# This methods sets the attribute to the normalized version.
# It also adds the country_code (number), eg. 31 for NL numbers.
def set_phony_normalized_numbers(attributes, options = {})
def set_phony_normalized_numbers(current_instance, attributes, options = {})
options = options.dup
assign_values_for_phony_symbol_options(options)
if respond_to?(:country_code)
Expand All @@ -146,7 +151,7 @@ def set_phony_normalized_numbers(attributes, options = {})
attributes.each do |attribute|
attribute_name = options[:as] || attribute
raise("No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
new_value = PhonyRails.normalize_number(send(attribute), options)
new_value = PhonyRails.normalize_number(send(attribute), options, current_instance)
send("#{attribute_name}=", new_value) if new_value || attribute_name != attribute
end
end
Expand Down Expand Up @@ -178,7 +183,7 @@ def phony_normalize(*attributes)

# Add before validation that saves a normalized version of the phone number
before_validation conditional do
set_phony_normalized_numbers(attributes, options)
set_phony_normalized_numbers(self, attributes, options)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/phony_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module PhonyRails
VERSION = '0.14.7'
VERSION = '0.14.8'
end
2 changes: 1 addition & 1 deletion phony_rails.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require File.expand_path('../lib/phony_rails/version', __FILE__)
require File.expand_path('lib/phony_rails/version', __dir__)

Gem::Specification.new do |gem|
gem.authors = ['Joost Hietbrink']
Expand Down
26 changes: 25 additions & 1 deletion spec/lib/phony_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
describe 'with the bang!' do
it 'changes the String using the bang method' do
# Mutable String
s = +'0101234123' rescue '0101234123' # rubocop:disable Style/RescueModifier
s = +'0101234123' rescue '0101234123'
expect(s.phony_formatted!(normalize: :NL, format: :international)).to eql('+31 10 123 4123')
expect(s).to eql('+31 10 123 4123')
end
Expand Down Expand Up @@ -722,6 +722,30 @@ class NormalHome < ActiveRecord::Base
expect(model.symboled_phone).to eql('+33606060606')
end

context 'relational normalization' do
it 'should normalize based on custom attribute of the current model' do
model_klass.phony_normalize :phone_number, default_country_code: ->(instance) { instance.custom_country_code }
model = model_klass.new phone_number: '012 416 0001', custom_country_code: 'MY'
expect(model).to be_valid
expect(model.phone_number).to eq('+60124160001')
end

it 'should normalize based on specific attribute of the associative model' do
model_klass.phony_normalize :phone_number, default_country_code: ->(instance) { instance.home_country.country_code }
home_country = double('home_country', country_code: 'MY')
model = model_klass.new phone_number: '012 416 0001', home_country: home_country
expect(model).to be_valid
expect(model.phone_number).to eq('+60124160001')
end

it 'should normalize based on default value if missing associative model' do
model_klass.phony_normalize :phone_number, default_country_code: ->(instance) { instance.home_country&.country_code || 'MY' }
model = model_klass.new phone_number: '012 416 0001', home_country: nil
expect(model).to be_valid
expect(model.phone_number).to eq('+60124160001')
end
end

context 'conditional normalization' do
context 'standalone methods' do
it 'should only normalize if the :if conditional is true' do
Expand Down
5 changes: 4 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
module SharedModelMethods
extend ActiveSupport::Concern
included do
attr_accessor :phone_method, :phone1_method, :symboled_phone_method, :country_code, :country_code_attribute, :recipient, :delivery_method
attr_accessor(
:country_code, :country_code_attribute, :custom_country_code, :delivery_method,
:home_country, :phone_method, :phone1_method, :recipient, :symboled_phone_method
)
phony_normalized_method :phone_attribute # adds normalized_phone_attribute method
phony_normalized_method :phone_method # adds normalized_phone_method method
phony_normalized_method :phone1_method, default_country_code: 'DE' # adds normalized_phone_method method
Expand Down