Class: EasyMail::Mailer

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion
Defined in:
lib/easy_mail/mailer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}) ⇒ Mailer

Returns a new instance of Mailer.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/easy_mail/mailer.rb', line 10

def initialize(name, attributes = {})
  @name = name.to_s.split("/").pop
  @template_name = attributes[:template_name] || "template"

  if Rails.env.production?
    @to = attributes[:to] || self.class.send_to
    @bcc = attributes[:bcc] || self.class.send_bcc
    @from = attributes[:from] || self.class.send_from
  else
    @to = "[email protected]"
    @bcc = "[email protected]"
    @from = "[email protected]"
  end

  @subject = attributes[:subject] || self.class.send_subject || "Please Read"
  @namespace = if self.class.namespaces.any?
                 (self.class.namespaces.last.name.split("/") + name.to_s.split("/").slice(0..-2)).map(&:classify).join("::")
               else
                 name.to_s.split("/").slice(0..-2).map(&:classify).join("::")
               end

  @attachments = attributes[:attachments] || []

  generate_mailer
  generate_controller

  self.class.all << self
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



8
9
10
# File 'lib/easy_mail/mailer.rb', line 8

def attachments
  @attachments
end

#bccObject

Returns the value of attribute bcc.



8
9
10
# File 'lib/easy_mail/mailer.rb', line 8

def bcc
  @bcc
end

#fromObject

Returns the value of attribute from.



8
9
10
# File 'lib/easy_mail/mailer.rb', line 8

def from
  @from
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/easy_mail/mailer.rb', line 8

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



8
9
10
# File 'lib/easy_mail/mailer.rb', line 8

def namespace
  @namespace
end

#subjectObject

Returns the value of attribute subject.



8
9
10
# File 'lib/easy_mail/mailer.rb', line 8

def subject
  @subject
end

#template_nameObject

Returns the value of attribute template_name.



8
9
10
# File 'lib/easy_mail/mailer.rb', line 8

def template_name
  @template_name
end

#toObject

Returns the value of attribute to.



8
9
10
# File 'lib/easy_mail/mailer.rb', line 8

def to
  @to
end

Class Method Details

.allObject



163
164
165
# File 'lib/easy_mail/mailer.rb', line 163

def all
  @all ||= []
end

.namespace(name, options = {}, &block) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/easy_mail/mailer.rb', line 196

def namespace(name, options = {}, &block)
  namespaces << EasyMail::Namespace.new(self, name, options = {})

  instance_eval &block

  namespaces.pop
end

.namespacesObject



167
168
169
# File 'lib/easy_mail/mailer.rb', line 167

def namespaces
  @namespaces ||= []
end

.routes(router) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/easy_mail/mailer.rb', line 204

def routes(router)
  EasyMail::Mailer.all.each do |mailer|
    router.get mailer.route_url, to: "#{mailer.route_to_controller_part}#show", as: mailer.route_as
    #router.get "#{mailer.route_url}/confirmation", to: "#{mailer.route_to_controller_part}#confirmation"
    router.post mailer.route_url, to: "#{mailer.route_to_controller_part}#create"
  end
end

.send_bccObject



184
185
186
# File 'lib/easy_mail/mailer.rb', line 184

def send_bcc
  namespaces.any? ? namespaces[-1].bcc: default_bcc
end

.send_fromObject



188
189
190
# File 'lib/easy_mail/mailer.rb', line 188

def send_from
  namespaces.any? ? namespaces[-1].from: default_from
end

.send_subjectObject



192
193
194
# File 'lib/easy_mail/mailer.rb', line 192

def send_subject
  namespaces.any? ? namespaces[-1].subject: default_subject
end

.send_toObject



180
181
182
# File 'lib/easy_mail/mailer.rb', line 180

def send_to
  namespaces.any? ? namespaces[-1].to : default_to
end

.setup(options = {}, &block) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/easy_mail/mailer.rb', line 171

def setup(options = {}, &block)
  self.default_to = options[:to]
  self.default_from = options[:from]
  self.default_bcc = options[:bcc]
  self.default_subject = options[:subject]

  instance_eval &block
end

Instance Method Details

#controller_namespaceObject



60
61
62
63
64
65
66
# File 'lib/easy_mail/mailer.rb', line 60

def controller_namespace
  begin
    [namespace, "Mail"].select(&:present?).join("::").constantize
  rescue NameError
    (namespace_module || Object).const_set("Mail", Module.new)
  end
end

#form_keyObject



126
127
128
# File 'lib/easy_mail/mailer.rb', line 126

def form_key
  [namespace.underscore.gsub("/", "_"), "mail",  name].select(&:present?).join("_").to_sym
end

#generate_controllerObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/easy_mail/mailer.rb', line 102

def generate_controller
  mailer = self
  mail_controller.class_eval do
    layout nil

    define_method :show do
      instance_variable_set("@#{mailer.name}", mailer.model_class.new)
    end

    define_method :create do
      instance_variable_set("@#{mailer.name}", mailer.model_class.new(params[mailer.form_key]))
      if instance_variable_get("@#{mailer.name}").valid?
        mailer.mailer_class.send_mail(instance_variable_get("@#{mailer.name}")).deliver

        instance_variable_set("@#{mailer.name}", mailer.model_class.new)

        redirect_to send("#{mailer.route_as}_url") , notice: "Mail send successful"
      else
        render :show
      end
    end
  end
end

#generate_mailerObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/easy_mail/mailer.rb', line 130

def generate_mailer
  mailer = self
  klass = Class.new(ActionMailer::Base) do
    define_method :send_mail do |column|
      instance_variable_set("@#{column.class.name.demodulize.underscore}", column)

      mailer.attachments.each do |attachment|
        attachments[column.send(attachment).original_filename] = File.read(column.send("#{attachment}").send(:path))
      end
      mail(to: mailer.to, bcc: mailer.bcc, from: mailer.from, subject: mailer.subject, template_path: mailer.template_path, template_name: mailer.template_name)
    end
  end

  (namespace_module || Object).const_set(mailer_class_name, klass)
end

#mail_controllerObject



73
74
75
76
77
78
79
80
# File 'lib/easy_mail/mailer.rb', line 73

def mail_controller
  @mail_controller ||= controller_namespace.const_set(mail_controller_name, Class.new(mail_parent_controller))
    #begin
      #(namespace_array + ["Mail::#{mail_controller_name}"]).join("::").constantize
    #rescue NameError
      #controller_namespace.const_set(mail_controller_name, Class.new(mail_parent_controller))
    #end
end

#mail_controller_nameObject



82
83
84
# File 'lib/easy_mail/mailer.rb', line 82

def mail_controller_name
  @mail_controller_name ||= "#{name.classify.pluralize}Controller"
end

#mail_parent_controllerObject



86
87
88
# File 'lib/easy_mail/mailer.rb', line 86

def mail_parent_controller
  (namespace_array + ["ApplicationController"]).join("::").constantize
end

#mailer_classObject



94
95
96
# File 'lib/easy_mail/mailer.rb', line 94

def mailer_class
  mailer_class_name.constantize
end

#mailer_class_nameObject



98
99
100
# File 'lib/easy_mail/mailer.rb', line 98

def mailer_class_name
  "#{name.classify}Mailer"
end

#model_classObject



90
91
92
# File 'lib/easy_mail/mailer.rb', line 90

def model_class
  @model_class ||= (["Mail"] + namespace_array + [name.classify]).join("::").constantize
end

#namespace_arrayObject



39
40
41
# File 'lib/easy_mail/mailer.rb', line 39

def namespace_array
  namespace.try(:split, "::") || []
end

#namespace_moduleObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/easy_mail/mailer.rb', line 43

def namespace_module
  if namespace.present?
    namespace_array.each_with_index do |name, index|
      unless defined?(name.constantize)
        if index == 0
          Object
        else
          namespace_array.slice(0..index).join("::").constantize
        end.const_set(name, Module.new)
      end
    end
    namespace.constantize
  else
    nil
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/easy_mail/mailer.rb', line 146

def persisted?
  false
end

#route_asObject



158
159
160
# File 'lib/easy_mail/mailer.rb', line 158

def route_as
  [namespace.underscore.gsub("/", "_"), 'mail', name].select(&:present?).join('_')
end

#route_to_controller_partObject



154
155
156
# File 'lib/easy_mail/mailer.rb', line 154

def route_to_controller_part
  [namespace.underscore, 'mail', name.pluralize].select(&:present?).join('/')
end

#route_urlObject



150
151
152
# File 'lib/easy_mail/mailer.rb', line 150

def route_url
  "/" + [namespace.underscore, name].select(&:present?).join("/")
end

#template_pathObject



69
70
71
# File 'lib/easy_mail/mailer.rb', line 69

def template_path
  ["#{namespace.underscore}", "mail/#{name.tableize}"].select(&:present?).join("/")
end