email_template 0.6.0 → 0.6.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/lib/email-template/version.rb +1 -1
- data/lib/email_template.rb +2 -2
- data/lib/email_template/j_mailers.rb +6 -4
- data/lib/email_template/j_models.rb +90 -88
- data/lib/email_template/linkable/devise.rb +29 -27
- data/lib/email_template/linkable/devise_helper.rb +10 -8
- data/lib/generators/active_record/migration.rb +1 -1
- data/lib/generators/templates/devise/custom_devise_mailer.rb +1 -1
- metadata +8 -2
data/lib/email_template.rb
CHANGED
@@ -2,8 +2,8 @@ require "email_template/j_models"
|
|
2
2
|
require "email_template/j_mailers"
|
3
3
|
require "email_template/j_helpers"
|
4
4
|
|
5
|
-
include JModels
|
6
|
-
include JMailers
|
5
|
+
include EmailTemplate::JModels
|
6
|
+
include EmailTemplate::JMailers
|
7
7
|
include EmailTemplate::Mailers::Helpers
|
8
8
|
|
9
9
|
module EmailTemplate
|
@@ -1,7 +1,9 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module EmailTemplate
|
2
|
+
module JMailers
|
3
|
+
class TemplateSendMailer < ActionMailer::Base
|
4
|
+
def send_mail(template_name, mail_params = {}, template_params = {})
|
5
|
+
mailing(check_template(template_name), mail_params, template_params)
|
6
|
+
end
|
5
7
|
end
|
6
8
|
end
|
7
9
|
end
|
@@ -1,112 +1,114 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
module EmailTemplate
|
2
|
+
module JModels
|
3
|
+
class MailTemplate < ActiveRecord::Base
|
4
|
+
include ::EmailTemplate
|
5
|
+
include ActionView::Helpers::SanitizeHelper
|
6
|
+
|
7
|
+
self.table_name = 'email_templates'
|
8
|
+
|
9
|
+
attr_accessor :prepared
|
10
|
+
|
11
|
+
attr_accessible :name, :body, :subject, :classes
|
12
|
+
serialize :classes, Array
|
13
|
+
|
14
|
+
validates :name, :uniqueness => true
|
15
|
+
|
16
|
+
def prepare(attrs={})
|
17
|
+
@prepared ||= lambda{ |attrs|
|
18
|
+
attrs.stringify_keys!
|
19
|
+
body.gsub(/\#{.*?}/) do |match|
|
20
|
+
elem, action = match[2..-2].split('.')
|
21
|
+
if (action)
|
22
|
+
attrs[elem].try(action.to_sym) rescue match.to_s
|
23
|
+
else
|
24
|
+
attrs[elem] || match.to_s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
}.call(attrs)
|
28
|
+
end
|
9
29
|
|
10
|
-
|
11
|
-
|
30
|
+
def as_html(attrs={})
|
31
|
+
prepare(attrs)
|
32
|
+
end
|
12
33
|
|
13
|
-
|
34
|
+
def as_text(attrs={})
|
35
|
+
strip_tags(prepare(attrs))
|
36
|
+
#sanitize(prepare(attrs), :tags => %w(a), :attributes => %w(href))
|
37
|
+
end
|
14
38
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
attrs[elem].try(action.to_sym) rescue match.to_s
|
39
|
+
def prepare_fields
|
40
|
+
possible_attrs = []
|
41
|
+
self.classes.each do |resource_class|
|
42
|
+
current_class = resource_class.downcase.match(/\w*/mix).to_a.first
|
43
|
+
if (current_class[0] == '_')
|
44
|
+
possible_attrs << obj(current_class.from(1))
|
22
45
|
else
|
23
|
-
|
46
|
+
const_obj = current_class.camelize.constantize
|
47
|
+
[:find_attributes, :find_columns, :find_methods].each do |method|
|
48
|
+
possible_attrs += self.send(method, current_class, const_obj)
|
49
|
+
end
|
24
50
|
end
|
25
51
|
end
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
def as_html(attrs={})
|
30
|
-
prepare(attrs)
|
31
|
-
end
|
32
|
-
|
33
|
-
def as_text(attrs={})
|
34
|
-
strip_tags(prepare(attrs))
|
35
|
-
#sanitize(prepare(attrs), :tags => %w(a), :attributes => %w(href))
|
36
|
-
end
|
52
|
+
possible_attrs.uniq
|
53
|
+
end
|
37
54
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
if (current_class[0] == '_')
|
43
|
-
possible_attrs << obj(current_class.from(1))
|
44
|
-
else
|
45
|
-
const_obj = current_class.camelize.constantize
|
46
|
-
[:find_attributes, :find_columns, :find_methods].each do |method|
|
47
|
-
possible_attrs += self.send(method, current_class, const_obj)
|
48
|
-
end
|
55
|
+
protected
|
56
|
+
def finder(classname, items, val, current_pattern)
|
57
|
+
items.each_with_object([]) do |attr, ret|
|
58
|
+
(ret << obj(classname, attr.send(val))) if ((attr.send(val) =~ /#{current_pattern}/).nil? || current_pattern.nil?)
|
49
59
|
end
|
50
60
|
end
|
51
|
-
possible_attrs.uniq
|
52
|
-
end
|
53
61
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
62
|
+
def find_attributes(classname, object)
|
63
|
+
if object.respond_to?(:attributes)
|
64
|
+
finder(classname, object.attributes, :first, attr_pattern(classname))
|
65
|
+
end || []
|
58
66
|
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def find_attributes(classname, object)
|
62
|
-
if object.respond_to?(:attributes)
|
63
|
-
finder(classname, object.attributes, :first, attr_pattern(classname))
|
64
|
-
end || []
|
65
|
-
end
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
68
|
+
def find_columns(classname, object)
|
69
|
+
if object.respond_to?(:columns)
|
70
|
+
finder(classname, object.columns, :name, column_pattern(classname))
|
71
|
+
end || []
|
72
|
+
end
|
72
73
|
|
73
|
-
|
74
|
-
|
75
|
-
|
74
|
+
def find_methods(classname, object)
|
75
|
+
object.public_instance_methods.each_with_object([]) do |m_alias, ret|
|
76
|
+
(ret << obj(classname, m_alias.to_s.from(methods_header.length))) if m_alias.to_s.start_with?(methods_header)
|
77
|
+
end
|
76
78
|
end
|
77
|
-
end
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
80
|
+
def find_devise_methods(object)
|
81
|
+
res = []
|
82
|
+
res << obj("confirmation_token") if object.respond_to? :confirmation_token
|
83
|
+
res << obj("reset_password_token") if object.respond_to? :reset_password_token
|
84
|
+
res << obj("unlock_token") if object.respond_to? :unlock_token
|
85
|
+
res
|
86
|
+
end
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
private
|
89
|
+
def regex_from_list(list)
|
90
|
+
list.blank? ? nil : "(" + list.join(")|(") + ")"
|
91
|
+
end
|
91
92
|
|
92
|
-
|
93
|
-
|
94
|
-
|
93
|
+
def regex_from_hash(hash, object)
|
94
|
+
regex_from_list(hash[object] || hash['*'] || [])
|
95
|
+
end
|
95
96
|
|
96
|
-
|
97
|
-
|
98
|
-
|
97
|
+
def pattern(elems, object)
|
98
|
+
elems.is_a?(Array) ? regex_from_list(elems) : regex_from_hash(elems, object)
|
99
|
+
end
|
99
100
|
|
100
|
-
|
101
|
-
|
102
|
-
|
101
|
+
def column_pattern(object)
|
102
|
+
pattern(columns_black_list, object)
|
103
|
+
end
|
103
104
|
|
104
|
-
|
105
|
-
|
106
|
-
|
105
|
+
def attr_pattern(object)
|
106
|
+
pattern(attributes_black_list, object)
|
107
|
+
end
|
107
108
|
|
108
|
-
|
109
|
-
|
109
|
+
def obj(clas, name = nil)
|
110
|
+
"\#{#{[clas, name].compact.join('.')}}"
|
111
|
+
end
|
110
112
|
end
|
111
113
|
end
|
112
114
|
end
|
@@ -1,35 +1,37 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module EmailTemplate
|
2
|
+
module Linkable
|
3
|
+
module DeviseMailer
|
4
|
+
class DeviseTemplateSendMailer < Devise::Mailer
|
5
|
+
require "email_template/linkable/devise_helper"
|
6
|
+
include EmailTemplate::Linkable::DeviseMailerHelper
|
7
|
+
include ActionView::Helpers::UrlHelper
|
8
|
+
include Devise::Mailers::Helpers
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
def send_mail(record, action, template_name, mail_params = {}, template_params = {})
|
11
|
+
initialize_from_record(record)
|
12
|
+
opts = template_params[:*] || {}
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
template_params.reverse_merge!(
|
15
|
+
:confirm_link => link_to(opts[:confirm_name] || 'Confirm my account',
|
16
|
+
"#{link_head(record)}/confirmation?confirmation_token=#{@resource.confirmation_token}")
|
17
|
+
) if @resource.respond_to? :confirmation_token
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
template_params.reverse_merge!(
|
20
|
+
:edit_password_link => link_to(opts[:change_name] || 'Change my password',
|
21
|
+
"#{link_head(record)}/password/edit?reset_password_token=#{@resource.reset_password_token}")
|
22
|
+
) if @resource.respond_to? :reset_password_token
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
template_params.reverse_merge!(
|
25
|
+
:unlock_link => link_to(opts[:unlock_name] || 'Unlock my account',
|
26
|
+
"#{link_head(record)}/unlock?unlock_token => #{@resource.unlock_token}")
|
27
|
+
) if @resource.respond_to? :unlock_token
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
mailing(
|
30
|
+
check_template(template_name),
|
31
|
+
headers_for(action, mail_params).except(:subject, :template_path, :template_name),
|
32
|
+
template_params.except(:*)
|
33
|
+
)
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
@@ -1,12 +1,14 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module EmailTemplate
|
2
|
+
module Linkable
|
3
|
+
module DeviseMailerHelper
|
4
|
+
def link_head(record)
|
5
|
+
"#{ActionMailer::Base.default_url_options[:host]}/#{record.class.name.tableize.singularize}"
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def sending(record, action, template_name, email_opts = {}, template_opts = {})
|
9
|
+
send_mail(record, action, template_name, email_opts,
|
10
|
+
template_opts.merge(obj_class_name(record).to_sym => record))
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: email_template
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
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-02-
|
12
|
+
date: 2013-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -71,12 +71,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
71
|
- - ! '>='
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: '0'
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
hash: -656039679
|
74
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
78
|
none: false
|
76
79
|
requirements:
|
77
80
|
- - ! '>='
|
78
81
|
- !ruby/object:Gem::Version
|
79
82
|
version: '0'
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: -656039679
|
80
86
|
requirements: []
|
81
87
|
rubyforge_project:
|
82
88
|
rubygems_version: 1.8.24
|