db_mailer_rails 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42052d77f07a90e9ba3872697932400da933c030
4
+ data.tar.gz: 6577ed9fd4e8a002fc81db6077e9f1dce6e5903e
5
+ SHA512:
6
+ metadata.gz: 89fb6764666ba884392f5744f70609aa140c0fc1bc0ccf0a2d1f68763dd9c9248d1c96005c22250c45dba0591448ab4aa03c304af890de526769d2a7b3b8a175
7
+ data.tar.gz: 480ab51e62f100b883c821d4541d3b973de295d2d852635f6f4ed98cfbdbab0b554119d2217029cf0d1664d40b26a439bc65e07e4d26d6a8ab66f3cc8a76940b
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ # RubyMine project folder
2
+ /.idea/
3
+
4
+ # bundler config
5
+ /.bundle/
6
+
7
+ /.yardoc
8
+ /_yardoc/
9
+ /coverage/
10
+ /doc/
11
+ /pkg/
12
+ /spec/reports/
13
+ /tmp/
14
+
15
+ # test RailsApp
16
+ spec/rails_app/log/
17
+ spec/rails_app/tmp/
18
+ spec/rails_app/db/*.sqlite3
19
+
20
+ # rspec failure tracking
21
+ .rspec_status
22
+
23
+ # linux files
24
+ .directory
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at alexhifer@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails', '~> 5.1'
6
+
7
+ group :development do
8
+ gem 'listen', '~> 3.0'
9
+ end
10
+
11
+ group :development, :test do
12
+ gem 'factory_bot', '~> 4.0'
13
+ end
14
+
15
+ group :test do
16
+ gem 'database_cleaner', '~> 1.6'
17
+ gem 'shoulda-matchers', '~> 3.1'
18
+ gem 'generator_spec', '~> 0.9'
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,161 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ db_mailer_rails (0.1.0)
5
+ actionmailer (>= 4.2, < 5.2)
6
+ actionpack (>= 4.2, < 5.2)
7
+ mustache (~> 1.0)
8
+ railties (>= 4.2, < 5.2)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ actioncable (5.1.4)
14
+ actionpack (= 5.1.4)
15
+ nio4r (~> 2.0)
16
+ websocket-driver (~> 0.6.1)
17
+ actionmailer (5.1.4)
18
+ actionpack (= 5.1.4)
19
+ actionview (= 5.1.4)
20
+ activejob (= 5.1.4)
21
+ mail (~> 2.5, >= 2.5.4)
22
+ rails-dom-testing (~> 2.0)
23
+ actionpack (5.1.4)
24
+ actionview (= 5.1.4)
25
+ activesupport (= 5.1.4)
26
+ rack (~> 2.0)
27
+ rack-test (>= 0.6.3)
28
+ rails-dom-testing (~> 2.0)
29
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
30
+ actionview (5.1.4)
31
+ activesupport (= 5.1.4)
32
+ builder (~> 3.1)
33
+ erubi (~> 1.4)
34
+ rails-dom-testing (~> 2.0)
35
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
36
+ activejob (5.1.4)
37
+ activesupport (= 5.1.4)
38
+ globalid (>= 0.3.6)
39
+ activemodel (5.1.4)
40
+ activesupport (= 5.1.4)
41
+ activerecord (5.1.4)
42
+ activemodel (= 5.1.4)
43
+ activesupport (= 5.1.4)
44
+ arel (~> 8.0)
45
+ activesupport (5.1.4)
46
+ concurrent-ruby (~> 1.0, >= 1.0.2)
47
+ i18n (~> 0.7)
48
+ minitest (~> 5.1)
49
+ tzinfo (~> 1.1)
50
+ arel (8.0.0)
51
+ builder (3.2.3)
52
+ concurrent-ruby (1.0.5)
53
+ crass (1.0.3)
54
+ database_cleaner (1.6.2)
55
+ diff-lcs (1.3)
56
+ erubi (1.7.0)
57
+ factory_bot (4.8.2)
58
+ activesupport (>= 3.0.0)
59
+ ffi (1.9.18)
60
+ generator_spec (0.9.4)
61
+ activesupport (>= 3.0.0)
62
+ railties (>= 3.0.0)
63
+ globalid (0.4.1)
64
+ activesupport (>= 4.2.0)
65
+ i18n (0.9.1)
66
+ concurrent-ruby (~> 1.0)
67
+ listen (3.1.5)
68
+ rb-fsevent (~> 0.9, >= 0.9.4)
69
+ rb-inotify (~> 0.9, >= 0.9.7)
70
+ ruby_dep (~> 1.2)
71
+ loofah (2.1.1)
72
+ crass (~> 1.0.2)
73
+ nokogiri (>= 1.5.9)
74
+ mail (2.7.0)
75
+ mini_mime (>= 0.1.1)
76
+ method_source (0.9.0)
77
+ mini_mime (1.0.0)
78
+ mini_portile2 (2.3.0)
79
+ minitest (5.10.3)
80
+ mustache (1.0.5)
81
+ nio4r (2.1.0)
82
+ nokogiri (1.8.1)
83
+ mini_portile2 (~> 2.3.0)
84
+ rack (2.0.3)
85
+ rack-test (0.8.2)
86
+ rack (>= 1.0, < 3)
87
+ rails (5.1.4)
88
+ actioncable (= 5.1.4)
89
+ actionmailer (= 5.1.4)
90
+ actionpack (= 5.1.4)
91
+ actionview (= 5.1.4)
92
+ activejob (= 5.1.4)
93
+ activemodel (= 5.1.4)
94
+ activerecord (= 5.1.4)
95
+ activesupport (= 5.1.4)
96
+ bundler (>= 1.3.0)
97
+ railties (= 5.1.4)
98
+ sprockets-rails (>= 2.0.0)
99
+ rails-dom-testing (2.0.3)
100
+ activesupport (>= 4.2.0)
101
+ nokogiri (>= 1.6)
102
+ rails-html-sanitizer (1.0.3)
103
+ loofah (~> 2.0)
104
+ railties (5.1.4)
105
+ actionpack (= 5.1.4)
106
+ activesupport (= 5.1.4)
107
+ method_source
108
+ rake (>= 0.8.7)
109
+ thor (>= 0.18.1, < 2.0)
110
+ rake (12.3.0)
111
+ rb-fsevent (0.10.2)
112
+ rb-inotify (0.9.10)
113
+ ffi (>= 0.5.0, < 2)
114
+ rspec (3.7.0)
115
+ rspec-core (~> 3.7.0)
116
+ rspec-expectations (~> 3.7.0)
117
+ rspec-mocks (~> 3.7.0)
118
+ rspec-core (3.7.0)
119
+ rspec-support (~> 3.7.0)
120
+ rspec-expectations (3.7.0)
121
+ diff-lcs (>= 1.2.0, < 2.0)
122
+ rspec-support (~> 3.7.0)
123
+ rspec-mocks (3.7.0)
124
+ diff-lcs (>= 1.2.0, < 2.0)
125
+ rspec-support (~> 3.7.0)
126
+ rspec-support (3.7.0)
127
+ ruby_dep (1.5.0)
128
+ shoulda-matchers (3.1.2)
129
+ activesupport (>= 4.0.0)
130
+ sprockets (3.7.1)
131
+ concurrent-ruby (~> 1.0)
132
+ rack (> 1, < 3)
133
+ sprockets-rails (3.2.1)
134
+ actionpack (>= 4.0)
135
+ activesupport (>= 4.0)
136
+ sprockets (>= 3.0.0)
137
+ sqlite3 (1.3.13)
138
+ thor (0.20.0)
139
+ thread_safe (0.3.6)
140
+ tzinfo (1.2.4)
141
+ thread_safe (~> 0.1)
142
+ websocket-driver (0.6.5)
143
+ websocket-extensions (>= 0.1.0)
144
+ websocket-extensions (0.1.3)
145
+
146
+ PLATFORMS
147
+ ruby
148
+
149
+ DEPENDENCIES
150
+ database_cleaner (~> 1.6)
151
+ db_mailer_rails!
152
+ factory_bot (~> 4.0)
153
+ generator_spec (~> 0.9)
154
+ listen (~> 3.0)
155
+ rails (~> 5.1)
156
+ rspec (~> 3.0)
157
+ shoulda-matchers (~> 3.1)
158
+ sqlite3 (~> 1.3.0)
159
+
160
+ BUNDLED WITH
161
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Alexey Degtyarev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,157 @@
1
+ ![DbMailerRails Logo](https://raw.github.com/alexhifer/db_mailer_rails/master/db_mailer_rails.png)
2
+
3
+ This gem allows you to store mail templates in the database.
4
+
5
+ Capabilities:
6
+
7
+ * Sending mails with ActionMailer::MessageDelivery (#deliver_now and #deliver_later methods)
8
+ * For replace values and logic in mail templates used Mustache
9
+ * To insert mail templates, use the configuration file(*config/db_mailer_templates.yml*) and task(*rake db_mailer_rails:sync*)
10
+ * Refactoring an existing project with sending mails is minimal
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'db_mailer_rails'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ Next, you need to run the generator:
25
+
26
+ $ rails g db_mailer_rails:install
27
+
28
+ And also execute the created migration:
29
+
30
+ $ rake db:migrate
31
+
32
+ Syncing the configuration file(*config/db_mailer_templates.yml*) with database:
33
+
34
+ $ rake db_mailer_rails:sync
35
+
36
+ ## Usage
37
+
38
+ ##### A quick example of sending:
39
+
40
+ ```ruby
41
+ HelloWorldDbMailer.build(OpenStruct.new(name: '')).deliver_now
42
+ ```
43
+
44
+ **HelloWorldDbMailer** - this is the inheritance class of DbMailerRails::Base. It provides a mechanism for searching and rendering a template. All db_mailers need to inherit from it. All db_mailers are in the app /db_mailers folder.
45
+
46
+ ##### A quick example of create db_mailer:
47
+
48
+ 1. Create *app/db_mailers/test_db_mailer.rb* with content:
49
+ ````ruby
50
+ class TestDbMailer < DbMailerRails::Base
51
+ def fields
52
+ super.merge(current_year: 'Current year')
53
+ end
54
+
55
+ def current_year
56
+ Time.current.year
57
+ end
58
+ end
59
+ ````
60
+
61
+ 2. Add the following lines to *config/db_mailer_rails.yml*:
62
+ ```yaml
63
+ TestDbMailer:
64
+ subject: This test email
65
+ from: Site <no-reply@example.com>
66
+ to: User <user@example.com>
67
+ body: 'Hello, User! Current year is {{current_year}}'
68
+ ```
69
+
70
+ 3. Run task:
71
+
72
+ $ rake db_mailer_rails:sync
73
+
74
+ 4. Use db_mailer in your project:
75
+
76
+ ```ruby
77
+ TestDbMailer.build.deliver_now
78
+ ````
79
+
80
+ ##### Build db_mailer with several attributes:
81
+
82
+ ```ruby
83
+ class TestDbMailer < DbMailerRails::Base
84
+ attr_accessor :auth_token
85
+
86
+ delegate :name, :email, to: :object, prefix: :user
87
+
88
+ def build(object, **args)
89
+ self.auth_token = args[:auth_token]
90
+ super
91
+ end
92
+
93
+ def fields
94
+ super.merge(
95
+ user_name: 'User name',
96
+ user_email: 'User email',
97
+ auth_token: 'Auth token'
98
+ )
99
+ end
100
+ end
101
+ ```
102
+
103
+ Sending a message with multiple attributes:
104
+
105
+ ```ruby
106
+ TestDbMailer.build(current_user, auth_token: 'qwerty123')
107
+ ```
108
+
109
+ **current_user** - instance of User model with attributes *name* and *email*
110
+
111
+ ##### DbMailer layout
112
+
113
+ The default template is *app/views/layouts/mailer.html.erb*.
114
+
115
+ To change the template for the db_mailer, you need to override method *layout* in your db_mailer:
116
+
117
+ ```ruby
118
+ def layout
119
+ 'custom_mail_template'
120
+ end
121
+ ```
122
+
123
+ ##### Development of mail template editing module
124
+
125
+
126
+ For the user to know what variables he can use in the template, you need helper *db_mailer_rails_fields_render*:
127
+
128
+ ```ruby
129
+ db_mailer_rails_fields_render(template)
130
+ ```
131
+
132
+ **template** - instance of DbMailerTemplate
133
+
134
+ This helper render:
135
+
136
+ ```html
137
+ <ul class="db_mailer__fields">
138
+ <li class="db_mailer__field"><span>{{user_name}}</span> - User name</li>
139
+ <li class="db_mailer__field"><span>{{user_email}}</span> - User e-mail</li>
140
+ </ul>
141
+ ```
142
+
143
+ ## Development
144
+
145
+ After checking out the repo, run `bundle` to install dependencies. Then, run `rake spec` to run the tests.
146
+
147
+ ## Contributing
148
+
149
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/db_mailer_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
150
+
151
+ ## License
152
+
153
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
154
+
155
+ ## Code of Conduct
156
+
157
+ Everyone interacting in the DbMailerRails project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/db_mailer_rails/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'db_mailer_rails'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ HelloWorldDbMailer:
2
+ subject: '{{app_name}} - Hello world!'
3
+ from: Site <no-reply@example.com>
4
+ to: '{{user_name}} <{{user_email}}>'
5
+ body: >
6
+ Hello, {{user_name}}!<br><br>
7
+ This is your first e-mail with <b>DbMailerRails gem</b> :)<br><br>
8
+ Don't forget to "Star" this <a href="https://github.com/thoughtbot/paperclip">gem on GitHub</a>.
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'db_mailer_rails/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'db_mailer_rails'
8
+ s.version = DbMailerRails::VERSION.dup
9
+ s.authors = 'Alexey Degtyarev'
10
+ s.email = 'alexhifer@gmail.com'
11
+
12
+ s.summary = %q{Store mail templates in the database.}
13
+ s.description = %q{This gem allows you to store mail templates in the database.}
14
+ s.license = 'MIT'
15
+
16
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ s.bindir = 'exe'
20
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ s.require_paths = ['lib']
22
+
23
+ s.add_dependency 'actionpack', ['>= 4.2', '< 5.2']
24
+ s.add_dependency 'actionmailer', ['>= 4.2', '< 5.2']
25
+ s.add_dependency 'railties', ['>= 4.2', '< 5.2']
26
+ s.add_dependency 'mustache', '~> 1.0'
27
+ s.add_development_dependency 'rspec', '~> 3.0'
28
+ s.add_development_dependency 'sqlite3', '~> 1.3.0'
29
+ end
Binary file
@@ -0,0 +1,58 @@
1
+ require 'action_mailer'
2
+ require 'db_mailer_rails/replacer'
3
+ require 'db_mailer_rails/template_finder'
4
+
5
+ module DbMailerRails
6
+ class Base < ActionMailer::Base
7
+ attr_accessor :object
8
+
9
+ def build(object = nil, **_args)
10
+ self.object = object
11
+ replacer = Replacer.new(fields_with_data)
12
+ finder = TemplateFinder.new(self.class)
13
+ template = finder.db_mail_template
14
+
15
+ mail_params = {
16
+ from: replacer.replace(template.from),
17
+ to: replacer.replace(template.to),
18
+ subject: replacer.replace(template.subject)
19
+ }
20
+
21
+ # add attachments to mail
22
+ mail_attachments
23
+
24
+ mail(mail_params) do |format|
25
+ format.html { render html: replacer.replace(template.body).html_safe, layout: layout_by_environment }
26
+ end
27
+ end
28
+
29
+ def fields
30
+ {}
31
+ end
32
+
33
+ def mail_attachments
34
+ end
35
+
36
+ private
37
+
38
+ def add_inline_image(file_path)
39
+ file = file_path.split('/').last
40
+ image_path = Rails.root.join('app', 'assets', 'images', file_path)
41
+ attachments.inline[file] = File.read(image_path) if File.exists?(image_path)
42
+ end
43
+
44
+ def layout
45
+ 'mailer'
46
+ end
47
+
48
+ def layout_by_environment
49
+ Rails.env.test? ? false : layout
50
+ end
51
+
52
+ def fields_with_data
53
+ @fields_with_data ||= fields.keys.each_with_object({}) do |field_name, res|
54
+ res[field_name] = self.send(field_name)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,24 @@
1
+ module DbMailerRails
2
+ module Helper
3
+ include ActionView::Helpers::TagHelper
4
+
5
+ # Render DbMailerRails fields
6
+ # <ul class="db_mailer__fields">
7
+ # <li class="db_mailer__field"><span>{{[FIELD_NAME]}}</span> - [FIELD_DESCRIPTION]</li>
8
+ # ...
9
+ # </ul>
10
+ #
11
+ # @param db_mailer_template [DbMailerTemplate]
12
+ # @return [String] html safe string
13
+ def db_mailer_rails_fields_render(db_mailer_template)
14
+ return '' unless db_mailer_template
15
+
16
+ list = db_mailer_template.db_mailer.new.fields.map do |field_name, description|
17
+ field = content_tag('span', "{{#{field_name}}}", class: 'db_mailer__field_name')
18
+ content_tag('li', "#{field} - #{description}".html_safe, class: 'db_mailer__field')
19
+ end
20
+
21
+ content_tag('ul', list.join.html_safe, class: 'db_mailer__fields')
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module DbMailerRails
2
+ module Models
3
+ module Methods
4
+ extend ActiveSupport::Concern
5
+
6
+ def db_mailer
7
+ db_mailer_class.constantize rescue nil
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module DbMailerRails
2
+ module Models
3
+ module Validatable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ validates :db_mailer_class, :subject, :from, :to, :body, presence: true, length: { minimum: 3 }
8
+ validates :subject, length: { maximum: 80 }
9
+ validates :from, length: { maximum: 64 }
10
+ validates :to, length: { maximum: 64 }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module DbMailerRails
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks { load 'tasks/db_mailer_rails.rake' }
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ require 'mustache'
2
+
3
+ module DbMailerRails
4
+ class Replacer
5
+ attr_accessor :replace_hash
6
+
7
+ # @param replace_hash [Hash]
8
+ def initialize(replace_hash)
9
+ self.replace_hash = replace_hash
10
+ end
11
+
12
+ # @param text [DbMailerRails::Base]
13
+ # @return [String]
14
+ def replace(text)
15
+ Mustache.render(text, replace_hash)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ module DbMailerRails
2
+ class TemplateFinder
3
+ attr_accessor :db_mail_template
4
+
5
+ # @param db_mailer_class [DbMailerRails::Base]
6
+ def initialize(db_mailer_class)
7
+ self.db_mail_template = find(db_mailer_class)
8
+ end
9
+
10
+ private
11
+
12
+ def find(db_mailer_class)
13
+ db_mail_template_class.find_by!(db_mailer_class: db_mailer_class.name)
14
+ end
15
+
16
+ def db_mail_template_class
17
+ DbMailerRails.template_model_class.constantize rescue nil
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module DbMailerRails
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails'
2
+
3
+ require 'db_mailer_rails/railtie' if defined?(Rails::Railtie)
4
+ require 'db_mailer_rails/base'
5
+ require 'db_mailer_rails/helper'
6
+ ActionView::Base.send :include, DbMailerRails::Helper
7
+
8
+ module DbMailerRails
9
+ # Template class
10
+ mattr_accessor :template_model_class
11
+ @@template_model_class = 'DbMailerTemplate'
12
+
13
+ # Sync file
14
+ mattr_accessor :sync_file_path
15
+ @@sync_file_path = 'config/db_mailer_templates.yml'
16
+
17
+ def self.setup
18
+ yield self
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ module DbMailerRails
2
+ module Generators
3
+ module Helpers
4
+ module MigrationHelper
5
+ def migration_path
6
+ if Rails.version >= '5.0.3'
7
+ db_migrate_path
8
+ else
9
+ 'db/migrate'
10
+ end
11
+ end
12
+
13
+ def migration_destination_path
14
+ File.join(destination_root, migration_path, "create_#{table_name}.rb")
15
+ end
16
+
17
+ def migration_version
18
+ if rails5?
19
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ module DbMailerRails
2
+ module Generators
3
+ module Helpers
4
+ module ModelHelper
5
+ private
6
+
7
+ def table_name
8
+ @table_name ||= model_class.tableize.gsub('/', '_')
9
+ end
10
+
11
+ def model_exists?
12
+ File.exist?(model_path)
13
+ end
14
+
15
+ def model_path
16
+ @model_path ||= File.join(destination_root, 'app', 'models', "#{model_class.underscore}.rb")
17
+ end
18
+
19
+ def model_class
20
+ @model_class ||= DbMailerRails.template_model_class
21
+ end
22
+
23
+ def model_superclass
24
+ rails5? ? 'ApplicationRecord' : 'ActiveRecord::Base'
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,53 @@
1
+ require 'rails/generators/base'
2
+ require 'rails/generators/active_record'
3
+ require 'active_support/core_ext/string'
4
+
5
+ require 'generators/db_mailer_rails/helpers/model_helper'
6
+ require 'generators/db_mailer_rails/helpers/migration_helper'
7
+
8
+ module DbMailerRails
9
+ module Generators
10
+ class InstallGenerator < Rails::Generators::Base
11
+ include Helpers::ModelHelper
12
+ include Helpers::MigrationHelper
13
+ include ActiveRecord::Generators::Migration
14
+
15
+ desc 'Creates a DbMailerRails initializer, migration, model, sync file, test db_mailer for your application.'
16
+
17
+ INITIALIZER_FILE = 'db_mailer_rails.rb'.freeze
18
+ TEST_DB_MAILER_FILE = 'hello_world_db_mailer.rb'.freeze
19
+
20
+ source_root File.expand_path('../templates', __FILE__)
21
+
22
+ def copy_sync_file
23
+ copy_file '../../../../config/db_mailer_templates.yml', sync_file_path
24
+ end
25
+
26
+ def copy_template_model_file
27
+ template 'template_model.rb', model_path
28
+ end
29
+
30
+ def copy_initializer_file
31
+ template 'initializer.rb', "config/initializers/#{INITIALIZER_FILE}"
32
+ end
33
+
34
+ def copy_test_db_mailer_file
35
+ copy_file TEST_DB_MAILER_FILE, "app/db_mailers/#{TEST_DB_MAILER_FILE}"
36
+ end
37
+
38
+ def copy_migration_file
39
+ migration_template 'migration.rb', migration_destination_path, migration_version: migration_version
40
+ end
41
+
42
+ private
43
+
44
+ def rails5?
45
+ Rails.version.start_with? '5'
46
+ end
47
+
48
+ def sync_file_path
49
+ DbMailerRails.sync_file_path
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ class HelloWorldDbMailer < DbMailerRails::Base
2
+ delegate :name, to: :object, prefix: :app
3
+
4
+ def fields
5
+ super.merge(
6
+ user_name: 'User test name',
7
+ user_email: 'User test e-mail address',
8
+ app_name: 'Application name'
9
+ )
10
+ end
11
+
12
+ def user_name
13
+ 'User'
14
+ end
15
+
16
+ def user_email
17
+ 'db_mailer_rails@example.com'
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ DbMailerRails.setup do |config|
2
+ # The name of the class where the DbMailerRails gem will store the email templates
3
+ # config.template_model_class = 'DbMailerTemplate'
4
+
5
+ # The path to the file that contains the mail templates in YAML format
6
+ # config.sync_file_path = 'config/db_mailer_templates.yml'
7
+ end
@@ -0,0 +1,15 @@
1
+ class Create<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :<%= table_name %> do |t|
4
+ t.string :db_mailer_class, limit: 64, null: false
5
+ t.string :subject, limit: 80, null: false
6
+ t.string :from, limit: 64, null: false
7
+ t.string :to, limit: 64, null: false
8
+ t.text :body, null: false
9
+
10
+ t.timestamps null: false
11
+ end
12
+
13
+ add_index :<%= table_name %>, :db_mailer_class, unique: true
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'db_mailer_rails/models/methods'
2
+ require 'db_mailer_rails/models/validatable'
3
+
4
+ class <%= model_class %> < <%= model_superclass %>
5
+ include DbMailerRails::Models::Methods
6
+ include DbMailerRails::Models::Validatable
7
+ end
@@ -0,0 +1,28 @@
1
+ require 'db_mailer_rails'
2
+
3
+ namespace :db_mailer_rails do
4
+ desc 'Syncing DbMailerRails templates sync_file_path(default config/db_mailer_rails.yml) file with database.'
5
+ task :sync, [:force] => :environment do |_task, args|
6
+ force = args[:force]
7
+ mail_template_class = DbMailerRails.template_model_class.constantize
8
+
9
+ config_path = Rails.root.join(DbMailerRails.sync_file_path).to_s
10
+ mail_templates_from_file = YAML.load_file(config_path)
11
+
12
+ # Remove from database if not exist in configuration file
13
+ mail_template_class.where('db_mailer_class NOT IN(?)', mail_templates_from_file.keys).delete_all
14
+
15
+ mail_templates_from_file.each do |db_mailer_class, attributes|
16
+ db_mail_template = mail_template_class.find_or_initialize_by(db_mailer_class: db_mailer_class)
17
+ next if !force && !db_mail_template.new_record?
18
+
19
+ db_mail_template.assign_attributes(attributes)
20
+
21
+ if db_mail_template.save
22
+ puts "#{db_mailer_class} synchronization is successful."
23
+ else
24
+ puts "#{db_mailer_class} synchronization is failed."
25
+ end
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db_mailer_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Degtyarev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: actionmailer
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '4.2'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '5.2'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '4.2'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '5.2'
53
+ - !ruby/object:Gem::Dependency
54
+ name: railties
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '4.2'
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '5.2'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '4.2'
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '5.2'
73
+ - !ruby/object:Gem::Dependency
74
+ name: mustache
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '1.0'
80
+ type: :runtime
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '1.0'
87
+ - !ruby/object:Gem::Dependency
88
+ name: rspec
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '3.0'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '3.0'
101
+ - !ruby/object:Gem::Dependency
102
+ name: sqlite3
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: 1.3.0
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: 1.3.0
115
+ description: This gem allows you to store mail templates in the database.
116
+ email: alexhifer@gmail.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".travis.yml"
124
+ - CODE_OF_CONDUCT.md
125
+ - Gemfile
126
+ - Gemfile.lock
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/setup
132
+ - config/db_mailer_templates.yml
133
+ - db_mailer_rails.gemspec
134
+ - db_mailer_rails.png
135
+ - lib/db_mailer_rails.rb
136
+ - lib/db_mailer_rails/base.rb
137
+ - lib/db_mailer_rails/helper.rb
138
+ - lib/db_mailer_rails/models/methods.rb
139
+ - lib/db_mailer_rails/models/validatable.rb
140
+ - lib/db_mailer_rails/railtie.rb
141
+ - lib/db_mailer_rails/replacer.rb
142
+ - lib/db_mailer_rails/template_finder.rb
143
+ - lib/db_mailer_rails/version.rb
144
+ - lib/generators/db_mailer_rails/helpers/migration_helper.rb
145
+ - lib/generators/db_mailer_rails/helpers/model_helper.rb
146
+ - lib/generators/db_mailer_rails/install_generator.rb
147
+ - lib/generators/db_mailer_rails/templates/hello_world_db_mailer.rb
148
+ - lib/generators/db_mailer_rails/templates/initializer.rb
149
+ - lib/generators/db_mailer_rails/templates/migration.rb
150
+ - lib/generators/db_mailer_rails/templates/template_model.rb
151
+ - lib/tasks/db_mailer_rails.rake
152
+ homepage:
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.6.13
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Store mail templates in the database.
176
+ test_files: []