immosquare-extensions 0.1.20 → 0.1.22

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: 839917dfe2c067bc70d57463de526a06881912487b894157636314507ddc6f80
4
- data.tar.gz: 2976c5d03bdbf5459a8a31ff56958e59ebd309642fc37936c414e1b5cdedf854
3
+ metadata.gz: 9340872ccbc9a67247ee5e5d07144909b38a2a521267b5a0610cc9ed8125c08e
4
+ data.tar.gz: bb68bbc53a56035518e9ccd4e2a8c4b121b5180d0183e03fd85226fe6aebad05
5
5
  SHA512:
6
- metadata.gz: 0ab25383ed25488126a7618dfa478e0dcb0892041558b186671a1c891b7738903070ccd2e434bd07fb32be38f2957eb2c0365017dcaf078cb78d1aa7c3d006ee
7
- data.tar.gz: 98910fa8d9d62bf352af1add06273134f86ab59b21e352eac5d48e9ea9dca189052a0998aa442e11f6054b0b316ed1b3519da568d730a499f53a523817adafde
6
+ metadata.gz: f4390440477c636fae97533afd2d4a6a63d4decf195a565172c907379946ab91aec6052a461cafa70928b701df32a6560b1729656a3a470d9f72985e72a2b1b0
7
+ data.tar.gz: f13ddf10e5ba8d02d238e43fa42cc98a239bb747d37c0990c16af787a647eb3d52308122e8c16af379dfac214c74ee2ec1a74f193b7ada055f481e0638881035
@@ -1,5 +1,5 @@
1
1
  module ImmosquareExtensions
2
- module MailHelper
2
+ module PreviewMailerHelper
3
3
  ##============================================================##
4
4
  ## Inspired by:
5
5
  ## https://github.com/rails/rails/blob/main/railties/lib/rails/templates/rails/mailers/email.html.erb
@@ -12,12 +12,13 @@ module ImmosquareExtensions
12
12
  :layout => false,
13
13
  :locals => {
14
14
  :email => email,
15
- :formats => get_email_formats(email).map do |format|
16
- name = format.split("/").last
17
- {:name => name, :url => url_for(:part => name)}
18
- end,
15
+ :formats => get_email_formats(email),
19
16
  :locales => I18n.available_locales.map do |locale|
20
- {:name => locale, :url => url_for(:locale => locale)}
17
+ {
18
+ :name => locale,
19
+ :url => url_for(:mailer_locale => locale, :part => params[:part].presence),
20
+ :css_class => (params[:mailer_locale].present? && params[:mailer_locale] == locale.to_s) || (params[:mailer_locale].blank? && I18n.locale == locale) ? "" : "text-body"
21
+ }
21
22
  end
22
23
  },
23
24
  :inline => <<~HTML
@@ -82,7 +83,7 @@ module ImmosquareExtensions
82
83
  <th>Formats:</th>
83
84
  <td>
84
85
  <% formats.each do |format| %>
85
- <%= link_to(format[:name], format[:url]) %>
86
+ <%= link_to(format[:name], format[:url], :class => "text-decoration-none hover-primary" + " " + format[:css_class]) %>
86
87
  <%= content_tag(:span, " | ") unless format == formats.last %>
87
88
  <% end %>
88
89
  </td>
@@ -93,7 +94,8 @@ module ImmosquareExtensions
93
94
  <th>Locale:</th>
94
95
  <td>
95
96
  <% locales.each do |locale| %>
96
- <%= link_to(locale[:name], locale[:url]) %>
97
+ <%= link_to(locale[:name], locale[:url], :class => "text-decoration-none hover-primary" + " " + locale[:css_class]) %>
98
+ <%= content_tag(:span, " | ") unless locale == locales.last %>
97
99
  <% end %>
98
100
  </td>
99
101
  </tr>
@@ -137,11 +139,14 @@ module ImmosquareExtensions
137
139
 
138
140
  def display_email_preview(email)
139
141
  begin
140
- availabe_formats = get_email_formats(email)
141
- part_format = params[:part].present? ? availabe_formats.find {|format| format.end_with?(params[:part]) } : availabe_formats.first
142
- part_format = availabe_formats.first if part_format.nil?
143
- parts = get_email_parts(email)
144
- parts.find {|part| part[:content_type] == part_format }[:body]
142
+ formats = get_email_formats(email)
143
+ part_format = formats.find {|f| f[:selected] }
144
+ part_type = Mime::Type.lookup(part_format[:content_type])
145
+
146
+ raise("No email part found for content type: #{part_format[:content_type]}") if !(part = find_part(email, part_type))
147
+
148
+ body = part.respond_to?(:decoded) ? part.decoded : part
149
+ part_format[:name] == "html" ? body.html_safe : simple_format(body)
145
150
  rescue StandardError => e
146
151
  "Error displaying email: #{e.message}"
147
152
  end
@@ -151,57 +156,52 @@ module ImmosquareExtensions
151
156
 
152
157
 
153
158
  ##============================================================##
154
- ## Return email formats
155
- ## ["text/plain", "text/html"]
156
- ## ["text/html"]
157
- ## ["text/plain"]
159
+ ## find_first_mime_type is a method from the Mail gem for module
160
+ ## Mail Message
161
+ ## ----------------
162
+ ## https://github.com/mikel/mail/blob/master/lib/mail/message.rb#L1938
158
163
  ##============================================================##
