easy_mail 0.0.2

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_mail.gemspec
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 liangmincong
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # EasyMail
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'easy_mail'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install easy_mail
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/easy_mail.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_mail/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "easy_mail"
8
+ gem.version = EasyMail::VERSION
9
+ gem.authors = ["liangmincong"]
10
+ gem.email = ["iceskylmc@gmail.com"]
11
+ gem.description = "test"
12
+ gem.summary = "test"
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "easy_validator"
21
+ end
@@ -0,0 +1,22 @@
1
+ module EasyMail
2
+ class BaseModel
3
+ extend ActiveModel::Naming
4
+ extend ActiveModel::Translation
5
+ include ActiveModel::Validations
6
+ include ActiveModel::Conversion
7
+
8
+ attr_accessor :email2
9
+
10
+ validates :email2, absence: true
11
+
12
+ def initialize(attributes = {})
13
+ attributes.each do |name, value|
14
+ send("#{name}=", value)
15
+ end
16
+ end
17
+
18
+ def persisted?
19
+ false
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,159 @@
1
+ module EasyMail
2
+ class Mailer
3
+ include ActiveModel::Conversion
4
+ extend ActiveModel::Naming
5
+
6
+ mattr_accessor :default_to, :default_bcc, :default_from, :default_subject, :default_config
7
+
8
+ attr_accessor :name, :to, :bcc, :from, :subject, :template_name, :namespace
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
15
+ @subject = attributes[:subject] || self.class.default_subject
16
+ @template_name = attributes[:name]
17
+ @namespace = attributes[:name].split("/").slice(0..-2).map(&:classify).join("::")
18
+
19
+ generate_mailer
20
+ generate_controller
21
+
22
+ self.class.all << self
23
+ end
24
+
25
+ def namespace_array
26
+ namespace.try(:split, "::") || []
27
+ end
28
+
29
+ def namespace_module
30
+ if namespace.present?
31
+ namespace_array.each_with_index do |name, index|
32
+ unless defined?(name.constantize)
33
+ if index == 0
34
+ Object
35
+ else
36
+ namespace_array.slice(0..index).join("::").constantize
37
+ end.const_set(name, Module.new)
38
+ end
39
+ end
40
+ namespace.constantize
41
+ else
42
+ nil
43
+ end
44
+ end
45
+
46
+ def controller_namespace
47
+ begin
48
+ [namespace, "Mail"].select(&:present?).join("::").constantize
49
+ rescue NameError
50
+ (namespace_module || Object).const_set("Mail", Module.new)
51
+ end
52
+ end
53
+
54
+
55
+ def template_path
56
+ ["mail_template", "#{namespace.underscore}"].select(&:present?).join("/")
57
+ end
58
+
59
+ def mail_controller
60
+ @mail_controller ||= begin
61
+ (namespace_array + ["Mail"] + [mail_controller_name]).join("::").constantize
62
+ rescue NameError
63
+ controller_namespace.const_set(mail_controller_name, Class.new(mail_parent_controller))
64
+ end
65
+ end
66
+
67
+ def mail_controller_name
68
+ @mail_controller_name ||= "#{name.classify.pluralize}Controller"
69
+ end
70
+
71
+ def mail_parent_controller
72
+ (namespace_array + ["ApplicationController"]).join("::").constantize
73
+ end
74
+
75
+ def model_class
76
+ @model_class ||= (["Mail"] + namespace_array + [name.classify]).join("::").constantize
77
+ end
78
+
79
+ def mailer_class
80
+ mailer_class_name.constantize
81
+ end
82
+
83
+ def mailer_class_name
84
+ "#{name.classify}Mailer"
85
+ end
86
+
87
+ def generate_controller
88
+ mailer = self
89
+ mail_controller.class_eval do
90
+ define_method :show do
91
+ instance_variable_set("@#{mailer.name}", mailer.model_class.new)
92
+ end
93
+
94
+ define_method :create do
95
+ instance_variable_set("@#{mailer.name}", mailer.model_class.new(params[mailer.form_key]))
96
+ if instance_variable_get("@#{mailer.name}").valid?
97
+ mailer.mailer_class.send_mail(instance_variable_get("@#{mailer.name}")).deliver
98
+ render "shared/mail/confirmation"
99
+ else
100
+ render :show
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ def form_key
107
+ [namespace.underscore.gsub("/", "_"), "mail", name].select(&:present?).join("_").to_sym
108
+ end
109
+
110
+ def generate_mailer
111
+ mailer = self
112
+ klass = Class.new(ActionMailer::Base) do
113
+ define_method :send_mail do |column|
114
+ instance_variable_set("@#{column.class.name.demodulize.underscore}", column)
115
+
116
+ mail(to: mailer.to, bcc: mailer.bcc, from: mailer.from, subject: mailer.subject, template_path: mailer.template_path, template_name: mailer.template_name)
117
+ end
118
+ end
119
+
120
+ (namespace_module || Object).const_set(mailer_class_name, klass)
121
+ end
122
+
123
+ def persisted?
124
+ false
125
+ end
126
+
127
+ def route_url
128
+ "/" + [namespace.underscore, name].select(&:present?).join("/")
129
+ end
130
+
131
+ def route_to_controller_part
132
+ [namespace.underscore, 'mail', name.pluralize].select(&:present?).join('/')
133
+ end
134
+
135
+ def route_as
136
+ [namespace.underscore.gsub("/", "_"), 'mail', name].select(&:present?).join('_')
137
+ end
138
+
139
+ class << self
140
+ def all
141
+ @all ||= []
142
+ end
143
+
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]
149
+ end
150
+
151
+ def routes(router)
152
+ EasyMail::Mailer.all.each do |mailer|
153
+ router.get mailer.route_url, to: "#{mailer.route_to_controller_part}#show", as: mailer.route_as
154
+ router.post mailer.route_url, to: "#{mailer.route_to_controller_part}#create"
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,3 @@
1
+ module EasyMail
2
+ VERSION = "0.0.2"
3
+ end
data/lib/easy_mail.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'active_model/naming'
2
+ require 'active_model/translation'
3
+ require 'active_model/validations'
4
+ require 'active_model/conversion'
5
+ require 'action_mailer'
6
+
7
+
8
+ require "easy_validator"
9
+
10
+ require "easy_mail/mailer"
11
+ require "easy_mail/base_model"
12
+ require "easy_mail/version"
13
+
14
+
15
+ module EasyMail
16
+ end
@@ -0,0 +1,13 @@
1
+ module EasyMail
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def generate_init_file
7
+ copy_file "easy_mail.rb", "config/initializers/easy_mail.rb"
8
+ copy_file "mail_config.yml", "config/mail_config.yml"
9
+ copy_file "confirmation.html.erb", "app/views/shared/mail/confirmation.html.erb"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ <div>
2
+ <h3>Thank you</h3>
3
+ <h4>Contact information was successfully sent. We will respond to your message shortly.</h4>
4
+ </div>
@@ -0,0 +1,13 @@
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
+ #})
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,11 @@
1
+ module EasyMail
2
+ module Generators
3
+ class ModelGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def generate_init_file
7
+ template "model.rb", "app/models/mail/#{file_path}.rb"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ class Mail::<%= class_name %> < EasyMail::BaseModel
2
+ # attr_accessor :email
3
+ end
@@ -0,0 +1,13 @@
1
+ require "easy_mail"
2
+
3
+ describe EasyMail::BaseModel do
4
+ context ".new" do
5
+ it do
6
+ EasyMail::BaseModel.new.valid?.should == true
7
+ end
8
+
9
+ it do
10
+ EasyMail::BaseModel.new(email2: "1").valid?.should == false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,106 @@
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 do
86
+ @mailer.name.should == "contact"
87
+ @mailer.namespace.should == "Store::Part"
88
+ end
89
+
90
+ it "controller part" do
91
+ defined?(Store::Part::Mail::ContactsController).should == "constant"
92
+ @mailer.form_key.should == :store_part_mail_contact
93
+ end
94
+
95
+ it "mailer part" do
96
+ defined?(Store::Part::ContactMailer).should == "constant"
97
+ @mailer.template_path.should == "mail_template/store/part"
98
+ end
99
+
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"
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,101 @@
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
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_mail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - liangmincong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: easy_validator
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: test
31
+ email:
32
+ - iceskylmc@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - easy_mail.gemspec
43
+ - lib/easy_mail.rb
44
+ - lib/easy_mail/base_model.rb
45
+ - lib/easy_mail/mailer.rb
46
+ - lib/easy_mail/version.rb
47
+ - lib/generators/easy_mail/install/install_generator.rb
48
+ - lib/generators/easy_mail/install/templates/confirmation.html.erb
49
+ - lib/generators/easy_mail/install/templates/easy_mail.rb
50
+ - lib/generators/easy_mail/install/templates/mail_config.yml
51
+ - lib/generators/easy_mail/model/model_generator.rb
52
+ - lib/generators/easy_mail/model/templates/model.rb
53
+ - spec/base_model_spec.rb
54
+ - spec/easy_mail_spec.rb
55
+ - spec/mailer_spec.rb
56
+ homepage: ''
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.24
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: test
80
+ test_files:
81
+ - spec/base_model_spec.rb
82
+ - spec/easy_mail_spec.rb
83
+ - spec/mailer_spec.rb
84
+ has_rdoc: