effective_resources 2.24.0 → 2.25.0

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
  SHA256:
3
- metadata.gz: d92db06b508d630c29e392525580e558bc4fa5505358a04dcc371397ef0c6bc3
4
- data.tar.gz: e808c12ee8b6e047409ec0c5fb4855db64abfe79f15b35769f324b0202ad97e8
3
+ metadata.gz: 3a0aa5a3bcbd09397c6cd7815465cdfe0882045fba170ced84f6dbf59732db87
4
+ data.tar.gz: 0f78f2cce168bbda0d6221d6f8ea41b143c080d97b52ad78c7193c659d1aafa7
5
5
  SHA512:
6
- metadata.gz: 17830298a77d5f208d346b7a154da717c3670803d9868c803fa08f57f1fb37a0ebcab523a476970be3e90fbdc35fb3df2d4cf2508ddb5f48f7956d8767991b0a
7
- data.tar.gz: c03f3f837d93e5c4ed6506f6110cdc737aa3643c1ec0fd88f93168d4dc7f06650253eb9daf09b55e3a949f1f8835926575e6fae98d8a36b55a0834c2569316c5
6
+ metadata.gz: 035ca01e53c2d094b2174048e9a1f27f01b64e9cf12539587ac94350402b4569562e3be33322eb44f23182a88d5eb2fa68df4d8ff491ad0b6cbc95f2d2da3c09
7
+ data.tar.gz: 1df2b4a3f3ba19cd4e3d2b1875cd1d58627f3ab181bdb8cf0ebf268fcec8ea8e161754ec7a1f91618dbecb71e6d8c94ee7f5163aa833444a1e616c8c066712cf
@@ -28,6 +28,10 @@ module Effective
28
28
  params[resource_name].to_unsafe_h
29
29
  end
30
30
 
31
+ to_assign ||= if params[effective_resource_name].present? && params[effective_resource_name].respond_to?(:to_unsafe_h)
32
+ params[effective_resource_name].to_unsafe_h
33
+ end
34
+
31
35
  to_assign ||= if params.present?
32
36
  params.to_unsafe_h.except(:controller, :action, :id, :duplicate_id)
33
37
  end
@@ -103,6 +103,10 @@ module Effective
103
103
  effective_resource.name
104
104
  end
105
105
 
106
+ def effective_resource_name # effective_notification
107
+ effective_resource.resource_name
108
+ end
109
+
106
110
  def resource_name_id
107
111
  (effective_resource.name + '_id').to_sym
108
112
  end
@@ -83,5 +83,20 @@ module EffectiveResourcesEmailHelper
83
83
  render(partial: (partial || 'effective/acts_as_email_notification/fields'), locals: locals)
84
84
  end
85
85
 
86
+ def email_message_html?(message)
87
+ message.parts.find { |part| part.content_type.start_with?('text/html') }.present?
88
+ end
89
+
90
+ def email_message_plain?(message)
91
+ message.parts.find { |part| part.content_type.start_with?('text/html') }.blank?
92
+ end
93
+
94
+ def email_message_body(message)
95
+ html_body = message.parts.find { |part| part.content_type.start_with?('text/html') }.try(:body).to_s
96
+ plain_body = message.parts.find { |part| part.content_type.start_with?('text/plain') }.try(:body).to_s
97
+ message_body = message.body.to_s
98
+
99
+ html_body.presence || plain_body.presence || message_body.presence
100
+ end
86
101
 
87
102
  end
@@ -48,12 +48,33 @@ module ActsAsEmailNotification
48
48
  validate(if: -> { email_notification_plain? && body.present? }) do
49
49
  errors.add(:body, 'unexpected html tags found in body') if email_notification_body_html?
50
50
  end
51
+
52
+ validate(if: -> { email_notification_subject_template.present? }) do
53
+ if(invalid = email_notification_subject_variables - email_template_variables).present?
54
+ errors.add(:subject, "Invalid variable: #{invalid.to_sentence}")
55
+ end
56
+ end
57
+
58
+ validate(if: -> { email_notification_body_template.present? }) do
59
+ if(invalid = email_notification_body_variables - email_template_variables).present?
60
+ errors.add(:body, "Invalid variable: #{invalid.to_sentence}")
61
+ end
62
+ end
51
63
  end
