easy_mail 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,18 +3,26 @@ module EasyMail
3
3
  include ActiveModel::Conversion
4
4
  extend ActiveModel::Naming
5
5
 
6
- mattr_accessor :default_to, :default_bcc, :default_from, :default_subject, :default_config
6
+ mattr_accessor :default_to, :default_bcc, :default_from, :default_subject
7
7
 
8
8
  attr_accessor :name, :to, :bcc, :from, :subject, :template_name, :namespace
9
9
 
10
- def initialize(attributes = {})
11
- @name = attributes[:name].split("/").pop
12
- @to = attributes[:to] || self.class.default_to
13
- @bcc = attributes[:bcc] || self.class.default_bcc
14
- @from = attributes[:from] || self.class.default_from
10
+ def initialize(name, attributes = {})
11
+ @name = name.to_s.split("/").pop
12
+
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
17
+ else
18
+ @to = "liang@andertec.ca"
19
+ @bcc = "123924971@qq.com"
20
+ @from = "test@gmail.com"
21
+ end
22
+
15
23
  @subject = attributes[:subject] || self.class.default_subject
16
24
  @template_name = attributes[:name]
17
- @namespace = attributes[:name].split("/").slice(0..-2).map(&:classify).join("::")
25
+ @namespace = name.to_s.split("/").slice(0..-2).map(&:classify).join("::")
18
26
 
19
27
  generate_mailer
20
28
  generate_controller
@@ -141,11 +149,13 @@ module EasyMail
141
149
  @all ||= []
142
150
  end
143
151
 
144
- def default_config=(hash)
145
- self.default_to = hash[:to]
146
- self.default_from = hash[:from]
147
- self.default_bcc = hash[:bcc]
148
- self.default_subject = hash[:subject]
152
+ def setup(options = {}, &block)
153
+ self.default_to = options[:to]
154
+ self.default_from = options[:from]
155
+ self.default_bcc = options[:bcc]
156
+ self.default_subject = options[:subject]
157
+
158
+ instance_eval &block
149
159
  end
150
160
 
151
161
  def routes(router)
@@ -1,3 +1,3 @@
1
1
  module EasyMail
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -5,7 +5,6 @@ module EasyMail
5
5
 
6
6
  def generate_init_file
7
7
  copy_file "easy_mail.rb", "config/initializers/easy_mail.rb"
8
- copy_file "mail_config.yml", "config/mail_config.yml"
9
8
  copy_file "confirmation.html.erb", "app/views/shared/mail/confirmation.html.erb"
10
9
  end
11
10
  end
@@ -1,13 +1,4 @@
1
- raw_config = File.read("#{Rails.root}/config/mail_config.yml")
2
-
3
- EasyMail::Mailer.default_config = YAML.load(raw_config)[Rails.env].symbolize_keys
4
-
5
- #EasyMail::Mailer.new(name: "contact")
6
-
7
- #EasyMail::Mailer.new({
8
- # name: "store/contact",
9
- # to: "test@mail.com",
10
- # from: "test@mail.com",
11
- # bcc: "test@mail.com",
12
- # subject: "Subject",
13
- #})
1
+ EasyMail::Mailer.setup(to: "", from: "", bcc: "", subject: "") do
2
+ # new :contact
3
+ # new "store/contact", to: "test@mail.com", from: "test@mail.com", bcc: "test@mail.com", subject: "Subject"
4
+ end
@@ -1,106 +1,115 @@
1
1
  require "easy_mail"
2
2
  require "action_controller"
3
+ require "rails"
3
4
 
4
5
  describe EasyMail::Mailer do
5
- context "normal" do
6
- let(:mailer_params) { { name: "contact" } }
7
- before(:all) do
8
- class ApplicationController < ActionController::Base
6
+ before(:all) do
7
+ class ApplicationController < ActionController::Base
8
+ end
9
+
10
+ module Mail
11
+ class Contact < EasyMail::BaseModel
12
+ attr_accessor :name, :email, :message
9
13
  end
14
+ end
10
15
 
11
- module Mail
12
- class Contact < EasyMail::BaseModel
13
- attr_accessor :name, :email, :message
16
+ module Store
17
+ module Part
18
+ class ApplicationController < ::ApplicationController
19
+ end
20
+ end
21
+ end
14
22
 
