module PhonyRails::Extension

Public Instance Methods

assign_values_for_phony_symbol_options(options) click to toggle source
# File lib/phony_rails.rb, line 169
def assign_values_for_phony_symbol_options(options)
  symbol_options = %i[country_number default_country_number country_code default_country_code]
  symbol_options.each do |option|
    options[option] = send(options[option]) if options[option].is_a?(Symbol)
  end
end
cache_original_attribute(current_instance, attribute) click to toggle source
# File lib/phony_rails.rb, line 177
def cache_original_attribute(current_instance, attribute)
  attribute_name = "#{attribute}_original"
  current_instance.define_singleton_method("#{attribute_name}=") { |value| instance_variable_set("@#{attribute_name}", value) }
  current_instance.define_singleton_method(attribute_name) { instance_variable_get("@#{attribute_name}") }
  current_instance.public_send("#{attribute}_original=", current_instance.public_send(attribute.to_s))
end
set_phony_normalized_numbers(current_instance, attributes, options = {}) click to toggle source

This methods sets the attribute to the normalized version. It also adds the country_code (number), eg. 31 for NL numbers.

# File lib/phony_rails.rb, line 152
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)

    cache_original_attribute(current_instance, attribute) if options[:normalize_when_valid]
    new_value = PhonyRails.normalize_number(send(attribute), options, current_instance)
    current_instance.public_send("#{attribute_name}=", new_value) if new_value || attribute_name != attribute
  end
end