rails_email_preview 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: 316441f9fa1877b4421bc4ebe7fbaf4a0dc5f325
4
- data.tar.gz: 3c3b9c876ae226366428581572419530652b6e29
3
+ metadata.gz: b46366e1472a3041b34ca170b5cc29825c9f5ed1
4
+ data.tar.gz: 8ba5c7bacfdfaef09c530d75672ff78f70690f11
5
5
  SHA512:
6
- metadata.gz: c85362bd7e84b577695377cde1bdb78c54f88103180d8f934f54be3f43fc7aed93b79784da79bd53025d875b780519c282db037c96973a99d311d1af3fd0e442
7
- data.tar.gz: 9061e1ea98c9b98a3e5f7d177f367629c139254bad43b26509e24c2caa7f5b90630dd5ffc4d26b52cee22a977f485c740086158a017dc4277e0ce85387954b0d
6
+ metadata.gz: 87929034726a606ffa0d438c3f91f99a4a89dbf760487d2c140e98d18fd46bb988f3cb4642012755612db68138706dd6dc4af7ab0f4ec1811ae9c524eba63233
7
+ data.tar.gz: beee657162ebe47a1e601e236fe12ff8f3c142136385c4a17b62a3fccc824ed18e76a3c1854a9754b976d36481a414e7f967a0ab9d8b501e0e0b17800233ebba
data/README.md CHANGED
@@ -6,7 +6,7 @@ A Rails Engine to preview plain text and html email in your browser. Compatible
6
6
  ![screenshot][rep-show-screenshot]
7
7
  ![screenshot][rep-nav-screenshot]
8
8
 
