locale_mailer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 84976aa5d9e1e0a87851e1fa39fc4468e39ec92d
4
+ data.tar.gz: 77e1d7298c24e1dd3af103e5c328d07500a10ee5
5
+ SHA512:
6
+ metadata.gz: d4f529b8cf0d3b852d2111797237cff27e83f1901cb2f081c3ad8291ce994420ef8eeddebdac39112c49325a33243cb057d5c1988a9bf43e20a0a292a511ee97
7
+ data.tar.gz: 0e11a064d9cfd5f766a45f2be01f971eb124cf91cd0ef82a093e163c12eacf82928888b8d30061db1c0ff6b93f596016c8a4c2fe526ef0ad2a618fc201fac244
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/
11
+ spec/internal/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in locale_mailer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 itkin
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,80 @@
1
+ # LocaleMailer
2
+
3
+ Handling action mailer view templates can become a bit heavy when your app has to send a lot of different emails
4
+
5
+ This gem aims at generating email view templates straight from your localization files when no template view is found
6
+
7
+ This is especially relevant for email views with simple content
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'locale_mailer'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install locale_mailer
24
+
25
+ ## Usage
26
+
27
+ Include `LocaleMailer::Concern` is automatically mixed into ActionMailer::Base during initialization phase.
28
+
29
+ Considering the following localization file
30
+
31
+ ```yaml
32
+ en:
33
+ my_mailer:
34
+ notify:
35
+ subject: "hello %{user_name}"
36
+ body:
37
+ - line 1
38
+ - line 2
39
+ - '<a href="%{user_link}">Click here</a>'
40
+ ```
41
+
42
+ ```ruby
43
+ class MyMailer < ApplicationMailer
44
+ def notify user
45
+ @user_name = "toto"
46
+ @user_link = "http://localhost.local"
47
+ mail to: user.email
48
+ end
49
+ end
50
+
51
+ MyMailer.notify.subject
52
+ > "hello toto"
53
+
54
+ MyMailer.notify.html_part.decoded
55
+ > "<p>line 1</p><p>line 2</p><p><a href=\"http://localhost.local\">Click here</a></p>"
56
+
57
+ MyMailer.notify.text_part.decoded
58
+ > "\nline 1\n\nline 2\n\nClick here http://localhost.local\n"
59
+
60
+ ```
61
+
62
+ 1 - If no template view is found by ActionMailer::Base, LocaleMailer will check if there is some locales specified at
63
+ `mailer_name`.`action_name` per default (or whatever template_path / template_name you passed to the `mail` method)
64
+
65
+ 2 - Your instance variables (usually available to views) will be passed to `I18n.translate`.
66
+
67
+ 3 - If your locale returns an array it will be converted in `<p>` tags into the html_part
68
+
69
+ 4 - Link tags present into the locale data will be converted to `text_content href_content`, other tags will be stripped
70
+
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/itkin/locale_mailer. This project is intended to be a safe, welcoming space for collaboration bla bla bla ...
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
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 "locale_mailer"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ module LocaleMailer
2
+ module ActionViewHelpers
3
+
4
+ #
5
+ # Retrieve and wrap a locale into a tag (p per default)
6
+ # If the locale sends back an array, build a collection of tags
7
+ #
8
+ def text locale, opts = {}
9
+ tag = opts.key?(:tag) ? opts.delete(:tag) : :p
10
+ array_to_tags t(locale, opts), tag
11
+ end
12
+
13
+ #
14
+ # Allow t to parse array retrieved from locale
15
+ #
16
+ def t *args
17
+ str = super
18
+ opts = args.extract_options!.except(:throw, :raise)
19
+ if str.is_a?(Array) and not opts.empty?
20
+ str.map { |item| I18n.interpolate(item, opts) unless item.nil? }.map(&:to_s)
21
+ else
22
+ str
23
+ end
24
+ end
25
+
26
+ #
27
+ # Build a collection of tags from an array
28
+ # todo : Check potential security issue here, use with caution
29
+ #
30
+ def array_to_tags array, tag_name = nil
31
+ Array.wrap(array).flatten.map do |line|
32
+ if tag_name
33
+ content_tag tag_name, line.to_s.html_safe
34
+ else
35
+ line.to_s.html_safe
36
+ end
37
+ end.join.html_safe
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,77 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/module/aliasing'
3
+ require 'active_support/core_ext/array/extract_options'
4
+ require 'active_support/core_ext/hash/keys'
5
+ require 'loofah'
6
+
7
+ module LocaleMailer
8
+
9
+ A2HREF = Loofah::Scrubber.new do |node|
10
+ if node.name == "a"
11
+ node.name = "text"
12
+ node.content = [node.content, node.attributes["href"].value].join(' ')
13
+ Loofah::Scrubber::STOP
14
+ end
15
+ end
16
+
17
+ module Concern
18
+ extend ActiveSupport::Concern
19
+
20
+ included do
21
+ alias_method_chain :mail, :localized_templates
22
+ private :mail, :mail_with_localized_templates, :mail_without_localized_templates
23
+ end
24
+
25
+ private
26
+
27
+ def mail_with_localized_templates(options = {}, &block)
28
+ begin
29
+ mail_without_localized_templates(options, &block)
30
+ rescue ActionView::MissingTemplate => e
31
+ options.symbolize_keys!
32
+
33
+ i18n_path = [
34
+ Rails.configuration.locale_mailer_path_prefix,
35
+ options[:template_path] || mailer_name,
36
+ options[:template_name] || action_name
37
+ ].compact.join('.')
38
+
39
+ if I18n.exists? i18n_path, I18n.locale
40
+ locale_options = instance_public_variables_to_hash
41
+ options[:subject] = subject_from_locale(i18n_path, locale_options) unless options.key?(:subject)
42
+ mail_without_localized_templates options do |format|
43
+ html_body = body_from_locale i18n_path, locale_options, options
44
+ format.html {
45
+ html_body
46
+ }
47
+ format.text {
48
+ Loofah.scrub_fragment(html_body, LocaleMailer::A2HREF).to_text
49
+ }
50
+ end
51
+ else
52
+ raise e
53
+ end
54
+ end
55
+ end
56
+
57
+
58
+
59
+ def instance_public_variables_to_hash
60
+ instance_variables.inject({}) do |memo, name|
61
+ memo[name.to_s.gsub(/\A@/,'').to_sym] = instance_variable_get(name) if name.to_s.match(/\A@(?!_).*/)
62
+ memo
63
+ end
64
+ end
65
+
66
+ def subject_from_locale i18n_path, locale_options
67
+ view_context.t([i18n_path, :subject].join('.'), locale_options)
68
+ end
69
+
70
+ def body_from_locale i18n_path, locale_options, options
71
+ render inline: view_context.text([i18n_path, :body].join('.'), locale_options),
72
+ layout: options[:layout] || _layout
73
+ end
74
+
75
+ end
76
+ end
77
+
@@ -0,0 +1,15 @@
1
+ module LocaleMailer
2
+ class Railtie < Rails::Railtie
3
+
4
+ config.locale_mailer_path_prefix = nil
5
+
6
+ initializer 'locale_mailer.actionview' do
7
+ ActionView::Base.send :include, LocaleMailer::ActionViewHelpers
8
+ end
9
+
10
+ initializer 'locale_mailer.actionmailer' do
11
+ ActionMailer::Base.send :include, LocaleMailer::Concern
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module LocaleMailer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "locale_mailer/version"
2
+ require "locale_mailer/actionview_helpers"
3
+ require "locale_mailer/concern"
4
+ require "locale_mailer/railtie" if defined?(Rails)
5
+
6
+ module LocaleMailer
7
+
8
+ end
9
+
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'locale_mailer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "locale_mailer"
8
+ spec.version = LocaleMailer::VERSION
9
+ spec.authors = ["itkin"]
10
+ spec.email = ["nicolas.papon@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby on Rails gem to allow email template views generation straight from localization data}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/itkin/locale_mailer"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "actionmailer"
31
+ spec.add_dependency "activesupport"
32
+ spec.add_dependency 'loofah'
33
+ spec.add_development_dependency 'combustion'
34
+ spec.add_development_dependency "bundler", "~> 1.10"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec"
37
+ spec.add_development_dependency "pry"
38
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locale_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - itkin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionmailer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: loofah
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: combustion
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - nicolas.papon@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - CODE_OF_CONDUCT.md
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/setup
142
+ - lib/locale_mailer.rb
143
+ - lib/locale_mailer/actionview_helpers.rb
144
+ - lib/locale_mailer/concern.rb
145
+ - lib/locale_mailer/railtie.rb
146
+ - lib/locale_mailer/version.rb
147
+ - locale_mailer.gemspec
148
+ homepage: https://github.com/itkin/locale_mailer
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.4.5.1
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Ruby on Rails gem to allow email template views generation straight from
172
+ localization data
173
+ test_files: []