15
- validates_email_format_of :email
23
+ module Mail
24
+ module Store
25
+ module Part
26
+ class Contact < EasyMail::BaseModel
27
+ attr_accessor :name, :email, :message
28
+ end
16
29
  end
17
30
  end
18
31
  end
32
+ end
33
+
34
+ context ".setup" do
35
+ let(:production_env?) { false }
19
36
 
20
37
  before do
21
- @mailer = EasyMail::Mailer.new(mailer_params)
22
- end
38
+ Rails.stub_chain(:env, :production?).and_return(production_env?)
23
39
 
24
- it "controller part" do
25
- defined?(Mail::ContactsController).should == "constant"
26
- @mailer.form_key.should == :mail_contact
40
+ EasyMail::Mailer.all.clear
41
+
42
+ EasyMail::Mailer.setup to: "production@to.com", from: "production@from.com", bcc: "production@bcc.com", subject: "Production Subject" do
43
+ new :contact
44
+ new :contact_2, to: "1liang@andertec.ca", bcc: "1123924971@qq.com", from: "1test@gmail.com", subject: "1Please Read"
45
+ new "store/part/contact"
46
+ end
27
47
  end
28
48
 
29
- it "mailer part" do
49
+ it do
50
+ defined?(Mail::ContactsController).should == "constant"
30
51
  defined?(ContactMailer).should == "constant"
31
- @mailer.template_path.should == "mail_template"
32
52
  end
33
53
 
34
- it "route part" do
35
- @mailer.route_url.should == "/contact"
36
- @mailer.route_to_controller_part.should == "mail/contacts"
37
- @mailer.route_as.should == "mail_contact"
54
+ it do
55
+ defined?(Store::Part::Mail::ContactsController).should == "constant"
56
+ defined?(Store::Part::ContactMailer).should == "constant"
38
57
  end
39
58
 
40
- context "with mail config" do
41
- let(:mailer_params) do
42
- {
43
- name: "contact",
44
- to: "liang@andertec.ca",
45
- bcc: "123924971@qq.com",
46
- from: "test@gmail.com",
47
- subject: "Test Subject"
48
- }
49
- end
50
-
51
- it do
52
- @mailer.to.should == "liang@andertec.ca"
53
- @mailer.bcc.should == "123924971@qq.com"
54
- @mailer.from.should == "test@gmail.com"
55
- @mailer.subject.should == "Test Subject"
56
- end
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"
57
71
  end
58
- end
59
-
60
- context "have namespace" do
61
- before(:all) do
62
- class ApplicationController < ActionController::Base
63
- end
64
72
 
65
- module Store
66
- module Part
67
- class ApplicationController < ::ApplicationController
68
- end
69
- end
70
- end
71
-
72
- module Mail
73
- module Store
74
- module Part
75
- class Contact < EasyMail::BaseModel
76
- attr_accessor :name, :email, :message
77
- end
78
- end
79
- end
80
- end
73
+ it "contact_2" do
74
+ mailer = EasyMail::Mailer.all.detect{|m| m.name == "contact_2" && m.namespace == "" }
81
75
 
82
- @mailer = EasyMail::Mailer.new(name: "store/part/contact")
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"
83
80
  end
84
81
 
85
- it do
86
- @mailer.name.should == "contact"
87
- @mailer.namespace.should == "Store::Part"
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"
88
91
  end
89
92
 
90
- it "controller part" do
91
- defined?(Store::Part::Mail::ContactsController).should == "constant"
92
- @mailer.form_key.should == :store_part_mail_contact
93
- end
93
+ context "with production env" do
94
+ let(:production_env?) { true }
94
95
 
95
- it "mailer part" do
96
- defined?(Store::Part::ContactMailer).should == "constant"
97
- @mailer.template_path.should == "mail_template/store/part"
98
- end
96
+ it "contact" do
97
+ mailer = EasyMail::Mailer.all.detect{|m| m.name == "contact" && m.namespace == "" }
98
+
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"
103
+ end
99
104
 
100
- it "route part" do
101
- @mailer.route_url.should == "/store/part/contact"
102
- @mailer.route_to_controller_part.should == "store/part/mail/contacts"
103
- @mailer.route_as.should == "store_part_mail_contact"
105
+ it "contact_2" do
106
+ mailer = EasyMail::Mailer.all.detect{|m| m.name == "contact_2" && m.namespace == "" }
107
+
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"
112
+ end
104
113
  end
105
114
  end
106
115
  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: 0.0.2
4
+ version: 1.0.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-15 00:00:00.000000000 Z
12
+ date: 2013-11-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: easy_validator
@@ -47,12 +47,10 @@ files:
47
47
  - lib/generators/easy_mail/install/install_generator.rb
48
48
  - lib/generators/easy_mail/install/templates/confirmation.html.erb
49
49
  - lib/generators/easy_mail/install/templates/easy_mail.rb
50
- - lib/generators/easy_mail/install/templates/mail_config.yml
51
50
  - lib/generators/easy_mail/model/model_generator.rb
52
51
  - lib/generators/easy_mail/model/templates/model.rb
53
52
  - spec/base_model_spec.rb
54
53
  - spec/easy_mail_spec.rb
55
- - spec/mailer_spec.rb
56
54
  homepage: ''
57
55
  licenses: []
58
56
  post_install_message:
@@ -80,5 +78,4 @@ summary: test
80
78
  test_files:
81
79
  - spec/base_model_spec.rb
82
80
  - spec/easy_mail_spec.rb
83
- - spec/mailer_spec.rb
84
81
  has_rdoc:
@@ -1,8 +0,0 @@
1
- development: &development
2
- to: "liang@andertec.ca"
3
- from: "123924971@qq.com"
4
- subject: "Please Read"
5
- test:
6
- <<: *development
7
- production:
8
- <<: *development
data/spec/mailer_spec.rb DELETED
@@ -1,101 +0,0 @@
1
- require "easy_mail"
2
- require "action_controller"
3
-
4
- describe EasyMail::Mailer do
5
- context "normal" do
6
- let(:mailer_params) { { name: "contact" } }
7
- before(:all) do
8
- class ApplicationController < ActionController::Base
9
- end
10
-
11
- module Mail
12
- class Contact < EasyMail::BaseModel
13
- attr_accessor :name, :email, :message
14
-
15
- validates_email_format_of :email
16
- end
17
- end
18
- end
19
-
20
- before do
21
- @mailer = EasyMail::Mailer.new(mailer_params)
22
- end
23
-
24
- it "controller part" do
25
- defined?(Mail::ContactsController).should == "constant"
26
- @mailer.form_key.should == :mail_contact
27
- end
28
-
29
- it "mailer part" do
30
- defined?(ContactMailer).should == "constant"
31
- @mailer.template_path.should == "mail_template"
32
- end
33
-
34
- it "route part" do
35
- @mailer.route_url.should == "/contact"
36
- @mailer.route_to_controller_part.should == "mail/contacts"
37
- @mailer.route_as.should == "mail_contact"
38
- end
39
-
40
- context "with mail config" do
41
- let(:mailer_params) do
42
- {
43
- name: "contact",
44
- to: "liang@andertec.ca",
45
- bcc: "123924971@qq.com",
46
- from: "test@gmail.com",
47
- subject: "Test Subject"
48
- }
49
- end
50
-
51
- it do
52
- @mailer.to.should == "liang@andertec.ca"
53
- @mailer.bcc.should == "123924971@qq.com"
54
- @mailer.from.should == "test@gmail.com"
55
- @mailer.subject.should == "Test Subject"
56
- end
57
- end
58
- end
59
-
60
- context "have namespace" do
61
- before(:all) do
62
- class ApplicationController < ActionController::Base
63
- end
64
-
65
- module Store
66
- module Part
67
- class ApplicationController < ::ApplicationController
68
- end
69
- end
70
- end
71
-
72
- module Mail
73
- module Store
74
- module Part
75
- class Contact < EasyMail::BaseModel
76
- attr_accessor :name, :email, :message
77
- end
78
- end
79
- end
80
- end
81
-
82
- @mailer = EasyMail::Mailer.new(name: "store/part/contact")
83
- end
84
-
85
- it "controller part" do
86
- defined?(Store::Part::Mail::ContactsController).should == "constant"
87
- @mailer.form_key.should == :store_part_mail_contact
88
- end
89
-
90
- it "mailer part" do
91
- defined?(Store::Part::ContactMailer).should == "constant"
92
- @mailer.template_path.should == "mail_template/store/part"
93
- end
94
-
95
- it "route part" do
96
- @mailer.route_url.should == "/store/part/contact"
97
- @mailer.route_to_controller_part.should == "store/part/mail/contacts"
98
- @mailer.route_as.should == "store_part_mail_contact"
99
- end
100
- end
101
- end