Module: SmsOnRails::ModelSupport::Draft::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#create_draft(options = {}) ⇒ Object

Create the draft object options - A Draft object, a hash of attributes(can be nested) or a String with the draft message



79
80
81
82
83
84
85
86
87
# File 'lib/sms_on_rails/model_support/draft.rb', line 79

def create_draft(options={})
  if options.is_a?(ActiveRecord::Base)
    options.dup
  elsif options.is_a?(Hash)
    new(options)
  elsif options.is_a?(String)
    new(:message => options)
  end
end

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

Create a new sms draft message - can either be a string, an attribute hash (nested ok) or and ActiveRecord phone_numbers - array or single text phone number or phone number . If message is

a nested attributes (Rails 2.3), +phone_numbers+ and +options+ should be blank

Options

:send_immediately - Send all outbounds now :draft - a hash of attributes for the draft object :phone_number - a hash of attributes applied to all phone_numbers :outbound - a hash of attributes applied to all generated Outbound instances :keep_failed_outbounds - typically outbound messages are destroyed if they fail during delivery :deliver - options to pass to the deliver method if :send_immediately is used

Example

SmsOnRails::Draft.create_sms(‘my_message’, ‘9995556667’) SmsOnRails::Draft.create_sms(‘my_message’, [‘2065556666’, ‘9995556667’])

SmsOnRails::Draft.create_sms(params) # assume nested with :outbound_attributes SmsOnRails::Draft.create_sms(params, params, :send_immediately => true



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sms_on_rails/model_support/draft.rb', line 45

def create_sms(message, phone_numbers=nil, options={})

  draft = create_draft(message)
  draft.attributes = options[:draft] if options[:draft]
  
  # Update draft with any existing phone numbers
  draft.outbounds.each {|o| o.assign_existing_phone } if draft.outbounds

  # Generate new phone_numbers
  draft.create_outbounds_for_phone_numbers(phone_numbers, options) if phone_numbers

  if draft.send(options[:bang] ? :save! : :save) && options[:send_immediately]
    if !draft.send(options[:bang] ? :deliver! : :deliver, options[:deliver])
      # this is really crappy but when we are sending multiple messages
      # locking has to actually create the object.
      # so if we fail try to delete
      # this could be terribly slow if there are a lot of outbounds
      if options[:keep_failed_outbounds]
        draft.outbounds.each{|o| o.update_attribute(:sms_draft_id, nil ) }
      else
        draft.outbounds.each{|o| o.destroy }
        draft.outbounds = []
      end
    end
  end
  draft
end

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



73
74
75
# File 'lib/sms_on_rails/model_support/draft.rb', line 73

def create_sms!(message, phone_numbers=nil, options={})
  create_sms(message, phone_numbers, options.reverse_merge(:bang => true, :create => :create!))
end