Class: Email

Inherits:
Record show all
Defined in:
lib/yodel/models/email/email.rb

Constant Summary

Constants inherited from AbstractRecord

AbstractRecord::CALLBACKS, AbstractRecord::FIELD_CALLBACKS, AbstractRecord::ORDERS

Instance Attribute Summary

Attributes inherited from Record

#mixins, #model, #model_record, #real_record

Attributes inherited from SiteRecord

#site

Attributes inherited from AbstractRecord

#changed, #errors, #stash, #typecast, #values

Instance Method Summary collapse

Methods inherited from Record

#all_children, #append_to_siblings, #children_and_self, #collection, #create_eigenmodel, #create_mixin_instances, #default_values, #delegate_mixins, #destroy_children, #field_sections, #fields, #first_non_blank_response_to, #first_parent, #first_response_to, #has_eigenmodel?, #initialize, #insert_in_siblings, #inspect_hash, #load_model, #model_name, #parent?, #parents, #perform_reload, #prepare_reload_params, #remove_eigenmodel, #remove_from_siblings, #root?, #run_record_after_create_callbacks, #run_record_after_destroy_callbacks, #run_record_after_save_callbacks, #run_record_after_update_callbacks, #run_record_after_validation_callbacks, #run_record_before_create_callbacks, #run_record_before_destroy_callbacks, #run_record_before_save_callbacks, #run_record_before_update_callbacks, #run_record_before_validation_callbacks, #siblings, #to_str, #update_search_keywords, #user_allowed_to?, #user_allowed_to_create?, #user_allowed_to_delete?, #user_allowed_to_update?, #user_allowed_to_view?

Methods inherited from SiteRecord

#default_values, #initialize, #inspect_hash, #perform_reload, #prepare_reload_params, #site_id

Methods included from SiteModel

#load, #scoped, #scoped_for

Methods included from MongoModel

#collection, #load, #scoped

Methods included from AbstractModel

#embed_many, #embed_one, #field, #fields, #many, #modify_field, #one, #remove_field

Methods inherited from MongoRecord

#collection, #default_values, #fields, #id, #increment!, #inspect_hash, #load_from_mongo, #load_mongo_document, #perform_destroy, #perform_reload, #perform_save, #set_id

Methods inherited from AbstractRecord

#changed!, #changed?, #clear_key, #default_values, #destroy, #destroyed?, #eql?, #errors?, #field, #field?, #field_was, #fields, #from_json, #get, #get_meta, #get_raw, #hash, #id, #increment!, inherited, #initialize, #inspect, #inspect_hash, #inspect_value, #method_missing, #new?, #prepare_reload_params, #present?, #reload, #save, #save_without_validation, #search_terms, #set, #set_meta, #set_raw, #to_json, #to_str, #trigger_field_callback, #update, #valid?

Constructor Details

This class inherits a constructor from Record

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AbstractRecord

Instance Method Details

#contentObject



64
65
66
# File 'lib/yodel/models/email/email.rb', line 64

def content
  @content
end

#deliver(options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/yodel/models/email/email.rb', line 3

def deliver(options)
  # we don't store complete records in the queue task, only IDs of records
  cleaned = {_id: self.id}
  options.each do |key, value|
    if value.is_a?(Record)
      value = {_id: value.id}
    end
    cleaned[key] = value
  end
  
  Task.add_task(:deliver_email, cleaned, site)
end

#get_bindingObject



72
73
74
# File 'lib/yodel/models/email/email.rb', line 72

def get_binding
  @binding
end

#perform_delivery(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/yodel/models/email/email.rb', line 16

def perform_delivery(options)
  mail = Mail.new
  
  # retrieve records that were replaced with their ID as above
  options.each do |key, value|
    if value.is_a?(Hash) && value.key?('_id')
      options[key] = site.records.find(value['_id'])
    end
  end
  
  # email headers
  %w{to from cc bcc subject}.each do |param|
    options[param] ||= options[param.to_sym] || self.send(param)
    mail.send("#{param}=", options[param])
  end
  render_binding = binding
  
  # rendered text body
  unless text_body.blank?
    text_body = self.text_body
    text_part = Mail::Part.new do
      body Ember::Template.new(text_body).render(render_binding)
    end
    mail.text_part = text_part
  end
  
  # rendered html body
  unless html_body.blank?
    html_body = self.html_body
    # FIXME: reloading should be done elsewhere, not a concern of email
    #Layout.reload_layouts(site) if Yodel.env.development?        
    if self.html_layout && layout = site.layouts.where(name: self.html_layout, mime_type: :html).first
      @content = Ember::Template.new(html_body).render(render_binding)
      @binding = render_binding
      html_content = layout.render(self)
    else
      html_content = Ember::Template.new(html_body).render(render_binding)
    end
    html_part = Mail::Part.new do
      content_type 'text/html; charset=UTF-8'
      body html_content
    end
    mail.html_part = html_part
  end
  
  mail.deliver!      
end

#set_binding(binding) ⇒ Object



76
77
78
# File 'lib/yodel/models/email/email.rb', line 76

def set_binding(binding)
  @binding = binding
end

#set_content(content) ⇒ Object



68
69
70
# File 'lib/yodel/models/email/email.rb', line 68

def set_content(content)
  @content = content
end