Module: SmsOnRails::ModelSupport::ClassMethods
- Defined in:
- lib/sms_on_rails/model_support/phone_number.rb
Instance Method Summary collapse
-
#add_number_search(numbers, options = {}, merge_options = {}) ⇒ Object
adds a sanitize search for numbers for the options used for finders Use with find for other ActiveRecord objects Outbound.find :all, add_number_search(, :conditions => ‘outbounds.send_priority == 1’).
-
#digits(text) ⇒ Object
The digits (numbers) only of the phone number Digits are how phone numbers are stored in the database The following all return
12065555555
SmsOnRails::digits(12065555555’) SmsOnRails::digits(206.555.5555’) SmsOnRails::digits(1206-555 5555’). -
#find_all_by_numbers(numbers, options = {}, merge_options = {}) ⇒ Object
Find all numbers (sanitized) that match a list of String
numbers
. -
#find_and_create_all_by_numbers(number_list, options = {}) ⇒ Object
Find all numbers and create if it doesn’t already exist
number_list
- a list of numbers (String, ActiveRecord or attribute hash)options
- additional create options and normal finder options like:conditions
=== Additional Options:create
-:new
(Default),:create
, or:create!
If the number does not exist, create it with this method. -
#find_and_create_by_number(number, options = {}) ⇒ Object
Find a number and create if it does not exist
options
include those specified in find_all_and_create_by_number. -
#find_by_number(number, options = {}) ⇒ Object
Find a single number.
-
#find_by_sms_email_address(address, options = {}) ⇒ Object
Return the phone number with specified carrier if the phone number is an sms email address.
-
#human_display(number) ⇒ Object
The human display pretty phone number (206) 555-5555.
Instance Method Details
#add_number_search(numbers, options = {}, merge_options = {}) ⇒ Object
adds a sanitize search for numbers for the options used for finders Use with find for other ActiveRecord objects
Outbound.find :all, add_number_search([1,2,3], :conditions => 'outbounds.send_priority == 1')
22 23 24 25 26 27 28 |
# File 'lib/sms_on_rails/model_support/phone_number.rb', line 22 def add_number_search(numbers, ={}, ={}) number_digits = [numbers].flatten number_digits.collect!{|n| digits(n) } unless [:skip_sanitize] number_conditions = ['number in (?)', number_digits.uniq.compact] [:conditions] = merge_conditions([:conditions]||{}, number_conditions) end |
#digits(text) ⇒ Object
The digits (numbers) only of the phone number Digits are how phone numbers are stored in the database The following all return 12065555555
SmsOnRails::digits(12065555555')
SmsOnRails::digits(206.555.5555')
SmsOnRails::digits(1206-555 5555')
105 106 107 108 109 110 |
# File 'lib/sms_on_rails/model_support/phone_number.rb', line 105 def digits(text) return text.digits if text.is_a?(self) number = text.to_s.gsub(/\D/,'') number = "1#{number}" if number.length == 10 number end |
#find_all_by_numbers(numbers, options = {}, merge_options = {}) ⇒ Object
Find all numbers (sanitized) that match a list of String numbers
31 32 33 34 |
# File 'lib/sms_on_rails/model_support/phone_number.rb', line 31 def find_all_by_numbers(numbers, ={}, ={}) return [] unless numbers.dup.delete_if{|x| x.blank? }.any? find(:all, add_number_search(numbers, , )) end |
#find_and_create_all_by_numbers(number_list, options = {}) ⇒ Object
Find all numbers and create if it doesn’t already exist number_list
- a list of numbers (String, ActiveRecord or attribute hash) options
- additional create options and normal finder options like :conditions
Additional Options
:create
- :new
(Default), :create
, or :create!
If the number does not exist, create it with this method. Using :new
means none of the objects are saved :keep_duplicates<tt> - When set to true, duplicates in the list are returned <tt>:skip_sort
- Default is to sort the list to be in the same order as number_list
. Set to false to skip sorting (perf boost). Instance creation or attribute update does not occur when skip_sort
is false.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sms_on_rails/model_support/phone_number.rb', line 53 def find_and_create_all_by_numbers(number_list, ={}) , = () # Collect a list of digits and a list of attributes attribute_list = [] number_digits = [number_list].flatten.inject([]) do |list, n| attribute_list << (new_attributes = value_to_hash(n)) digit = digits(new_attributes[:number]||new_attributes['number']) digit = new_attributes[:number]||new_attributes['number'] if digit.blank? list << digit list end found_numbers = find_all_by_numbers(number_digits, , :skip_sanitize => true) # sort the list based on the order of the original input # not found values have nil if [:skip_sort] found_numbers else sorted_numbers = sort_by_numbers(found_numbers, number_digits, ) transaction { update_attributes_on_list(sorted_numbers, attribute_list, ) } end end |
#find_and_create_by_number(number, options = {}) ⇒ Object
Find a number and create if it does not exist options
include those specified in find_all_and_create_by_number
80 81 82 |
# File 'lib/sms_on_rails/model_support/phone_number.rb', line 80 def find_and_create_by_number(number, ={}) find_and_create_all_by_numbers(number, .reverse_merge(:create => :new)).first end |
#find_by_number(number, options = {}) ⇒ Object
Find a single number
37 38 39 |
# File 'lib/sms_on_rails/model_support/phone_number.rb', line 37 def find_by_number(number, ={}) find_all_by_numbers([number], ).first; end |
#find_by_sms_email_address(address, options = {}) ⇒ Object
Return the phone number with specified carrier if the phone number is an sms email address
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sms_on_rails/model_support/phone_number.rb', line 86 def find_by_sms_email_address(address, ={}) number, carrier = reflections[:carrier].klass.carrier_from_sms_email(address) if number phone = find_by_number(number, )||new(:number => number) phone.carrier = carrier if carrier phone else nil end end |
#human_display(number) ⇒ Object
The human display pretty phone number (206) 555-5555
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sms_on_rails/model_support/phone_number.rb', line 114 def human_display(number) base_number = digits(number) if base_number.length == 11 && base_number.first == '1' "(#{base_number[1..3]}) #{base_number[4..6]}-#{base_number[7..10]}" elsif base_number.length > 0 "+#{base_number}" else nil end end |