9
- *(styles are from the application)*
9
+ *styles are provided by the application, above is [Zuigo](http://zuigo.com/), a platform to organize & discover events*
10
10
 
11
11
  How to
12
12
  -----
@@ -63,6 +63,15 @@ You can access REP urls like this:
63
63
  # email show:
64
64
  rails_email_preview.rep_email_url(mail_class: "user_mailer", mail_action: "welcome")
65
65
 
66
+ Sending test emails
67
+ -------------------
68
+
69
+ You can send emails via REP. This is especially useful when testing with limited clients (Blackberry, Outlook, etc.).
70
+ This will use the environment's mailer settings, but the handler will `perform_deliveries`.
71
+ Uncomment this line in the initializer to disable sending test emails:
72
+
73
+ config.enable_send_email = false
74
+
66
75
  Email editing
67
76
  -------------
68
77
 
@@ -5,7 +5,7 @@ class RailsEmailPreview::EmailsController < ::RailsEmailPreview::ApplicationCont
5
5
 
6
6
  # list screen
7
7
  def index
8
- @preview_class_names = (RailsEmailPreview.preview_classes || []).map { |klass| klass.is_a?(String) ? klass : klass.name }
8
+ @preview_class_names = (RailsEmailPreview.preview_classes || []).map { |klass| klass.is_a?(String) ? klass : klass.name }.sort
9
9
  end
10
10
 
11
11
  # preview screen
@@ -13,25 +13,18 @@ class RailsEmailPreview::EmailsController < ::RailsEmailPreview::ApplicationCont
13
13
  I18n.with_locale @email_locale do
14
14
  @part_type = params[:part_type] || 'text/html'
15
15
  if @preview_class.respond_to?(:new)
16
- @mail = @preview_class.new.send(params[:mail_action])
16
+ @mail = preview_mail
17
17
  else
18
- # @preview_class is not a preview class
19
- arg_info = "#{@preview_class}"
20
- if @preview_class.is_a?(Module)
21
- chain = @preview_class.ancestors.map(&:to_s)
22
- chain = chain[1 .. chain.index { |c| c =~ /^Application[A-Z][A-z]+$/ } || -1]
23
- end
24
- raise ArgumentError.new("#{arg_info} is not a preview class #{"(ancestors: #{chain * ' < '})"} ")
18
+ raise ArgumentError.new("#{@preview_class} is not a preview class, does not respond_to?(:new)")
25
19
  end
26
20
  end
27
- render
28
21
  end
29
22
 
30
23
  # render actual email content
31
24
  def show_raw
32
25
  I18n.with_locale @email_locale do
33
- @mail = @preview_class.new.send(params[:mail_action])
34
- RailsEmailPreview.run_before_render(@mail)
26
+ @mail = preview_mail
27
+ RailsEmailPreview.run_before_render(@mail, @preview_class.name, @mail_action)
35
28
  if @part_type == 'raw'
36
29
  body = "<pre id='raw_message'>#{html_escape(@mail.to_s)}</pre>"
37
30
  else
@@ -49,8 +42,39 @@ class RailsEmailPreview::EmailsController < ::RailsEmailPreview::ApplicationCont
49
42
  end
50
43
  end
51
44
 
45
+ def test_deliver
46
+ redirect_url = rep_email_url(params.slice(:mail_class, :mail_action, :email_locale))
47
+ if (address = params[:recipient_email]).blank? || address !~ /@/
48
+ redirect_to redirect_url, alert: t('rep.test_deliver.provide_email')
49
+ return
50
+ end
51
+ if !(delivery_method = Rails.application.config.action_mailer.delivery_method)
52
+ redirect_to redirect_url, alert: t('rep.test_deliver.no_delivery_method', environment: Rails.env)
53
+ return
54
+ end
55
+ I18n.with_locale @email_locale do
56
+ delivery_handler = RailsEmailPreview::DeliveryHandler.new(preview_mail, to: address, cc: nil, bcc: nil)
57
+ deliver_email!(delivery_handler.mail)
58
+
59
+ redirect_to redirect_url, notice: t('rep.test_deliver.sent_notice', address: address, delivery_method: delivery_method)
60
+ end
61
+ end
62
+
52
63
  protected
53
64
 
65
+ def deliver_email!(mail)
66
+ # support deliver! if present
67
+ if mail.respond_to?(:deliver!)
68
+ mail.deliver!
69
+ else
70
+ mail.deliver
71
+ end
72
+ end
73
+
74
+ def preview_mail
75
+ @preview_class.new.send(@mail_action)
76
+ end
77
+
54
78
  def set_email_preview_locale
55
79
  @email_locale = (params[:email_locale] || I18n.default_locale).to_s
56
80
  end
@@ -62,6 +86,7 @@ class RailsEmailPreview::EmailsController < ::RailsEmailPreview::ApplicationCont
62
86
  (pc.is_a?(String) ? pc : pc.name).underscore == params[:mail_class]
63
87
  }
64
88
  @preview_class = @preview_class.constantize if @preview_class.is_a?(String)
65
- @part_type = params[:part_type] || 'text/html'
89
+ @mail_action = params[:mail_action]
90
+ @part_type = params[:part_type] || 'text/html'
66
91
  end
67
92
  end
@@ -6,4 +6,22 @@ module RailsEmailPreview::EmailsHelper
6
6
  value.to_s
7
7
  end
8
8
  end
9
+
10
+ def email_methods(m)
11
+ m.constantize.instance_methods(false).map(&:to_s).sort
12
+ end
13
+
14
+ def split_in_halves(elements, &weight)
15
+ tot_w = elements.map(&weight).sum
16
+ cols = [elements.dup, []]
17
+ col2_w = 0
18
+ cols[0].reverse_each do |cl|
19
+ n = weight.call(cl)
20
+ break if col2_w + n > tot_w / 2
21
+ col2_w += n
22
+ cols[1] << cols[0].pop
23
+ end
24
+ cols[1].reverse!
25
+ cols
26
+ end
9
27
  end
@@ -1,15 +1,17 @@
1
1
  ruby:
2
- headers = {
3
- 'From' => @mail.from,
4
- 'To' => @mail.to,
5
- 'Reply to' => @mail.reply_to,
6
- 'CC' => @mail.cc,
7
- 'BCC' => @mail.bcc,
8
- 'Subject' => @mail.subject || '(no subject)'
9
- }.delete_if { |k, v| v.blank? }
2
+ headers = I18n.with_locale @email_locale do
3
+ {
4
+ 'Subject' => @mail.subject || '(no subject)',
5
+ 'From' => @mail.from,
6
+ 'Reply to' => @mail.reply_to,
7
+ 'To' => @mail.to,
8
+ 'CC' => @mail.cc,
9
+ 'BCC' => @mail.bcc
10
+ }.delete_if { |k, v| v.blank? }
11
+ end
10
12
 
11
13
 
12
- dl
14
+ dl.inline-dl
13
15
  - headers.each do |header, value|
14
- dt #{header}
15
- dd #{format_header(value)}
16
+ dt = header
17
+ dd = format_header(value)
@@ -0,0 +1,6 @@
1
+ div style="max-width: 250px"
2
+ = form_tag rails_email_preview.rep_test_deliver_url, method: :post do
3
+ = hidden_field_tag :email_locale, @email_locale
4
+ .input-group
5
+ span.input-group-btn: button.btn.btn-danger type='submit' data-confirm=t('rep.send_are_you_sure') #{t('rep.send_btn')}
6
+ = text_field_tag :recipient_email, '', class: 'form-control', placeholder: t('rep.send_recipient_placeholder')
@@ -0,0 +1,13 @@
1
+ .btn-toolbar.pull-right
2
+ .btn-group
3
+ - I18n.available_locales.each do |locale|
4
+ a.btn href=rails_email_preview.rep_email_url(params.merge(part_type: @part_type, email_locale: locale)) class=('active' if @email_locale == locale.to_s)
5
+ = locale
6
+
7
+ .btn-group
8
+ - { 'text/html' => 'HTML', 'text/plain' => 'Text', 'raw' => 'Raw'}.each do |mime, label|
9
+ a.btn href=rails_email_preview.rep_email_url(params.merge(part_type: mime)) class=('active' if @part_type == mime)
10
+ = label
11
+
12
+ - if RailsEmailPreview.enable_send_email
13
+ .btn-group= render 'rails_email_preview/emails/send_form'
@@ -1,10 +1,16 @@
1
- h1 Application emails
2
- ul.list-unstyled
3
- - @preview_class_names.each do |m|
4
- li.col-lg-6.col-sm-6
5
- h2= m.underscore.sub(/(_mailer)?_preview$/, '').humanize
6
- ul.list-group
7
- - m.constantize.instance_methods(false).map(&:to_s).each do |method|
8
- li.list-group-item
9
- a href=rails_email_preview.rep_email_url(mail_class: m.to_s.underscore, mail_action: method.to_s, email_locale: @email_locale)
10
- = method.to_s.humanize
1
+ h1 #{t 'rep.list_title'}
2
+
3
+ - previews_group = proc do |m|
4
+ - capture do
5
+ h2= m.underscore.sub(/(_mailer)?_preview$/, '').humanize
6
+ ul.list-group
7
+ - email_methods(m).each do |method|
8
+ li.list-group-item
9
+ a href=rails_email_preview.rep_email_url(mail_class: m.to_s.underscore, mail_action: method.to_s, email_locale: @email_locale)
10
+ = method.to_s.humanize
11
+
12
+ .row
13
+ - split_in_halves(@preview_class_names) { |c| email_methods(c).length } .each do |classes|
14
+ .col-sm-6
15
+ - classes.each do |class_name|
16
+ = previews_group.call(class_name)
@@ -1,11 +1,14 @@
1
- a.btn.btn-link href=rails_email_preview.rep_emails_url(email_locale: @email_locale) &laquo; Back to list
1
+ .row
2
+ .pull-left
3
+ a.btn.btn-link href=rails_email_preview.rep_emails_url(email_locale: @email_locale) « #{t 'rep.back_to_list'}
4
+ .margin-right= render 'rails_email_preview/emails/toolbar'
2
5
 
3
- #email-show
4
- -# email format and language
5
- .pull-right = render 'rails_email_preview/emails/options'
6
-
7
- -# from, to, subject
6
+ #email-show: .row
7
+ -# From, To, Subject and other
8
8
  = render 'rails_email_preview/emails/headers'
9
+ -# Locale, format and actions
9
10
 
10
11
  -# actual email content, rendered in an iframe to prevent browser styles from interfering
11
- = render 'rails_email_preview/emails/email_iframe'
12
+ .row
13
+ .margin-right
14
+ = render 'rails_email_preview/emails/email_iframe'
@@ -1,18 +1,26 @@
1
1
  require 'rails_email_preview'
2
2
 
3
- #= Premailer
3
+ #= REP hooks and config
4
+ #RailsEmailPreview.setup do |config|
4
5
  #
5
- # # process preview with premailer-rails:
6
- # RailsEmailPreview.before_render { |message| Premailer::Rails::Hook.delivering_email(message) }
7
- # # process preview with actionmailer-inline-css:
8
- # RailsEmailPreview.before_render { |message| ActionMailer::InlineCssHook.delivering_email(message) }
6
+ # # hook before rendering preview:
7
+ # config.before_render do |message, preview_class_name, mailer_action|
8
+ # # apply premailer-rails:
9
+ # Premailer::Rails::Hook.delivering_email(message)
10
+ # # or actionmailer-inline-css:
11
+ # ActionMailer::InlineCssHook.delivering_email(message)
12
+ # end
13
+ #
14
+ # # do not show Send Email button
15
+ # config.enable_send_email = false
16
+ #end
9
17
 
10
- #= Comfortable Mexican Sofa
18
+ #= REP + Comfortable Mexican Sofa integration
11
19
  #
12
20
  # # enable comfortable_mexican_sofa integration:
13
21
  # require 'rails_email_preview/integrations/comfortable_mexica_sofa'
14
22
 
15
- #= Application-specific
23
+ #= Application-specific (layout, permissions, customizations)
16
24
  #
17
25
  # # You can specify a controller for RailsEmailPreview::ApplicationController to inherit from:
18
26
  # RailsEmailPreview.parent_controller = 'Admin::ApplicationController' # default: '::ApplicationController'
@@ -0,0 +1,12 @@
1
+ en:
2
+ rep:
3
+ test_deliver:
4
+ no_delivery_method: Please set "config.action_mailer.delivery_method" to send emails in "%{environment}" environment
5
+ provide_email: Send to which address?
6
+ sent_notice: "Sent to %{address} via %{delivery_method}"
7
+ list_title: Application Emails
8
+ back_to_list: Back to list
9
+ send_recipient_placeholder: Email
10
+ send_btn: Send to
11
+ send_are_you_sure: This will actually send this email. Are you sure?
12
+
data/config/routes.rb CHANGED
@@ -3,6 +3,7 @@ RailsEmailPreview::Engine.routes.draw do
3
3
 
4
4
  get 'raw/:mail_class/:mail_action' => 'emails#show_raw', as: :rep_raw_email
5
5
  get ':mail_class/:mail_action' => 'emails#show', as: :rep_email
6
+ post 'test_deliver/:mail_class/:mail_action' => 'emails#test_deliver', as: :rep_test_deliver
6
7
 
7
8
  # define alias for the root url as part of stable api
8
9
  get '/' => 'emails#index', as: :rep_root
@@ -2,6 +2,7 @@
2
2
  require 'rails_email_preview/engine'
3
3
  require 'rails_email_preview/main_app_route_delegator'
4
4
  require 'rails_email_preview/version'
5
+ require 'rails_email_preview/delivery_handler'
5
6
 
6
7
  require 'slim'
7
8
  require 'slim-rails'
@@ -14,10 +15,14 @@ module RailsEmailPreview
14
15
  # auto-loading configured in initializer
15
16
  mattr_accessor :preview_classes
16
17
 
18
+ # send email button (experimental, false by default)
19
+ mattr_accessor :enable_send_email
20
+ self.enable_send_email = true
21
+
17
22
  class << self
18
- def run_before_render(mail)
23
+ def run_before_render(mail, preview_class_name, mailer_action)
19
24
  (defined?(@hooks) && @hooks[:before_render] || []).each do |block|
20
- block.call(mail)
25
+ block.call(mail, preview_class_name, mailer_action)
21
26
  end
22
27
  end
23
28
 
@@ -0,0 +1,15 @@
1
+ module RailsEmailPreview
2
+ class DeliveryHandler
3
+ def initialize(mail, headers)
4
+ @mail = mail
5
+ @mail.headers(headers)
6
+ @mail.delivery_handler = self
7
+ end
8
+
9
+ attr_accessor :mail
10
+
11
+ def deliver_mail(mail)
12
+ yield
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  # simply require this file to enable Comfortable Mexican Sofa integration
2
3
  # read more https://github.com/glebm/rails_email_preview/wiki/Edit-Emails-with-Comfortable-Mexican-Sofa
3
4
 
@@ -48,7 +49,7 @@ module RailsEmailPreview
48
49
  result = (content || fallback_content).to_s
49
50
 
50
51
  # If rendering in preview from admin, add edit/create lnk
51
- if caller.grep(/emails_controller/).present?
52
+ if caller.grep(/rails_email_preview\/emails_controller/).present? && !caller.grep(/rails_email_preview\/delivery_handler/).present?
52
53
  snippet = site.snippets.find_by_identifier(snippet_id)
53
54
 
54
55
 
@@ -67,11 +68,10 @@ module RailsEmailPreview
67
68
  })
