easy_mail 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,18 +11,22 @@ module EasyMail
11
11
  @name = name.to_s.split("/").pop
12
12
 
13
13
  if Rails.env.production?
14
- @to = attributes[:to] || self.class.default_to
15
- @bcc = attributes[:bcc] || self.class.default_bcc
16
- @from = attributes[:from] || self.class.default_from
14
+ @to = attributes[:to] || self.class.send_to
15
+ @bcc = attributes[:bcc] || self.class.send_bcc
16
+ @from = attributes[:from] || self.class.send_from
17
17
  else
18
18
  @to = "liang@andertec.ca"
19
19
  @bcc = "123924971@qq.com"
20
20
  @from = "test@gmail.com"
21
21
  end
22
22
 
23
- @subject = attributes[:subject] || self.class.default_subject
23
+ @subject = attributes[:subject] || self.class.send_subject || "Please Read"
24
24
  @template_name = attributes[:name]
25
- @namespace = name.to_s.split("/").slice(0..-2).map(&:classify).join("::")
25
+ @namespace = if self.class.namespaces.any?
26
+ (self.class.namespaces.last.name.split("/") + name.to_s.split("/").slice(0..-2)).map(&:classify).join("::")
27
+ else
28
+ name.to_s.split("/").slice(0..-2).map(&:classify).join("::")
29
+ end
26
30
 
27
31
  generate_mailer
28
32
  generate_controller
@@ -149,6 +153,10 @@ module EasyMail
149
153
  @all ||= []
150
154
  end
151
155
 
156
+ def namespaces
157
+ @namespaces ||= []
158
+ end
159
+
152
160
  def setup(options = {}, &block)
153
161
  self.default_to = options[:to]
154
162
  self.default_from = options[:from]
@@ -158,6 +166,30 @@ module EasyMail
158
166
  instance_eval &block
159
167
  end
160
168
 
169
+ def send_to
170
+ namespaces.any? ? namespaces[-1].to : default_to
171
+ end
172
+
173
+ def send_bcc
174
+ namespaces.any? ? namespaces[-1].bcc: default_bcc
175
+ end
176
+
177
+ def send_from
178
+ namespaces.any? ? namespaces[-1].from: default_from
179
+ end
180
+
181
+ def send_subject
182
+ namespaces.any? ? namespaces[-1].subject: default_subject
183
+ end
184
+
185
+ def namespace(name, options = {}, &block)
186
+ namespaces << EasyMail::Namespace.new(self, name, options = {})
187
+
188
+ instance_eval &block
189
+
190
+ namespaces.pop
191
+ end
192
+
161
193
  def routes(router)
162
194
  EasyMail::Mailer.all.each do |mailer|
163
195
  router.get mailer.route_url, to: "#{mailer.route_to_controller_part}#show", as: mailer.route_as
@@ -0,0 +1,23 @@
1
+ module EasyMail
2
+ class Namespace
3
+ include ActiveModel::Conversion
4
+ extend ActiveModel::Naming
5
+
6
+ attr_accessor :name, :to, :bcc, :from, :subject
7
+
8
+ def initialize(mailer, name, attributes = {})
9
+ @name = (mailer.namespaces.map(&:name) + [name.to_s]).join("/")
10
+ if mailer.namespaces.any?
11
+ @to = attributes[:to] || mailer.namespaces[-1].to
12
+ @bcc = attributes[:bcc] || mailer.namespaces[-1].bcc
13
+ @from = attributes[:from] || mailer.namespaces[-1].from
14
+ @subject = attributes[:subject] || mailer.namespaces[-1].subject
15
+ else
16
+ @to = attributes[:to] || mailer.default_to
17
+ @bcc= attributes[:bcc] || mailer.default_bcc
18
+ @from = attributes[:from] || mailer.default_from
19
+ @subject = attributes[:subject] || mailer.default_subject
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module EasyMail
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/easy_mail.rb CHANGED
@@ -8,6 +8,7 @@ require 'action_mailer'
8
8
  require "easy_validator"
9
9
 
10
10
  require "easy_mail/mailer"
11
+ require "easy_mail/namespace"
11
12
  require "easy_mail/base_model"
12
13
  require "easy_mail/version"
13
14
 
@@ -13,16 +13,19 @@ describe EasyMail::Mailer do
13
13
  end
14
14
  end
15
15
 
16
- module Store
17
- module Part
16
+ module NamespaceOne
17
+ class ApplicationController < ::ApplicationController
18
+ end
19
+
20
+ module NamespaceTwo
18
21
  class ApplicationController < ::ApplicationController
19
22
  end
20
23
  end
21
24
  end
22
25
 
23
26
  module Mail
24
- module Store
25
- module Part
27
+ module NamespaceOne
28
+ module NamespaceTwo
26
29
  class Contact < EasyMail::BaseModel
27
30
  attr_accessor :name, :email, :message
28
31
  end
