rails_email_preview 0.0.9 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8bee2bfee317025f4b297f8982ae6322714d6854
4
- data.tar.gz: dd9b7bc1aa56a14dce7a96a55ee348b92e4ad3d8
3
+ metadata.gz: 37f26bca79a358d23b5558ccdf69ec9d14806c8c
4
+ data.tar.gz: 212af63e4f7389f0c7b10170ccd09c382fa1a0b9
5
5
  SHA512:
6
- metadata.gz: 35e24aa93dbb5ea4884b55a2764234b7e155074f3016f3fb4563c3c479dea01a6b0b197362b2ff39c5a294922203e093d48050e9e40008212a21f4a5d641a359
7
- data.tar.gz: 7206cf3f0c4fe7a629a88cb66a1709704b5f03d184f23bddf4bbc274cb3e15cf009fb39d4343489e7b906e912bb9ad79b2b0790feda7e4af6209a1be280e0060
6
+ metadata.gz: 1b52eef72bf07ada652564f93e1a068ae7ad5046c2fa927a6f47cebc96e8b567da4726ff124d6857c6c097b95970b710a1a8d68601b17e07ea18a07a5aaf1b0c
7
+ data.tar.gz: 8b9a369c78e56782055e007cdc1b732230207318b98d1e0b50e10536062b3f04611d64eac600028c3a8725faf4bdd42b7c201b22da8693f88b903971886a00ab
data/README.md CHANGED
@@ -3,8 +3,9 @@ Rails Email Preview
3
3
 
4
4
  A Rails Engine to preview plain text and html email in your browser. Compatible with Rails 3 and 4.
5
5
 
