locale_mailer 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 26b6b91858f7a2e8c5e7ff0958d62ac522edf7b6
4
- data.tar.gz: 30f6e223d04fa5b0d0cc545d7f8b624675ee3465
3
+ metadata.gz: 991fd4e039b610e59c3a7931e3ac33152c91cb14
4
+ data.tar.gz: e896a44e5623e26fd90da6ea88941ce587f660cd
5
5
  SHA512:
6
- metadata.gz: ca62cf839282f01fe71bd47b441f7776cdd9f69b0a2e28d286012ecad11a421f249688d670768f551ca024ea726ee28c2139276a573cc3cb9a842c4d0e1419bb
7
- data.tar.gz: 206c0c95ed7d9031ee62b11a08d1576455d624de5883ce7e019c24b622b03a4624a946328b8c86cdba1e4089e891535b5df567e81596ae537a2db70336c979d5
6
+ metadata.gz: a60553225e034aa5af897f3c68d1af9d0c8abadf0abf477edb977ee909d663bda8b915c4dfdc855e3c7fffbfcd02e7c83219b460d0f3735d7efea51e780c3302
7
+ data.tar.gz: 61aba9f059b18ec8d5d85f15a3f63c911f3af38a72cecd758c496c928723bec07f2ab1d0615505d93be4cb3dcf751b4e15e313821a29a8e0398128427cc9032b
@@ -20,84 +20,77 @@ module LocaleMailer
20
20
  included do
21
21
  alias_method_chain :mail, :localized_templates
22
22
  private :mail, :mail_with_localized_templates, :mail_without_localized_templates
23
+ helper_method :action_i18n_options, :action_i18n_path
23
24
  end
24
25
 
25
26
  private
26
27
 
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.gsub('/','.').underscore,
36
- # options[:template_name] || action_name
37
- # ].compact.join('.')
38
-
39
-
40
- if I18n.exists? i18n_path(options), I18n.locale
41
- locale_options = instance_public_variables_to_hash
42
- options[:subject] = subject_from_locale(i18n_path(options), locale_options) unless options.key?(:subject)
43
- mail_without_localized_templates options do |format|
44
- html_body = body_from_locale i18n_path(options), locale_options, options
45
- format.html {
46
- html_body
47
- }
48
- format.text {
49
- Loofah.scrub_fragment(html_body, LocaleMailer::A2HREF).to_text
50
- }
51
- end
52
- else
53
- raise e
28
+ def mail_with_localized_templates(options = {}, &block)
29
+ begin
30
+ mail_without_localized_templates(options, &block)
31
+ rescue ActionView::MissingTemplate => e
32
+ options.symbolize_keys!
33
+
34
+ if I18n.exists? action_i18n_path(options), I18n.locale
35
+ options[:subject] = subject_from_locale(options) unless options.key?(:subject)
36
+ mail_without_localized_templates options do |format|
37
+ html_body = body_from_locale options
38
+ format.html {
39
+ html_body
40
+ }
41
+ format.text {
42
+ Loofah.scrub_fragment(html_body, LocaleMailer::A2HREF).to_text
43
+ }
54
44
  end
45
+ else
46
+ raise e
55
47
  end
56
48
  end
49
+ end
57
50
 
58
- def i18n_path options
59
- [
60
- Rails.configuration.locale_mailer_path_prefix,
61
- options[:template_path] || mailer_name.gsub('/','.').underscore,
62
- options[:template_name] || action_name
63
- ].compact.join('.')
64
- end
51
+ def action_i18n_path options = {}
52
+ @action_i18n_path ||= [
53
+ Rails.configuration.locale_mailer_path_prefix,
54
+ (options[:template_path] || mailer_name.gsub('/','.').underscore),
55
+ (options[:template_name] || action_name)
56
+ ].compact.join('.')
57
+ end
65
58
 
66
- def instance_public_variables_to_hash
67
- instance_variables.inject({}) do |memo, name|
68
- if name.to_s.match(/\A@(?!_).*/)
69
- if instance_variable_get(name).try(:as_json).is_a? Hash
70
- flatten_hash(instance_variable_get(name).as_json).each do |(k,v)|
71
- memo["#{name.to_s.gsub(/\A@/,'')}_#{k}".to_sym] = v
72
- end
73
- else
74
- memo[name.to_s.gsub(/\A@/,'').to_sym] = instance_variable_get(name)
59
+ def action_i18n_options
60
+ @action_i18n_options ||= instance_variables.inject({}) do |memo, name|
61
+ if name.to_s.match(/\A@(?!_).*/) and not name.to_s.match(/\A@action_i18n_/)
62
+ if instance_variable_get(name).try(:as_json).is_a? Hash
63
+ flatten_hash(instance_variable_get(name).as_json).each do |(k,v)|
64
+ memo["#{name.to_s.gsub(/\A@/,'')}_#{k}".to_sym] = v
75
65
  end
66
+ else
67
+ memo[name.to_s.gsub(/\A@/,'').to_sym] = instance_variable_get(name)
76
68
  end
77
- memo
78
69
  end
70
+ memo
79
71
  end
72
+ end
80
73
 
81
- def flatten_hash(hash)
82
- hash.each_with_object({}) do |(k, v), h|
83
- if v.is_a? Hash
84
- flatten_hash(v).map do |h_k, h_v|
85
- h["#{k}_#{h_k}".to_sym] = h_v
86
- end
87
- else
88
- h[k] = v
74
+ def flatten_hash(hash)
75
+ hash.each_with_object({}) do |(k, v), h|
76
+ if v.is_a? Hash
77
+ flatten_hash(v).map do |h_k, h_v|
78
+ h["#{k}_#{h_k}".to_sym] = h_v
89
79
  end
80
+ else
81
+ h[k] = v
90
82
  end
91
83
  end
84
+ end
92
85
 
93
- def subject_from_locale i18n_path, locale_options
94
- view_context.t([i18n_path, :subject].join('.'), locale_options)
95
- end
86
+ def subject_from_locale options
87
+ view_context.t([action_i18n_path(options), :subject].join('.'), action_i18n_options)
88
+ end
96
89
 
97
- def body_from_locale i18n_path, locale_options, options
98
- render inline: view_context.text([i18n_path, :body].join('.'), locale_options),
99
- layout: options[:layout] || _layout
100
- end
90
+ def body_from_locale options
91
+ render inline: view_context.text([action_i18n_path(options), :body].join('.'), action_i18n_options),
92
+ layout: options[:layout] || _layout
93
+ end
101
94
 
102
95
  end
103
96
  end
@@ -1,3 +1,3 @@
1
1
  module LocaleMailer
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locale_mailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - itkin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-08 00:00:00.000000000 Z
11
+ date: 2016-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionmailer