Module: SmsOnRails::ModelSupport::Outbound::ClassMethods

Defined in:
lib/sms_on_rails/model_support/outbound.rb

Instance Method Summary collapse

Instance Method Details

#create_outbounds_for_phone_numbers(phone_numbers, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sms_on_rails/model_support/outbound.rb', line 52

def create_outbounds_for_phone_numbers(phone_numbers, options={})
  smses = reflections[:phone_number].klass.find_and_create_all_by_numbers(phone_numbers, (options[:find]||{}).reverse_merge(:create => :new)).inject([]) do |smses, phone|
    phone.attributes = options[:phone_number] if options[:phone_number]
    phone.carrier = options[:carrier] if options[:carrier]
    sms = self.new(options[:sms]||{})
    sms.phone_number = phone
    sms.service_provider = options[:service_provider] if options[:service_provider]
    smses << sms
    smses
  end
  smses
end

#create_sms(message, number, options = {}) ⇒ Object



42
43
44
45
# File 'lib/sms_on_rails/model_support/outbound.rb', line 42

def create_sms(message, number, options={})
  draft = reflections[:draft].klass.create_sms(message, number, options.reverse_merge(:keep_failed_outbounds => true))
  number.is_a?(Array) ? draft.outbounds : draft.outbounds.first
end

#create_sms!(message, number, options = {}) ⇒ Object



47
48
49
50
# File 'lib/sms_on_rails/model_support/outbound.rb', line 47

def create_sms!(message, number, options={})
  draft = reflections[:draft].klass.create_sms!(message, number, options)
  number.is_a?(Array) ? draft.outbounds : draft.outbounds.first
end

#create_with_phone(attributes, draft = nil) ⇒ Object

Create the object find the existing phone if already stored



66
67
68
69
70
71
72
73
74
# File 'lib/sms_on_rails/model_support/outbound.rb', line 66

def create_with_phone(attributes, draft=nil)
  outbound = new(attributes)
  transaction {
    outbound.assign_existing_phone
    outbound.draft = draft
    outbound.save
  }
  outbound
end

#send_immediately(message, phone_number, options = {}) ⇒ Object



34
35
36
# File 'lib/sms_on_rails/model_support/outbound.rb', line 34

def send_immediately(message, phone_number, options={})
  create_sms(message, phone_number, options.merge(:send_immediately => true))
end

#send_immediately!(message, phone_number, options = {}) ⇒ Object



38
39
40
# File 'lib/sms_on_rails/model_support/outbound.rb', line 38

def send_immediately!(message, phone_number, options={})
  create_sms!(message, phone_number, options.merge(:send_immediately => true))
end