68
69
  end
69
70
 
70
- result = safe_join [result, "\n\n",
71
- "<table><tr><td>".html_safe,
71
+ result = safe_join ["<table class='rep-edit-link'><tr><td>".html_safe,
72
72
  cms_edit_email_snippet_link(cms_path),
73
73
  "</td></tr></table>".html_safe,
74
- "\n"]
74
+ "\n\n", result]
75
75
  end
76
76
  result
77
77
  end
@@ -1,3 +1,3 @@
1
1
  module RailsEmailPreview
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: rails_email_preview
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
  - Gleb Mazovetskiy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-18 00:00:00.000000000 Z
11
+ date: 2013-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -110,17 +110,20 @@ files:
110
110
  - app/views/layouts/rails_email_preview/application.html.slim
111
111
  - app/views/rails_email_preview/emails/_email_iframe.html.slim
112
112
  - app/views/rails_email_preview/emails/_headers.html.slim
113
- - app/views/rails_email_preview/emails/_options.html.slim
113
+ - app/views/rails_email_preview/emails/_send_form.html.slim
114
+ - app/views/rails_email_preview/emails/_toolbar.html.slim
114
115
  - app/views/rails_email_preview/emails/index.html.slim
115
116
  - app/views/rails_email_preview/emails/show.html.slim
116
117
  - lib/generators/rails_email_preview/install_generator.rb
117
118
  - lib/generators/rails_email_preview/update_previews_generator.rb
119
+ - lib/rails_email_preview/delivery_handler.rb
118
120
  - lib/rails_email_preview/engine.rb
119
121
  - lib/rails_email_preview/integrations/comfortable_mexica_sofa.rb
120
122
  - lib/rails_email_preview/main_app_route_delegator.rb
121
123
  - lib/rails_email_preview/version.rb
122
124
  - lib/rails_email_preview.rb
123
125
  - config/initializers/rails_email_preview.rb
126
+ - config/locales/rep.en.yml
124
127
  - config/routes.rb
125
128
  - MIT-LICENSE
126
129
  - Rakefile
@@ -1,9 +0,0 @@
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.rep_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.rep_email_url(params.merge(part_type: @part_type, email_locale: locale)) class=('active' if @email_locale == locale.to_s)
9
- = locale