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
-
#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
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 |
# File 'lib/easy_mail/mailer.rb', line 10 def initialize(name, attributes = {}) @name = name.to_s.split("/").pop if Rails.env.production? @to = attributes[:to] || self.class.default_to @bcc = attributes[:bcc] || self.class.default_bcc @from = attributes[:from] || self.class.default_from else @to = "[email protected]" @bcc = "[email protected]" @from = "[email protected]" end @subject = attributes[:subject] || self.class.default_subject @template_name = attributes[:name] @namespace = name.to_s.split("/").slice(0..-2).map(&:classify).join("::") generate_mailer generate_controller self.class.all << self end |
Instance Attribute Details
#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
148 149 150 |
# File 'lib/easy_mail/mailer.rb', line 148 def all @all ||= [] end |
.routes(router) ⇒ Object
161 162 163 164 165 166 |
# File 'lib/easy_mail/mailer.rb', line 161 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.post mailer.route_url, to: "#{mailer.route_to_controller_part}#create" end end |
.setup(options = {}, &block) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/easy_mail/mailer.rb', line 152 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
54 55 56 57 58 59 60 |
# File 'lib/easy_mail/mailer.rb', line 54 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
114 115 116 |
# File 'lib/easy_mail/mailer.rb', line 114 def form_key [namespace.underscore.gsub("/", "_"), "mail", name].select(&:present?).join("_").to_sym end |
#generate_controller ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/easy_mail/mailer.rb', line 95 def generate_controller mailer = self mail_controller.class_eval do 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 render "shared/mail/confirmation" else render :show end end end end |
#generate_mailer ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/easy_mail/mailer.rb', line 118 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) 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
67 68 69 70 71 72 73 |
# File 'lib/easy_mail/mailer.rb', line 67 def mail_controller @mail_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
75 76 77 |
# File 'lib/easy_mail/mailer.rb', line 75 def mail_controller_name @mail_controller_name ||= "#{name.classify.pluralize}Controller" end |
#mail_parent_controller ⇒ Object
79 80 81 |
# File 'lib/easy_mail/mailer.rb', line 79 def mail_parent_controller (namespace_array + ["ApplicationController"]).join("::").constantize end |
#mailer_class ⇒ Object
87 88 89 |
# File 'lib/easy_mail/mailer.rb', line 87 def mailer_class mailer_class_name.constantize end |
#mailer_class_name ⇒ Object
91 92 93 |
# File 'lib/easy_mail/mailer.rb', line 91 def mailer_class_name "#{name.classify}Mailer" end |
#model_class ⇒ Object
83 84 85 |
# File 'lib/easy_mail/mailer.rb', line 83 def model_class @model_class ||= (["Mail"] + namespace_array + [name.classify]).join("::").constantize end |
#namespace_array ⇒ Object
33 34 35 |
# File 'lib/easy_mail/mailer.rb', line 33 def namespace_array namespace.try(:split, "::") || [] end |
#namespace_module ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/easy_mail/mailer.rb', line 37 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
131 132 133 |
# File 'lib/easy_mail/mailer.rb', line 131 def persisted? false end |
#route_as ⇒ Object
143 144 145 |
# File 'lib/easy_mail/mailer.rb', line 143 def route_as [namespace.underscore.gsub("/", "_"), 'mail', name].select(&:present?).join('_') end |
#route_to_controller_part ⇒ Object
139 140 141 |
# File 'lib/easy_mail/mailer.rb', line 139 def route_to_controller_part [namespace.underscore, 'mail', name.pluralize].select(&:present?).join('/') end |
#route_url ⇒ Object
135 136 137 |
# File 'lib/easy_mail/mailer.rb', line 135 def route_url "/" + [namespace.underscore, name].select(&:present?).join("/") end |
#template_path ⇒ Object
63 64 65 |
# File 'lib/easy_mail/mailer.rb', line 63 def template_path ["mail_template", "#{namespace.underscore}"].select(&:present?).join("/") end |