159
- def get_email_formats(email)
160
- content_types = email.parts.map(&:content_type)
161
- content_types_allowed = ["text/html", "text/plain"]
162
- if !email.multipart? || content_types.empty?
163
- if email.header_fields.nil? || email.header_fields.empty?
164
- ["text/plain"]
165
- else
166
- header = email.header_fields.find {|header| header.name == "Content-Type" }
167
- [header.nil? || !content_types_allowed.include?(header.value) ? "text/plain" : header.value]
168
- end
169
- else
170
- content_types.map do |content_type|
171
- content_type = content_type.split(";").first
172
- content_types_allowed.include?(content_type) ? content_type : nil
173
- end
174
- .compact.sort_by do |content_type|
175
- content_types_allowed.index(content_type)
164
+ def find_preferred_part(email, *formats)
165
+ formats.each do |format|
166
+ if (part = email.find_first_mime_type(format))
167
+ return part
176
168
  end
177
169
  end
170
+
171
+ email if formats.any? {|f| email.mime_type == f }
178
172
  end
179
173
 
180
174
  ##============================================================##
181
- ## Return email parts with content type and body
175
+ ## find_part use the find_first_mime_type method from the Mail
176
+ ## gem for module Mail Message
182
177
  ##============================================================##
183
- def get_email_parts(email)
184
- parts_type = email.parts.map(&:content_type)
185
- if !email.multipart? || parts_type.empty?
186
- if email.header_fields.nil? || email.header_fields.empty?
187
- content_type = "text/plain"
188
- else
189
- header = email.header_fields.find {|header| header.name == "Content-Type" }
190
- content_type = header.nil? ? "text/plain" : header.value
191
- end
192
- [{
193
- :content_type => content_type,
194
- :body => content_type == "text/plain" ? simple_format(email.body.decoded) : email.body.decoded
195
- }]
196
- else
197
- email.parts.map do |part|
198
- content_type = part.content_type.split(";").first
199
- {
200
- :content_type => content_type,
201
- :body => content_type == "text/plain" ? simple_format(part.body.decoded) : part.body.decoded
202
- }
203
- end
178
+ def find_part(email, format)
179
+ if (part = email.find_first_mime_type(format))
180
+ part
181
+ elsif email.mime_type == format
182
+ email
204
183
  end
205
184
  end
185
+
186
+ ##============================================================##
187
+ ## Return email formats
188
+ ##============================================================##
189
+ def get_email_formats(email)
190
+ part = find_preferred_part(email, Mime[:html], Mime[:text])
191
+ formats = []
192
+ formats =
193
+ if email.html_part && email.text_part
194
+ selected = params[:part].present? && params[:part] == "text" ? "text" : "html"
195
+ [
196
+ {:name => "html", :content_type => "text/html", :url => url_for(:part => "html", :mailer_locale => params[:mailer_locale].presence), :selected => selected == "html", :css_class => selected == "html" ? "" : "text-body"},
197
+ {:name => "text", :content_type => "text/plain", :url => url_for(:part => "text", :mailer_locale => params[:mailer_locale].presence), :selected => selected == "text", :css_class => selected == "text" ? "" : "text-body"}
198
+ ]
199
+ elsif part
200
+ data = part.content_type.split(";").first.split("/").last
201
+ [
202
+ {:name => data, :content_type => "text/#{data}", :url => url_for(:part => data, :mailer_locale => params[:mailer_locale].presence), :selected => true, :css_class => ""}
203
+ ]
204
+ end
205
+ end
206
206
  end
207
207
  end
@@ -1,3 +1,3 @@
1
1
  module ImmosquareExtensions
2
- VERSION = "0.1.20".freeze
2
+ VERSION = "0.1.22".freeze
3
3
  end
@@ -3,7 +3,7 @@ require_relative "immosquare-extensions/array"
3
3
  require_relative "immosquare-extensions/file"
4
4
  require_relative "immosquare-extensions/hash"
5
5
  require_relative "immosquare-extensions/string"
6
- require_relative "immosquare-extensions/helpers/mail_helper"
6
+ require_relative "immosquare-extensions/helpers/preview_mailer_helper"
7
7
  require_relative "immosquare-extensions/railtie" if defined?(Rails)
8
8
 
9
9
  module ImmosquareExtensions
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.20
4
+ version: 0.1.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - IMMO SQUARE
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-06 00:00:00.000000000 Z
11
+ date: 2024-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unicode_utils
@@ -38,7 +38,7 @@ files:
38
38
  - lib/immosquare-extensions/array.rb
39
39
  - lib/immosquare-extensions/file.rb
40
40
  - lib/immosquare-extensions/hash.rb
41
- - lib/immosquare-extensions/helpers/mail_helper.rb
41
+ - lib/immosquare-extensions/helpers/preview_mailer_helper.rb
42
42
  - lib/immosquare-extensions/railtie.rb
43
43
  - lib/immosquare-extensions/shared_methods.rb
44
44
  - lib/immosquare-extensions/string.rb