Module: SmsOnRails::ModelSupport::Draft::ClassMethods
- Defined in:
- lib/sms_on_rails/model_support/draft.rb
Instance Method Summary collapse
-
#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. -
#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 ActiveRecordphone_numbers
- array or single text phone number or phone number . - #create_sms!(message, phone_numbers = nil, options = {}) ⇒ Object
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(={}) if .is_a?(ActiveRecord::Base) .dup elsif .is_a?(Hash) new() elsif .is_a?(String) new(:message => ) 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(, phone_numbers=nil, ={}) draft = create_draft() draft.attributes = [:draft] if [: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, ) if phone_numbers if draft.send([:bang] ? :save! : :save) && [:send_immediately] if !draft.send([:bang] ? :deliver! : :deliver, [: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 [: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!(, phone_numbers=nil, ={}) create_sms(, phone_numbers, .reverse_merge(:bang => true, :create => :create!)) end |