letter_bomb 0.0.1 → 0.0.2

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: 9c67ffe4932cf6ab7ad4afb2b846bbaac8750fa8
4
- data.tar.gz: 0d2ba7da06ca5b5fec46b5dbec6224fb864890bd
3
+ metadata.gz: ca88daac8b46e5920ae83fb0df5232d37deab93d
4
+ data.tar.gz: e01a4819e6c2717b49db1a82f7c34590b29d5804
5
5
  SHA512:
6
- metadata.gz: 2a178785f941f88ab127a964e421f6dac918484aaaa4b0e870ead025748ed1baa54ba62eccce731e81026bcd76d2699216dd5d532fca261be0b63b4d022848c9
7
- data.tar.gz: 62665b33af2ecbe0b38dd0f3002c0b0efe8e2b5d633f744fd859fa1801e5d77c0117e58209e7ba4c8909c95b477f448d5d0dc29d632112f4783fb750ad268eb9
6
+ metadata.gz: df503fcfd3a2406b40c15dc87780c24b8ae38c6c0ae575cf48fb0389ca9b086f1b3995240f000651a38defc82f5f40f19ecac5dcaec39f7b138dd61fdc01c2ea
7
+ data.tar.gz: b063d82333e4db87cd7560265921d64e3bfb3046b021f9d607b5ccd83de8e41b9e1a9d865ba5967bd6e9213efe484536046efbef8fb06d89e5e6ba87fb95c071
data/README.md CHANGED
@@ -25,7 +25,8 @@ mount LetterBomb::Engine, at: '/letter_bomb'
25
25
 
26
26
  and hit `/letter_bomb` for a list of previews.
27
27
 
28
- Previews can be defined anywhere within `app/mailers`.
28
+ Previews can be defined anywhere within `app/mailers` suffixed with `preview`, i.e. `app/mailers/user_mailer_preview.rb` or `app/mailers/user_mailer/preview.rb`.
29
+ Preview method names are arbitrary so long as they return a `Mail` object.
29
30
 
30
31
  ```ruby
31
32
  class UserMailer::Preview < LetterBomb::Preview
@@ -53,8 +54,6 @@ class UserMailer::Preview < LetterBomb::Preview
53
54
  end
54
55
  ```
55
56
 
56
- Preview class names and methods are arbitrary so long as they return a `Mail` object.
57
-
58
57
  Alternatives
59
58
  ------------
60
59
 
@@ -1,13 +1,39 @@
1
1
  module LetterBomb
2
2
  class MailersController < ApplicationController
3
+
3
4
  def index
4
5
  @mailers = LetterBomb::Preview.previews
5
6
  end
6
7
 
7
8
  def show
8
- mailer_class = params[:mailer_class]
9
- mailer_action = params[:mailer_action]
10
- @mail = mailer_class.constantize.preview_action(mailer_action)
9
+ klass = params[:mailer_class]
10
+ @action = params[:mailer_action]
11
+ @mail = klass.constantize.preview_action(@action)
12
+
13
+ respond_to do |format|
14
+ format.html
15
+ format.text { render formats: [:html], content_type: 'text/html' }
16
+ end
11
17
  end
18
+
19
+ private
20
+
21
+ def body_part
22
+ return @mail unless @mail.multipart?
23
+
24
+ content_type = Rack::Mime.mime_type(".#{params[:format]}")
25
+ if @mail.respond_to?(:all_parts)
26
+ @mail.all_parts.find { |part| part.content_type.match(content_type) } || @mail.parts.first
27
+ else
28
+ @mail.parts.find { |part| part.content_type.match(content_type) } || @mail.parts.first
29
+ end
30
+ end
31
+ helper_method :body_part
32
+
33
+ def html_template?
34
+ params[:format] == 'html'
35
+ end
36
+ helper_method :html_template?
37
+
12
38
  end
13
39
  end
@@ -1,11 +1,11 @@
1
- <h1>Mailers</h1>
1
+ <h1>Mailer Previews</h1>
2
2
 
3
3
  <% @mailers.each do |mailer| %>
4
4
  <p><%= mailer.to_s %></p>
5
5
 
6
6
  <ul>
7
7
  <% mailer.mailer_actions.each do |action| %>
8
- <li><%= link_to action, mailer_path(mailer.to_s, action) %></li>
8
+ <li><%= link_to action, mailer_path(mailer.to_s, action, format: :html) %></li>
9
9
  <% end %>
10
10
  </ul>
11
11
 
@@ -75,14 +75,21 @@
75
75
 