@@ -42,73 +45,132 @@ describe EasyMail::Mailer do
42
45
  EasyMail::Mailer.setup to: "production@to.com", from: "production@from.com", bcc: "production@bcc.com", subject: "Production Subject" do
43
46
  new :contact
44
47
  new :contact_2, to: "1liang@andertec.ca", bcc: "1123924971@qq.com", from: "1test@gmail.com", subject: "1Please Read"
45
- new "store/part/contact"
48
+ new "namespace_one/namespace_two/contact"
49
+
50
+ namespace :namespace_one do
51
+ new :contact
52
+
53
+ new "namespace_two/contact_3"
54
+
55
+ namespace :namespace_two do
56
+ new :contact_2
57
+ end
58
+ end
46
59
  end
47
60
  end
48
61
 
49
62
  it do
50
- defined?(Mail::ContactsController).should == "constant"
51
- defined?(ContactMailer).should == "constant"
63
+ EasyMail::Mailer.all.select{|m| m.name == "contact" && m.namespace == ""}.count.should == 1
64
+ EasyMail::Mailer.all.select{|m| m.name == "contact_2" && m.namespace == ""}.count.should == 1
65
+
66
+ EasyMail::Mailer.all.select{|m| m.name == "contact" && m.namespace == "NamespaceOne"}.count.should == 1
67
+
68
+ EasyMail::Mailer.all.select{|m| m.name == "contact" && m.namespace == "NamespaceOne::NamespaceTwo"}.count.should == 1
69
+ EasyMail::Mailer.all.select{|m| m.name == "contact_2" && m.namespace == "NamespaceOne::NamespaceTwo"}.count.should == 1
70
+ EasyMail::Mailer.all.select{|m| m.name == "contact_3" && m.namespace == "NamespaceOne::NamespaceTwo"}.count.should == 1
52
71
  end
53
72
 
54
- it do
55
- defined?(Store::Part::Mail::ContactsController).should == "constant"
56
- defined?(Store::Part::ContactMailer).should == "constant"
73
+
57
74
  end
58
75
 
59
- it "contact" do
60
- mailer = EasyMail::Mailer.all.detect{|m| m.name == "contact" && m.namespace == "" }
61
- mailer.form_key.should == :mail_contact
62
- mailer.template_path.should == "mail_template"
63
- mailer.route_url.should == "/contact"
64
- mailer.route_to_controller_part.should == "mail/contacts"
65
- mailer.route_as.should == "mail_contact"
66
-
67
- mailer.to.should == "liang@andertec.ca"
68
- mailer.bcc.should == "123924971@qq.com"
69
- mailer.from.should == "test@gmail.com"
70
- mailer.subject.should == "Production Subject"
76
+ describe ".new" do
77
+ let(:production_env?) { false }
78
+ let(:mailer_params) { {} }
79
+
80
+ before do
81
+ Rails.stub_chain(:env, :production?).and_return(production_env?)
82
+
83
+ EasyMail::Mailer.stub(:default_to).and_return("default_to@gmail.com")
84
+ EasyMail::Mailer.stub(:default_bcc).and_return("default_bcc@gmail.com")
85
+ EasyMail::Mailer.stub(:default_from).and_return("default_from@gmail.com")
86
+ EasyMail::Mailer.stub(:default_subject).and_return("Default Subject")
71
87
  end
72
88
 
73
- it "contact_2" do
74
- mailer = EasyMail::Mailer.all.detect{|m| m.name == "contact_2" && m.namespace == "" }
89
+ it do
90
+ @mailer = EasyMail::Mailer.new(:contact, mailer_params)
91
+ @mailer.to.should == "liang@andertec.ca"
92
+ @mailer.bcc.should == "123924971@qq.com"
93
+ @mailer.from.should == "test@gmail.com"
94
+ @mailer.subject.should == "Default Subject"
95
+ @mailer.namespace.should == ""
96
+
97
+ @mailer.form_key.should == :mail_contact
98
+ @mailer.template_path.should == "mail_template"
99
+ @mailer.route_url.should == "/contact"
100
+ @mailer.route_to_controller_part.should == "mail/contacts"
101
+ @mailer.route_as.should == "mail_contact"
75
102
 
76
- mailer.to.should == "liang@andertec.ca"
77
- mailer.bcc.should == "123924971@qq.com"
78
- mailer.from.should == "test@gmail.com"
79
- mailer.subject.should == "1Please Read"
103
+ defined?(Mail::ContactsController).should == "constant"
104
+ defined?(ContactMailer).should == "constant"
80
105
  end
81
106
 