6
- ![screenshot](http://screencloud.net//img/screenshots/749d6c6a84b5d79b436ad627902944a8.png)
7
- *Preview UI*
6
+ ![screenshot](http://screencloud.net//img/screenshots/8861336ed60923429d3747e1fd379619.png)
7
+ ![screenshot](http://screencloud.net//img/screenshots/22aa58b651815068f4b0676754275c6a.png)
8
+ *Rendered within an application layout*
8
9
 
9
10
  How to
10
11
  -----
@@ -57,19 +58,12 @@ Configuration
57
58
  config.preview_classes = [ UserMailerPreview ]
58
59
  end
59
60
 
60
- # To render previews within layout other than the default one. NB: that layout must reference application urls via `main_app`, e.g. `main_app.login_url` (due to how isolated engines are in rails):
61
-
62
- Rails.application.config.to_prepare do
63
- RailsEmailPreview::ApplicationController.layout 'admin'
64
- end
65
-
66
-
67
61
  Premailer integration
68
62
  ---------------------
69
63
 
70
64
  [Premailer](https://github.com/alexdunae/premailer) automatically translates standard CSS rules into old-school inline styles. Integration can be done by using the <code>before_render</code> hook.
71
65
 
72
- To integrate Premailer with rails you can use either [actionmailer_inline_css](https://github.com/ndbroadbent/actionmailer_inline_css) or [premailer-rails](https://github.com/fphilipe/premailer-rails).
66
+ To integrate Premailer with your Rails app you can use either [actionmailer_inline_css](https://github.com/ndbroadbent/actionmailer_inline_css) or [premailer-rails](https://github.com/fphilipe/premailer-rails).
73
67
 
74
68
  For [actionmailer_inline_css](https://github.com/ndbroadbent/actionmailer_inline_css), add to `RailsEmailPreview.setup`:
75
69
 
@@ -83,24 +77,57 @@ Email editing
83
77
  -------------
84
78
 
85
79
  You can use [comfortable_mexican_sofa](https://github.com/comfy/comfortable-mexican-sofa) for storing and editing emails.
86
- See [ComfortableMexicanSofa integration guide](https://github.com/glebm/rails_email_preview/wiki/Edit-Emails-with-Comfortable-Mexican-Sofa).
87
-
88
- This is what it looks like:
80
+ REP comes with a CMS integration, see [ComfortableMexicanSofa integration guide](https://github.com/glebm/rails_email_preview/wiki/Edit-Emails-with-Comfortable-Mexican-Sofa).
89
81
 
90
- ![CMS integration screenshot](http://screencloud.net//img/screenshots/c3437edd8cdd52dbff58663a0b30d6ca.png)
82
+ ![CMS integration screenshot](http://screencloud.net//img/screenshots/b000595dbd13ae061373fd1473f113ba.png)
91
83
 
92
84
  I18n
93
85
  -------------
94
86
 
95
- Rails Email Preview sets `I18n.locale` for rendering an email.
96
- This means your emails should use I18n.locale but not set it.
97
- If you are using `Resque::Mailer` or `Devise::Async`, you will want to [inject I18n.locale into the job](https://gist.github.com/glebm/5725347).
87
+ Rails Email Preview expect emails to be rendered using `I18n.locale`:
88
+
89
+ # current locale
90
+ AccountMailer.some_notification.deliver
91
+ # different locale
92
+ I18n.with_locale('es') { InviteMailer.send_invites.deliver }
93
+
94
+
95
+ If you are using `Resque::Mailer` or `Devise::Async`, you can automatically add I18n.locale information when the mail job is scheduled
96
+ [with this initializer](https://gist.github.com/glebm/5725347).
98
97
 
99
98
 
100
- Customizing views
99
+ Views
101
100
  ---------------------
102
101
 
103
- You can override any `rails_email_preview` view by placing a file with the same path as in the gem in your project's `app/views`.
104
- You can also extend the `RailsEmailPreview::EmailsController` for further customization.
102
+ You can render all REP views inside your own layout:
103
+
104
+ Rails.application.config.to_prepare do
105
+ RailsEmailPreview::ApplicationController.layout 'admin'
106
+ end
107
+
108
+ When using a layout other than the default, that layout has to access all route helpers via `main_app`, e.g. `main_app.login_url`.
109
+ This is due to how [isolated engines](http://edgeapi.rubyonrails.org/classes/Rails/Engine.html#label-Isolated+Engine) work in Rails.
110
+
111
+ You can also override any individual view by placing a file with the same path in your project's `app/views`, e.g. `app/views/rails_email_preview/emails/index.html.slim`.
112
+
113
+ *Pull requests adding view hooks are welcome!*
114
+
115
+
116
+ Authentication & authorization
117
+ ------------------------------
118
+
119
+ To only allow certain users view emails add a before filter to `RailsEmailPreview::ApplicationController`, e.g.:
120
+
121
+ Rails.application.config.to_prepare do
122
+ RailsEmailPreview::ApplicationController.module_eval do
123
+ before_filter :check_permissions
124
+
125
+ private
126
+ def check_permissions
127
+ render status: 403 unless current_user.try(:admin?)
128
+ end
129
+ end
130
+ end
131
+
105
132
 
106
133
  This project rocks and uses MIT-LICENSE.
@@ -1,30 +1,32 @@
1
- #message_headers
1
+ #email-show
2
2
  padding: 10px 0 0 0
3
3
  margin: 0
4
- background: #fff
4
+ background: rgb(242, 236, 245)
5
5
  font-size: 12px
6
- font-family: "Lucida Grande"
7
- border-bottom: 1px solid #dedede
6
+ font-family: "Monaco", "Lucida Grande", sans-serif
7
+ border-bottom: 1px solid rgb(142, 137, 148)
8
8
  overflow: hidden
9
9
 
10
+ .pull-right
11
+ float: right
12
+
13
+ .btn-group
14
+ a
15
+ display: inline-block
16
+
10
17
  dl
11
18
  margin: 0
12
19
  padding: 0
13
20
 
14
-
15
21
  dt
16
22
  width: 60px
17
23
  padding: 1px
18
24
  float: left
19
25
  text-align: right
20
26
  font-weight: bold
21
- color: #7f7f7f
22
-
27
+ color: rgb(142, 137, 148)
23
28
 
24
29
  dd
25
30
  margin-left: 70px
26
31
  padding: 1px
27
32
 
28
-
29
- p.alternate
30
- float: right
@@ -1,7 +1,7 @@
1
1
  class RailsEmailPreview::EmailsController < RailsEmailPreview::ApplicationController
2
2
  include ERB::Util
3
3
  before_filter :load_preview_class, except: :index
4
- around_filter :with_email_locale, only: [:show, :show_raw]
4
+ before_filter :set_email_preview_locale
5
5
 
6
6
  # list screen
7
7
  def index
@@ -10,35 +10,39 @@ class RailsEmailPreview::EmailsController < RailsEmailPreview::ApplicationContro
10
10
 
11
11
  # preview screen
12
12
  def show
13
- @part_type = params[:part_type] || 'text/html'
14
- @mail = @preview_class.new.send(params[:mail_action])
13
+ I18n.with_locale @email_locale do
14
+ @part_type = params[:part_type] || 'text/html'
15
+ @mail = @preview_class.new.send(params[:mail_action])
16
+ end
17
+ render
15
18
  end
16
19
 
17
20
  # render actual email content
18
21
  def show_raw
19
- @mail = @preview_class.new.send(params[:mail_action])
20
- RailsEmailPreview.run_before_render(@mail)
21
- if @part_type == 'raw'
22
- body = "<pre id='raw_message'>#{html_escape(@mail.to_s)}</pre>"
23
- else
24
- if @mail.multipart?
25
- body_part = (@part_type =~ /html/ ? @mail.html_part : @mail.text_part)
22
+ I18n.with_locale @email_locale do
23
+ @mail = @preview_class.new.send(params[:mail_action])
24
+ RailsEmailPreview.run_before_render(@mail)
25
+ if @part_type == 'raw'
26
+ body = "<pre id='raw_message'>#{html_escape(@mail.to_s)}</pre>"
26
27
  else
27
- body_part = @mail
28
- end
29
- body = body_part.body
30
- if body_part.content_type =~ /plain/
31
- body = "<pre id='message_body'>#{body}</body>"
28
+ if @mail.multipart?
29
+ body_part = (@part_type =~ /html/ ? @mail.html_part : @mail.text_part)
30
+ else
31
+ body_part = @mail
32
+ end
33
+ body = body_part.body
34
+ if body_part.content_type =~ /plain/
35
+ body = "<pre id='message_body'>#{body}</body>"
36
+ end
32
37
  end
38
+ render text: body, layout: false
33
39
  end
34
- render text: body, layout: false
35
40
  end
36
41
 
37
42
  protected
38
43
 
39
- def with_email_locale(&block)
44
+ def set_email_preview_locale
40
45
  @email_locale = (params[:email_locale] || I18n.locale).to_s
41
- I18n.with_locale @email_locale, &block
42
46
  end
43
47
 
44
48
  private
@@ -0,0 +1,17 @@
1
+ -# Hide nav when rendering inside rails_email_preview
2
+ javascript:
3
+ jQuery(function($) {
4
+ if (window.parent && /email/.test(parent.location.href)) {
5
+ $('.left-column,.right-column').hide();
6
+ $('.center-column').css('margin', 0);
7
+ }
8
+ // Snippet form:
9
+ // * Label -> Subject
10
+ $('[name="snippet[label]"]').closest('.control-group').find('.control-label').text("Subject");
11
+ // * Content -> Body
12
+ $('[name="snippet[content]"]').closest('.control-group').find('.control-label').text("Body");
13
+ // * Hide identifier
14
+ $('[name="snippet[identifier]"]').closest('.control-group').hide();
15
+ // * Hide categories
16
+ $('label[for="snippet_"]').closest('.control-group').hide();
17
+ });
@@ -0,0 +1,16 @@
1
+ javascript:
2
+ function iFrameLoad(id){
3
+ var resize = function(id) {
4
+ var el = document.getElementById(id),
5
+ w = jQuery(el.parentNode).innerWidth(),
6
+ h = el.contentWindow.document.body.scrollHeight;
7
+ el.height = (h) + "px";
8
+ el.width = (w) + "px";
9
+ };
10
+ document.getElementById('loading-el').style.display = 'none';
11
+ setTimeout(function() { resize(id) });
12
+ }
13
+
14
+ pre#loading-el.lead #{icon 'spinner', 'spin'} Loading...
15
+ iframe#src-iframe[src=rails_email_preview.raw_email_url(params.slice(:mail_class, :mail_action, :part_type).merge(email_locale: @email_locale))
16
+ width="100%" height=1 onLoad="iFrameLoad('src-iframe')" style="border:none;padding:0;margin:0"]
@@ -0,0 +1,4 @@
1
+ dl
2
+ - { 'From' => @mail.from * ', ', 'To' => @mail.to * ', ', 'Subject' => @mail.subject}.each do |header, value|
3
+ dt #{header}
4
+ dd #{value}
@@ -0,0 +1,9 @@
1
+ p.btn-group
2
+ - { 'text/html' => 'HTML', 'text/plain' => 'Text', 'raw' => 'Raw'}.each do |mime, label|
3
+ a.btn.btn-link href=rails_email_preview.email_url(params.merge(part_type: mime)) class=('active' if @part_type == mime)
4
+ = label
5
+ br
6
+ p.btn-group
7
+ - I18n.available_locales.each do |locale|
8
+ a.btn.btn-link href=rails_email_preview.email_url(params.merge(part_type: @part_type, email_locale: locale)) class=('active' if @email_locale == locale.to_s)
9
+ = locale
@@ -6,5 +6,5 @@ ul.list-unstyled
6
6
  ul.list-group
7
7
  - m.constantize.instance_methods(false).map(&:to_s).each do |method|
8
8
  li.list-group-item
9
- a href=rails_email_preview.email_url(mail_class: m.to_s.underscore, mail_action: method.to_s)
9
+ a href=rails_email_preview.email_url(mail_class: m.to_s.underscore, mail_action: method.to_s, email_locale: @email_locale)
10
10
  = method.to_s.humanize
@@ -1,44 +1,11 @@
1
- a.btn.btn-link href=rails_email_preview.root_url &laquo; Back to list
1
+ a.btn.btn-link href=rails_email_preview.root_url(email_locale: @email_locale) &laquo; Back to list
2
2
 
3
- #message_headers
4
- .pull-right
5
- p.btn-group
6
- - { 'text/html' => 'HTML', 'text/plain' => 'Text', 'raw' => 'Raw'}.each do |mime, label|
7
- a.btn.btn-link href=rails_email_preview.email_url(params.merge(part_type: mime)) class=('active' if @part_type == mime) = label
8
- br
9
- p.btn-group
10
- - I18n.available_locales.each do |locale|
11
- a.btn.btn-link href=rails_email_preview.email_url(params.merge(part_type: @part_type, email_locale: locale)) class=('active' if @email_locale == locale.to_s) = locale
12
- dl
13
- dt From:
14
- dd= @mail.from * ', '
3
+ #email-show
4
+ -# email format and language
5
+ .pull-right = render 'rails_email_preview/emails/options'
15
6
 
16
- dt To:
17
- dd= @mail.to * ', '
7
+ -# from, to, subject
8
+ = render 'rails_email_preview/emails/headers'
18
9
 
19
- dt Subject:
20
- dd
21
- strong= @mail.subject
22
-
23
- javascript:
24
- function iFrameAutoResize(id){
25
- document.getElementById('loading-el').style.display = 'none';
26
- setTimeout(function(){
27
- var newheight;
28
- var newwidth;
29
-
30
- if(document.getElementById){
31
- newheight = document.getElementById(id).contentWindow.document.body.scrollHeight;
32
-
33
- newwidth = $(document.getElementById(id).parentNode).innerWidth();
34
- }
35
-
36
- var el = document.getElementById(id);
37
- el.height = (newheight) + "px";
38
- el.width = (newwidth) + "px";
39
- });
40
- }
41
-
42
- pre#loading-el.lead #{icon 'spinner', 'spin'} Loading...
43
- iframe#src-iframe[src=rails_email_preview.raw_email_url(params.slice(:mail_class, :mail_action, :part_type).merge(email_locale: @email_locale))
44
- width="100%" height=1 onLoad="iFrameAutoResize('src-iframe')" style="border:none;padding:0;margin:0"]
10
+ -# actual email content, rendered in an iframe to prevent browser styles from interfering
11
+ = render 'rails_email_preview/emails/email_iframe'
@@ -18,4 +18,19 @@ module RailsEmailPreview
18
18
  yield self
19
19
  end
20
20
  end
21
- end
21
+
22
+ # = Editing settings
23
+ # edit link is rendered inside an iframe, so these options are provided for simple styling
24
+ mattr_accessor :edit_link_text
25
+ self.edit_link_text = '✎ Edit Text'
26
+ mattr_accessor :edit_link_style
27
+ self.edit_link_style = <<-CSS.strip.gsub(/\n+/m, ' ')
28
+ display: block;
29
+ font-family: "Monaco", "Helvetica", sans-serif;
30
+ color: #7a4b8a;
31
+ border: 2px dashed #7a4b8a;
32
+ font-size: 20px;
33
+ padding: 8px 12px;
34
+ margin-top: 1em;
35
+ CSS
36
+ end
@@ -0,0 +1,86 @@
1
+ # simply require this file to enable Comfortable Mexican Sofa integration
2
+ # read more https://github.com/glebm/rails_email_preview/wiki/Edit-Emails-with-Comfortable-Mexican-Sofa
3
+
4
+ module RailsEmailPreview
5
+ module Integrations
6
+ module ComfortableMexicanSofa
7
+ # returns CMS identifier for the current email
8
+ # ModerationMailer#approve -> "moderation_mailer-approve"
9
+ def cms_email_id
10
+ mailer = respond_to?(:controller) ? controller : self
11
+ "#{mailer.class.name.underscore}-#{action_name}"
12
+ end
13
+
14
+ # Will return snippet title interpolated with passed variables
15
+ # E.g, for snippet with title "Welcome, %{name}!"
16
+ # cms_email_subject(name: "Alice") #=> "Welcome, Alice!"
17
+ def cms_email_subject(interpolation = {})
18
+ snippet_id = "email-#{cms_email_id}"
19
+ [I18n.locale, I18n.default_locale].compact.each do |locale|
20
+ site = Cms::Site.find_by_locale(I18n.locale)
21
+ snippet = site.snippets.find_by_identifier(snippet_id)
22
+
23
+ # interpolate even if keys/values are missing
24
+ title = snippet.label.to_s
25
+ interpolation = interpolation.stringify_keys
26
+ # set all missing values to ''
27
+ title.scan(/%{([^}]+)}/) { |key| interpolation[key] ||= ''}
28
+ # remove all missing keys
29
+ subject = title % interpolation.delete_if { |k, v| title !~ /%{#{k}}/ }
30
+
31
+ return subject if subject.present?
32
+ end
33
+ end
34
+
35
+ # Will return snippet content, passing through Kramdown
36
+ # Will also render an "✎ Edit text" link if used from
37
+ def cms_email_snippet(snippet_id = self.cms_email_id)
38
+ snippet_id = "email-#{snippet_id}"
39
+ site = Cms::Site.find_by_locale(I18n.locale)
40
+
41
+ # Fallback default locale: (# prefill)
42
+ unless (content = cms_snippet_content(snippet_id, site).presence)
43
+ default_site = Cms::Site.find_by_locale(I18n.default_locale)
44
+ fallback_content = cms_snippet_content(snippet_id, default_site).presence
45
+ end
46
+ result = content || fallback_content
47
+
48
+ # If rendering in preview from admin, add edit/create lnk
49
+ if caller.grep(/emails_controller/).present?
50
+ snippet = site.snippets.find_by_identifier(snippet_id)
51
+
52
+
53
+ cms_path = if snippet
54
+ unless content
55
+ fallback_snippet = default_site.snippets.find_by_identifier(snippet_id)
56
+ prefill_from_default = {label: fallback_snippet.label, content: fallback_snippet.content}
57
+ end
58
+ edit_cms_admin_site_snippet_path(site_id: site.id, id: snippet.id,
59
+ snippet: prefill_from_default || nil)
60
+ else
61
+ new_cms_admin_site_snippet_path(site_id: site.id, snippet: {
62
+ label: "#{snippet_id.sub('-', ' / ').humanize}",
63
+ identifier: snippet_id,
64
+ category_ids: [site.categories.find_by_label('email').try(:id)]
65
+ })
66
+ end
67
+
68
+ result += cms_edit_email_snippet_link(cms_path)
69
+ end
70
+ result
71
+ end
72
+
73
+ def cms_edit_email_snippet_link(path)
74
+ link_to(RailsEmailPreview.edit_link_text, path, style: RailsEmailPreview.edit_link_style, 'onClick' => '')
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ ActionMailer::Base.module_eval do
81
+ include ::RailsEmailPreview::Integrations::ComfortableMexicanSofa
82
+ helper ::RailsEmailPreview::Integrations::ComfortableMexicanSofa
83
+ end
84
+
85
+ require 'comfortable_mexican_sofa'
86
+ ComfortableMexicanSofa::ViewHooks.add :header, 'integrations/cms/customize_cms_for_rails_email_preview'
@@ -1,3 +1,3 @@
1
1
  module RailsEmailPreview
2
- VERSION = '0.0.9'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_email_preview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gleb Mazovetskiy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-07 00:00:00.000000000 Z
11
+ date: 2013-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -68,8 +68,7 @@ dependencies:
68
68
  version: '0'
69
69
  description: |-
70
70
  Implemented as a rails engine with a simple UI for previewing your app emails,
71
- I18n support, easy premailer integration and editing via comfortable_mexican_sofa
72
- (https://github.com/comfy/comfortable-mexican-sofa).
71
+ I18n support, easy premailer integration and editing integration via comfortable_mexican_sofa CMS.
73
72
  email: glex.spb@gmail.com
74
73
  executables: []
75
74
  extensions: []
@@ -78,10 +77,15 @@ files:
78
77
  - app/assets/stylesheets/rails_email_preview/application.css.sass
79
78
  - app/controllers/rails_email_preview/application_controller.rb
80
79
  - app/controllers/rails_email_preview/emails_controller.rb
80
+ - app/views/integrations/cms/_customize_cms_for_rails_email_preview.html.slim
81
81
  - app/views/layouts/rails_email_preview/application.html.slim
82
+ - app/views/rails_email_preview/emails/_email_iframe.html.slim
83
+ - app/views/rails_email_preview/emails/_headers.html.slim
84
+ - app/views/rails_email_preview/emails/_options.html.slim
82
85
  - app/views/rails_email_preview/emails/index.html.slim
83
86
  - app/views/rails_email_preview/emails/show.html.slim
84
87
  - lib/rails_email_preview/engine.rb
88
+ - lib/rails_email_preview/integrations/comfortable_mexica_sofa.rb
85
89
  - lib/rails_email_preview/version.rb
86
90
  - lib/rails_email_preview.rb
87
91
  - config/routes.rb