76
76
  <dt>To:</dt>
77
77
  <dd><%= @mail.to %></dd>
78
-
79
- <dt>Content-Type:</dt>
80
- <dd><%= @mail.content_type %></dd>
81
78
  </dl>
79
+
80
+ <% if @mail.multipart? %>
81
+ <p class="alternate">
82
+ <% if html_template? %>
83
+ <a href="<%= @action %>.txt">View plain text version</a>
84
+ <% else %>
85
+ <a href="<%= @action %>.html">View HTML version</a>
86
+ <% end %>
87
+ </p>
88
+ <% end %>
82
89
  </div>
83
90
 
84
- <% if @mail.content_type && @mail.content_type.match(/text\/html/) %>
85
- <%= @mail.body.to_s.html_safe %>
91
+ <% if html_template? %>
92
+ <%= body_part.body.to_s.html_safe %>
86
93
  <% else %>
87
- <pre id="message_body"><%= @mail.body %></pre>
94
+ <pre id="message_body"><%= body_part.body %></pre>
88
95
  <% end %>
@@ -1,17 +1,5 @@
1
1
  module LetterBomb
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace LetterBomb
4
-
5
- initializer 'letterbomb.mailers.autoload' do |app|
6
- # TODO this is sloppy. the reason behind it is:
7
- # 1) to generate the list of previews, we need all subclasses.
8
- # rails doesn't load them up front, so we need to do that manually.
9
- #
10
- # 2) because we load the mailers and previews up-front, we have to manually tell
11
- # rails to reload each per request.
12
- ActionDispatch::Callbacks.before do
13
- Dir[File.join(app.config.root, "/app/mailers/**/*.rb")].each { |file| load(file) }
14
- end
15
- end
16
4
  end
17
5
  end
@@ -2,11 +2,15 @@ module LetterBomb
2
2
  class Preview
3
3
 
4
4
  def self.previews
5
- subclasses
5
+ load_dir = Rails.application.root.join('app/mailers').to_s
6
+ Dir.glob(load_dir + '/**/*preview.rb').map do |filename|
7
+ # app/mailers/module/class.rb => module/class.rb => module/class => Module::Class
8
+ lchomp(filename, load_dir).chomp('.rb').camelize.constantize
9
+ end
6
10
  end
7
11
 
8
12
  def self.mailer_actions
9
- new.public_methods(false).map(&:to_s).sort
13
+ public_instance_methods(false).map(&:to_s).sort
10
14
  end
11
15
 
12
16
  def self.preview_action(mailer_action)
@@ -29,5 +33,9 @@ module LetterBomb
29
33
  end
30
34
  end
31
35
 
36
+ def self.lchomp(base, arg)
37
+ base.to_s.reverse.chomp(arg.to_s.reverse).reverse
38
+ end
39
+
32
40
  end
33
41
  end
@@ -1,3 +1,3 @@
1
1
  module LetterBomb
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
@@ -0,0 +1,12 @@
1
+ class FooMailerPreview < LetterBomb::Preview
2
+ def good
3
+ User.create!(name: 'bob')
4
+ 'result of good'
5
+ end
6
+
7
+ def bad; end
8
+
9
+ private
10
+
11
+ def ugly; end
12
+ end
@@ -0,0 +1,6 @@
1
+ class UserMailer::Preview < LetterBomb::Preview
2
+ def welcome
3
+ user = User.create!(name: 'john')
4
+ UserMailer.welcome(user.name)
5
+ end
6
+ end
@@ -4,18 +4,6 @@ class UserMailer < ActionMailer::Base
4
4
 
5
5
  def welcome(var)
6
6
  @var = var
7
- mail(subject: 'welcome to the show!')
8
- end
9
- end
10
-
11
- class UserMailer::Preview < LetterBomb::Preview
12
- def welcome
13
- user = User.create!(name: 'john')
14
- UserMailer.welcome(user.name)
15
- end
16
- end
17
-
18
- class UserMailer::PreviewAgaian < LetterBomb::Preview
19
- def foo
7
+ mail(subject: 'welcome to the show!')
20
8
  end
21
9
  end
@@ -1,3 +1,3 @@
1
1
  <h1>welcome!</h1>
2
2
 
3
- <p>a mailer var: <%= @var %></p>
3
+ <p style="color: red">a mailer var: <%= @var %></p>
@@ -0,0 +1,3 @@
1
+ welcome!
2
+
3
+ a mailer var: <%= @var %>
Binary file
Binary file