52
64
 
53
65
  module ClassMethods
54
66
  def acts_as_email_notification?; true; end
55
67
  end
56
68
 
69
+ # To be overrided
70
+ def email_template
71
+ raise('to be implemented')
72
+ end
73
+
74
+ def email_template_variables
75
+ raise('to be implemented')
76
+ end
77
+
57
78
  def email_notification_params
58
79
  { from: from, cc: cc.presence, bcc: bcc.presence, subject: subject, body: body, content_type: content_type }
59
80
  end
@@ -74,9 +95,30 @@ module ActsAsEmailNotification
74
95
  body.present? && !(body.include?('</p>') || body.include?('</div>'))
75
96
  end
76
97
 
77
- # To be overrided
78
- def email_template
79
- nil
98
+ def email_notification_body_template
99
+ Liquid::Template.parse(body) rescue nil
100
+ end
101
+
102
+ def email_notification_subject_template
103
+ Liquid::Template.parse(subject) rescue nil
104
+ end
105
+
106
+ def email_notification_body_variables
107
+ template = email_notification_body_template()
108
+ return unless template.present?
109
+
110
+ Liquid::ParseTreeVisitor.for(template.root).add_callback_for(Liquid::VariableLookup) do |node|
111
+ [node.name, *node.lookups].join('.')
112
+ end.visit.flatten.uniq.compact
113
+ end
114
+
115
+ def email_notification_subject_variables
116
+ template = email_notification_subject_template()
117
+ return unless template.present?
118
+
119
+ Liquid::ParseTreeVisitor.for(template.root).add_callback_for(Liquid::VariableLookup) do |node|
120
+ [node.name, *node.lookups].join('.')
121
+ end.visit.flatten.uniq.compact
80
122
  end
81
123
 
82
124
  end
@@ -5,6 +5,7 @@
5
5
 
6
6
  - uid = effective_bootstrap_unique_id
7
7
  - expanded = form.object.cc.present? || form.object.bcc.present?
8
+ - mailer_subject_prefix_hint = EffectiveResources.mailer_subject_prefix_hint.to_s.strip.presence
8
9
 
9
10
  - if form.object.new_record? && form.object.errors.blank?
10
11
  .row
@@ -17,7 +18,7 @@
17
18
  = form.text_field :cc, label: 'CC', value: email_cc
18
19
  = form.text_field :bcc, label: 'BCC', value: email_bcc
19
20
 
20
- = form.text_field :subject, label: 'Subject', value: email_subject
21
+ = form.text_field :subject, value: email_subject, hint: (mailer_subject_prefix_hint ? "The subject of your email. It will be automatically prefixed with: #{mailer_subject_prefix_hint}" : 'The subject of your email')
21
22
 
22
23
  - if email_content_type == 'text/html'
23
24
  = form.article_editor :body, label: 'Body', mode: :email, value: email_body
@@ -35,7 +36,7 @@
35
36
  = form.text_field :cc, label: 'CC'
36
37
  = form.text_field :bcc, label: 'BCC'
37
38
 
38
- = form.text_field :subject, label: 'Subject'
39
+ = form.text_field :subject, hint: (mailer_subject_prefix_hint ? "The subject of your email. It will be automatically prefixed with: #{mailer_subject_prefix_hint}" : 'The subject of your email')
39
40
 
40
41
  - if email_content_type == 'text/html'
41
42
  = form.article_editor :body, label: 'Body', mode: :email
@@ -44,10 +45,9 @@
44
45
 
45
46
  - if email_variables.present?
46
47
  = card do
47
- %p The available variables are:
48
+ %p You can use the following variables in the subject and body:
48
49
 
49
50
  %ul
50
51
  - email_variables.each do |variable|
51
52
  %li {{ #{variable} }}
52
53
 
53
- %small.text-muted Please contact us to add additional variables
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '2.24.0'.freeze
2
+ VERSION = '2.25.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.24.0
4
+ version: 2.25.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-24 00:00:00.000000000 Z
11
+ date: 2024-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails