Class: EasyMail::Mailer
- Inherits:
-
Object
- Object
- EasyMail::Mailer
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion
- Defined in:
- lib/easy_mail/mailer.rb
Instance Attribute Summary collapse
-
#attachments ⇒ Object
Returns the value of attribute attachments.
-
#bcc ⇒ Object
Returns the value of attribute bcc.
-
#from ⇒ Object
Returns the value of attribute from.
-
#name ⇒ Object
Returns the value of attribute name.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#template_name ⇒ Object
Returns the value of attribute template_name.
-
#to ⇒ Object
Returns the value of attribute to.
Class Method Summary collapse
- .all ⇒ Object
- .namespace(name, options = {}, &block) ⇒ Object
- .namespaces ⇒ Object
- .routes(router) ⇒ Object
- .send_bcc ⇒ Object
- .send_from ⇒ Object
- .send_subject ⇒ Object
- .send_to ⇒ Object
- .setup(options = {}, &block) ⇒ Object
Instance Method Summary collapse
- #controller_namespace ⇒ Object
- #form_key ⇒ Object
- #generate_controller ⇒ Object
- #generate_mailer ⇒ Object
-
#initialize(name, attributes = {}) ⇒ Mailer
constructor
A new instance of Mailer.
- #mail_controller ⇒ Object
- #mail_controller_name ⇒ Object
- #mail_parent_controller ⇒ Object
- #mailer_class ⇒ Object
- #mailer_class_name ⇒ Object
- #model_class ⇒ Object
- #namespace_array ⇒ Object
- #namespace_module ⇒ Object
- #persisted? ⇒ Boolean
- #route_as ⇒ Object
- #route_to_controller_part ⇒ Object
- #route_url ⇒ Object
- #template_path ⇒ Object
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 = attributes[:attachments] || [] generate_mailer generate_controller self.class.all << self end |
Instance Attribute Details
#attachments ⇒ Object
Returns the value of attribute attachments.
8 9 10 |
# File 'lib/easy_mail/mailer.rb', line 8 def end |
#bcc ⇒ Object
Returns the value of attribute bcc.
8 9 10 |
# File 'lib/easy_mail/mailer.rb', line 8 def bcc @bcc end |
#from ⇒ Object
Returns the value of attribute from.
8 9 10 |
# File 'lib/easy_mail/mailer.rb', line 8 def from @from end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/easy_mail/mailer.rb', line 8 def name @name end |
#namespace ⇒ Object
Returns the value of attribute namespace.
8 9 10 |
# File 'lib/easy_mail/mailer.rb', line 8 def namespace @namespace end |
#subject ⇒ Object
Returns the value of attribute subject.
8 9 10 |
# File 'lib/easy_mail/mailer.rb', line 8 def subject @subject end |
#template_name ⇒ Object
Returns the value of attribute template_name.
8 9 10 |
# File 'lib/easy_mail/mailer.rb', line 8 def template_name @template_name end |
#to ⇒ Object
Returns the value of attribute to.
8 9 10 |
# File 'lib/easy_mail/mailer.rb', line 8 def to @to end |
Class Method Details
.all ⇒ Object
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, = {}, &block) namespaces << EasyMail::Namespace.new(self, name, = {}) instance_eval &block namespaces.pop end |
.namespaces ⇒ Object
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_bcc ⇒ Object
184 185 186 |
# File 'lib/easy_mail/mailer.rb', line 184 def send_bcc namespaces.any? ? namespaces[-1].bcc: default_bcc end |
.send_from ⇒ Object
188 189 190 |
# File 'lib/easy_mail/mailer.rb', line 188 def send_from namespaces.any? ? namespaces[-1].from: default_from end |
.send_subject ⇒ Object
192 193 194 |
# File 'lib/easy_mail/mailer.rb', line 192 def send_subject namespaces.any? ? namespaces[-1].subject: default_subject end |
.send_to ⇒ Object
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( = {}, &block) self.default_to = [:to] self.default_from = [:from] self.default_bcc = [:bcc] self.default_subject = [:subject] instance_eval &block end |
Instance Method Details
#controller_namespace ⇒ Object
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_key ⇒ Object
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_controller ⇒ Object
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_mailer ⇒ Object
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..each do || [column.send().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_controller ⇒ Object
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_name ⇒ Object
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_controller ⇒ Object
86 87 88 |
# File 'lib/easy_mail/mailer.rb', line 86 def mail_parent_controller (namespace_array + ["ApplicationController"]).join("::").constantize end |
#mailer_class ⇒ Object
94 95 96 |
# File 'lib/easy_mail/mailer.rb', line 94 def mailer_class mailer_class_name.constantize end |
#mailer_class_name ⇒ Object
98 99 100 |
# File 'lib/easy_mail/mailer.rb', line 98 def mailer_class_name "#{name.classify}Mailer" end |
#model_class ⇒ Object
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_array ⇒ Object
39 40 41 |
# File 'lib/easy_mail/mailer.rb', line 39 def namespace_array namespace.try(:split, "::") || [] end |
#namespace_module ⇒ Object
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
146 147 148 |
# File 'lib/easy_mail/mailer.rb', line 146 def persisted? false end |
#route_as ⇒ Object
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_part ⇒ Object
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_url ⇒ Object
150 151 152 |
# File 'lib/easy_mail/mailer.rb', line 150 def route_url "/" + [namespace.underscore, name].select(&:present?).join("/") end |
#template_path ⇒ Object
69 70 71 |
# File 'lib/easy_mail/mailer.rb', line 69 def template_path ["#{namespace.underscore}", "mail/#{name.tableize}"].select(&:present?).join("/") end |