lib/phony_rails.rb in phony_rails-0.14.7 vs lib/phony_rails.rb in phony_rails-0.14.9
- old
+ new
@@ -42,11 +42,11 @@
# :default_country_code => Some fallback code (eg. 'NL') that can be used as default (comes from phony_normalize_numbers method).
# :add_plus => Add a '+' in front so we know the country code is added. (default: true)
# :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.
number, ext = extract_extension(number)
number.gsub!(/[^\(\)\d\+]/, '') # Strips weird stuff from the number
@@ -56,11 +56,11 @@
# (Force) add country_number if missing
# NOTE: do we need to force adding country code? Otherwise we can share logic with next block
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
normalized_number = Phony.normalize(number)
options[:add_plus] = true if options[:add_plus].nil? && Phony.plausible?(normalized_number)
@@ -74,11 +74,11 @@
end
def self.normalize_number_default_country(number, default_country_number)
# 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
- unless number =~ /\A\+/ # if we don't have a +
+ unless number =~ /\A\(?\+/ # if we don't have a +
return "#{default_country_number}#{number}" if Phony.plausible?("#{default_country_number}#{number}") || !Phony.plausible?(number) || country_code_from_number(number).nil?
# If the number starts with ONE zero (two might indicate a country code)
# and this is a plausible number for the default_country
# we prefer that one.
return "#{default_country_number}#{number.gsub(/^0/, '')}" if (number =~ /^0[^0]/) && Phony.plausible?("#{default_country_number}#{number.gsub(/^0/, '')}")
@@ -86,12 +86,17 @@
# number = "#{default_country_number}#{number}" unless Phony.plausible?(number)
# Just return the number unchanged
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)
return nil unless Phony.plausible?(number)
Phony.split(Phony.normalize(number)).first
@@ -134,21 +139,21 @@
included do
private
# 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)
set_country_as = options[:enforce_record_country] ? :country_code : :default_country_code
options[set_country_as] ||= country_code
end
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
def assign_values_for_phony_symbol_options(options)
@@ -176,10 +181,10 @@
conditional = create_before_validation_conditional_hash(options)
# 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
# Usage:
# phony_normalized_method :fax_number, :default_country_code => 'US'