Module: PhonyRails::Extension::ClassMethods
- Defined in:
- lib/phony_rails.rb
Constant Summary collapse
- PHONY_RAILS_COLLECTION_VALID_KEYS =
%i[ add_plus as country_code country_number default_country_code default_country_number enforce_record_country if normalize_when_valid unless ].freeze
Instance Method Summary collapse
-
#phony_normalize(*attributes) ⇒ Object
Use this method on the class level like: phony_normalize :phone_number, :fax_number, :default_country_code => 'NL'.
-
#phony_normalized_method(*attributes) ⇒ Object
Usage: phony_normalized_method :fax_number, :default_country_code => 'US' Creates a normalized_fax_number method.
Instance Method Details
#phony_normalize(*attributes) ⇒ Object
Use this method on the class level like:
phony_normalize :phone_number, :fax_number, :default_country_code => 'NL'
It checks your model object for a a country_code attribute (eg. 'NL') to do the normalizing so make sure you've geocoded before calling this method!
202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/phony_rails.rb', line 202 def phony_normalize(*attributes) = attributes.last.is_a?(Hash) ? attributes.pop : {} .assert_valid_keys(*PHONY_RAILS_COLLECTION_VALID_KEYS) raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if [:as].present? && (attributes.size > 1) [:enforce_record_country] = true if [:enforce_record_country].nil? conditional = create_before_validation_conditional_hash() # Add before validation that saves a normalized version of the phone number before_validation conditional do set_phony_normalized_numbers(self, attributes, ) end end |
#phony_normalized_method(*attributes) ⇒ Object
Usage:
phony_normalized_method :fax_number, :default_country_code => 'US'
Creates a normalized_fax_number method.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/phony_rails.rb', line 220 def phony_normalized_method(*attributes) = attributes.last.is_a?(Hash) ? attributes.pop : {} .assert_valid_keys :country_code, :default_country_code attributes.each do |attribute| raise(StandardError, "Instance method normalized_#{attribute} already exists on #{name} (PhonyRails)") if method_defined?(:"normalized_#{attribute}") define_method :"normalized_#{attribute}" do |*args| = .merge(args.first || {}) () raise(ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)") unless respond_to?(attribute) [:country_code] ||= country_code if respond_to?(:country_code) PhonyRails.normalize_number(send(attribute), ) end end end |