82
- it "store/part/contact" do
83
- mailer = EasyMail::Mailer.all.detect{|m| m.name == "contact" && m.namespace == "Store::Part" }
84
- mailer.name.should == "contact"
85
- mailer.namespace.should == "Store::Part"
86
- mailer.form_key.should == :store_part_mail_contact
87
- mailer.template_path.should == "mail_template/store/part"
88
- mailer.route_url.should == "/store/part/contact"
89
- mailer.route_to_controller_part.should == "store/part/mail/contacts"
90
- mailer.route_as.should == "store_part_mail_contact"
107
+ context "with have mailer_params" do
108
+ let(:mailer_params) { { to: "to@gmail.com", bcc: "bcc@gmail.com", from: "from@gamil.com", subject: "Subject 1" } }
109
+
110
+ it do
111
+ @mailer = EasyMail::Mailer.new(:contact, mailer_params)
112
+ @mailer.to.should == "liang@andertec.ca"
113
+ @mailer.bcc.should == "123924971@qq.com"
114
+ @mailer.from.should == "test@gmail.com"
115
+ @mailer.subject.should == "Subject 1"
116
+ @mailer.namespace.should == ""
117
+ end
91
118
  end
92
119
 
93
120
  context "with production env" do
94
121
  let(:production_env?) { true }
95
122
 
96
- it "contact" do
97
- mailer = EasyMail::Mailer.all.detect{|m| m.name == "contact" && m.namespace == "" }
98
123
 
99
- mailer.to.should == "production@to.com"
100
- mailer.bcc.should == "production@bcc.com"
101
- mailer.from.should == "production@from.com"
102
- mailer.subject.should == "Production Subject"
124
+ it do
125
+ @mailer = EasyMail::Mailer.new(:contact, mailer_params)
126
+ @mailer.to.should == "default_to@gmail.com"
127
+ @mailer.bcc.should == "default_bcc@gmail.com"
128
+ @mailer.from.should == "default_from@gmail.com"
129
+ @mailer.subject.should == "Default Subject"
130
+ @mailer.namespace.should == ""
103
131
  end
104
132
 
105
- it "contact_2" do
106
- mailer = EasyMail::Mailer.all.detect{|m| m.name == "contact_2" && m.namespace == "" }
133
+ context "with namespace" do
134
+ let(:namespace_params) { {} }
135
+
136
+ before do
137
+ EasyMail::Mailer.namespaces.clear
107
138
 
108
- mailer.to.should == "1liang@andertec.ca"
109
- mailer.bcc.should == "1123924971@qq.com"
110
- mailer.from.should == "1test@gmail.com"
111
- mailer.subject.should == "1Please Read"
139
+ EasyMail::Mailer.namespaces << EasyMail::Namespace.new(EasyMail::Mailer, :namespace_one)
140
+ EasyMail::Mailer.namespaces << EasyMail::Namespace.new(EasyMail::Mailer, :namespace_two, namespace_params)
141
+ end
142
+
143
+ it do
144
+ @mailer = EasyMail::Mailer.new(:contact, mailer_params)
145
+ @mailer.to.should == "default_to@gmail.com"
146
+ @mailer.bcc.should == "default_bcc@gmail.com"
147
+ @mailer.from.should == "default_from@gmail.com"
148
+ @mailer.subject.should == "Default Subject"
149
+ @mailer.namespace.should == "NamespaceOne::NamespaceTwo"
150
+
151
+ @mailer.name.should == "contact"
152
+ @mailer.form_key.should == :namespace_one_namespace_two_mail_contact
153
+ @mailer.template_path.should == "mail_template/namespace_one/namespace_two"
154
+ @mailer.route_url.should == "/namespace_one/namespace_two/contact"
155
+ @mailer.route_to_controller_part.should == "namespace_one/namespace_two/mail/contacts"
156
+ @mailer.route_as.should == "namespace_one_namespace_two_mail_contact"
157
+
158
+ defined?(NamespaceOne::NamespaceTwo::Mail::ContactsController).should == "constant"
159
+ defined?(NamespaceOne::NamespaceTwo::ContactMailer).should == "constant"
160
+ end
161
+
162
+ context "with have attributes" do
163
+ let(:namespace_params) { { to: "n2_to@gmail.com", from: "n2_from@gmail.com", bcc: "n2_bcc@gmail.com", subject: "N2 Subject"} }
164
+
165
+ it do
166
+ @mailer = EasyMail::Mailer.new(:contact, mailer_params)
167
+ @mailer.to.should == "n2_to@gmail.com"
168
+ @mailer.bcc.should == "n2_bcc@gmail.com"
169
+ @mailer.from.should == "n2_from@gmail.com"
170
+ @mailer.subject.should == "N2 Subject"
171
+ @mailer.namespace.should == "NamespaceOne::NamespaceTwo"
172
+ end
173
+ end
112
174
  end
113
175
  end
114
176
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_mail
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-20 00:00:00.000000000 Z
12
+ date: 2013-11-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: easy_validator
@@ -43,6 +43,7 @@ files:
43
43
  - lib/easy_mail.rb
44
44
  - lib/easy_mail/base_model.rb
45
45
  - lib/easy_mail/mailer.rb
46
+ - lib/easy_mail/namespace.rb
46
47
  - lib/easy_mail/version.rb
47
48
  - lib/generators/easy_mail/install/install_generator.rb
48
49
  - lib/generators/easy_mail/install/templates/confirmation.html.erb