action_mailer_auto_previews 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 52bad65f9a1b0bca6857aa3b2dbb75f0c3395394
4
+ data.tar.gz: c9965341bf8952cdeb4790dc6531489d683bb048
5
+ SHA512:
6
+ metadata.gz: 7c284d8ff304d7b77e9c463b84c171674f3933661ba9baf73412545303feec2919624f344f3deab9b2376f324ac9a77dd2fb535dbe4ceca83f0d6f655c5396cb
7
+ data.tar.gz: 312c392ea209c157da007b6589c5e43ba884080cac51ff34f59a02cb82e85ccbedd899f6808d749584652413382f98cc34e0a286d15d7cdfec897d5ed4729bf4
@@ -0,0 +1,2 @@
1
+ lib/action_mailer_auto_previews.rb 0e0dfb0d8eb7fecfc1b4ef19108844a138fdbd73
2
+ lib/action_mailer_auto_previews/version.rb 7ff771be0445f700b37db11ae255f4114960eb34
Binary file
Binary file
Binary file
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in action_mailer_auto_previews.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Matthew B. Jones
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # ActionMailerAutoPreviews
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/action_mailer_auto_previews`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile :development group:
10
+
11
+ ```ruby
12
+ group :development do
13
+ gem 'action_mailer_auto_previews'
14
+ end
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install action_mailer_auto_previews
24
+
25
+ ## Usage
26
+
27
+ That's it! Simply include the gem in your Gemfile's :development group, and by default, you will be able to automatically see previews of your ActionMailer emails. Whenever `deliver` or `deliver_later`
28
+ is called on one of your ActionMailers, the gem will automatically generate the ActionMailer::Preview required and open the preview in your default browser. However, there are several configuration options
29
+ that can be used to alter this behavior.
30
+
31
+ ## Configuration Options
32
+ ```ruby
33
+ ActionMailerAutoPreviews.setup do |config|
34
+ config.enabled = true
35
+ config.history_limit = 10
36
+ config.intercept_mode = :preview_only # [:preview_only, :preview_and_deliver]
37
+ config.launch_browser = true
38
+ config.logger = Rails.logger
39
+ config.preview_host_url = "http://localhost:3000"
40
+ end
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/makifund/action_mailer_auto_previews.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
51
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'action_mailer_auto_previews/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "action_mailer_auto_previews"
8
+ spec.version = ActionMailerAutoPreviews::VERSION
9
+ spec.authors = ["Matthew B. Jones", "Andrew Yi"]
10
+ spec.email = ["matt@makifund.com", "andrew@makifund.com"]
11
+
12
+ spec.summary = %q{
13
+ Enhances the ActionMailer Previews introduced in 4.1 by automatically creating ActionMailer Previews at runtime in development mode.
14
+ }
15
+ spec.description = %q{
16
+ Enhances the ActionMailer Previews introduced in 4.1 by automatically creating ActionMailer Previews at runtime in development mode.
17
+ See automatic previews of your ActionMailer emails, with no extra effort or mock data. Install the action_mailer_auto_previews gem
18
+ into your :development group, and it'll 'just work' with sensible defaults. Each ActionMailer email object that has .deliver or
19
+ .deliver_later called will automatically launch your default browser right to a ActionMailer Preview page with the real data
20
+ passed to that email. Flexible options allow you to alter this behavior as well.
21
+
22
+ Warning: Since this is dynamically creating Ruby classes/methods, you will want to make sure your web-server is single threaded.
23
+ For example, if you're using Puma, be sure to set the `workers` configuration parameter to 1.
24
+ }
25
+ spec.homepage = "https://github.com/makifund/action_mailer_auto_previews"
26
+ spec.license = "MIT"
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.11"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+
37
+ spec.add_dependency "actionmailer", "~> 4.1"
38
+ spec.add_dependency "launchy", "~> 2.4.3"
39
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "action_mailer_auto_previews"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,107 @@
1
+ require "action_mailer_auto_previews/version"
2
+ require "launchy"
3
+
4
+ # This gem uses sensible defaults, but can be altered as follows:
5
+ # @example
6
+ # ActionMailerAutoPreviews.setup do |config|
7
+ # config.enabled = true
8
+ # config.history_limit = 10
9
+ # config.intercept_mode = :preview_only
10
+ # config.launch_browser = true
11
+ # config.logger = Rails.logger
12
+ # config.preview_host_url = "http://localhost:3000"
13
+ # end
14
+ module ActionMailerAutoPreviews
15
+ # This is a cache that holds onto the Mail objects for viewing via the ActionMailer::Preview web pages. This
16
+ # cache will grow up the max capacity defined in the :history_limit attribute.
17
+ MAIL_CACHE = {}
18
+
19
+ mattr_accessor :enabled
20
+ # Whether or not this gem and its functionality should be enabled
21
+ @@enabled = Rails.env.development?
22
+
23
+ mattr_accessor :history_limit
24
+ # The maximum number of emails to keep around in MAIL_CACHE
25
+ @@history_limit = 25
26
+
27
+ mattr_accessor :intercept_mode
28
+ # Whether to generate the Preview only (:preview_only), or, to generate the Preview and also do the default Mailer behavior
29
+ @@intercept_mode = :preview_only
30
+
31
+ mattr_accessor :launch_browser
32
+ # Automatically launch the Preview in the default browser, otherwise, logs the new Preview URL to the logger
33
+ @@launch_browser = true
34
+
35
+ mattr_accessor :logger
36
+ # Logger to be used. When nil (default), will use Rails.logger if Rails is present, otherwise, defaults to Logger.new(STDOUT)
37
+ @@logger = nil
38
+
39
+ mattr_accessor :preview_host_url
40
+ # Base URL that is used for the Preview URLs. This should be set to the hostname and port of your development server.
41
+ @@preview_host_url = "http://localhost:3000"
42
+
43
+ # Call with a block to override one or more of the default configuration options.
44
+ # @example
45
+ # ActionMailerAutoPreviews.setup do |config|
46
+ # config.launch_browser = false
47
+ # config.history_limit = 10
48
+ # end
49
+ def self.setup
50
+ yield self
51
+ end
52
+
53
+ private
54
+ # Logic to return the appropriate logger to be used by this gem
55
+ def self.logger
56
+ @@logger ||= Object.const_defined?('Rails') ? Rails.logger : Logger.new(STDOUT)
57
+ end
58
+
59
+ # This method is the magic sauce of what we're trying to achieve. It is called via the patch on ActionMailer::MessageDelivery
60
+ def self.auto_preview(message_delivery)
61
+ # Generate a unique preview key
62
+ preview_key = "preview_#{Time.now.to_i}#{rand(1000)}"
63
+
64
+ # Store the message object that contains the data for the preview
65
+ ActionMailerAutoPreviews::MAIL_CACHE[preview_key] = message_delivery
66
+
67
+ # Dynamically add a ActionMailer::Preview method, named after `preview_key`
68
+ eval %Q(
69
+ class ActionMailerAutoPreviews::AutomaticPreviewMailer < ActionMailer::Preview
70
+ define_method("#{preview_key}") do
71
+ ActionMailerAutoPreviews::MAIL_CACHE["#{preview_key}"]
72
+ end
73
+ end
74
+ )
75
+
76
+ # Launch the default browser to the newly-available ActionMailer::Preview
77
+ preview_url = "#{ActionMailerAutoPreviews.preview_host_url}/rails/mailers/action_mailer_auto_previews/automatic_preview_mailer/#{preview_key}"
78
+ ActionMailerAutoPreviews.launch_browser ? Launchy.open(preview_url) : ActionMailerAutoPreviews.logger.info("New ActionMailer::Preview available at #{preview_url}")
79
+
80
+ # Clean up the cache if the number of generated previews now exceeds `history_limit`
81
+ if ActionMailerAutoPreviews::MAIL_CACHE.count > ActionMailerAutoPreviews.history_limit
82
+ preview_key_to_purge = ActionMailerAutoPreviews::MAIL_CACHE.keys.first # Get the oldest preview_key
83
+ ActionMailerAutoPreviews::MAIL_CACHE.delete preview_key_to_purge # Delete the reference to the mail object
84
+ eval %Q(
85
+ class ActionMailerAutoPreviews::AutomaticPreviewMailer < ActionMailer::Preview
86
+ remove_method("#{preview_key_to_purge}")
87
+ end
88
+ ) # Removes the ActionMailer::Preview method, so the Preview is no longer available and the old URL will stop functioning
89
+ ActionMailerAutoPreviews.logger.info("ActionMailer::Preview #{preview_key_to_purge} has been purged, history buffer is full")
90
+ end
91
+ end
92
+
93
+ # Check to see if we should be enabled or not, and if so, patch into ActionMailer
94
+ if ActionMailerAutoPreviews.enabled
95
+ class ActionMailer::MessageDelivery
96
+ def deliver(options={})
97
+ ActionMailerAutoPreviews.auto_preview(self)
98
+ super(options) if ActionMailerAutoPreviews.intercept_mode == :preview_and_deliver
99
+ end
100
+
101
+ def deliver_later(options={})
102
+ ActionMailerAutoPreviews.auto_preview(self)
103
+ super(options) if ActionMailerAutoPreviews.intercept_mode == :preview_and_deliver
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,4 @@
1
+ module ActionMailerAutoPreviews
2
+ # @private
3
+ VERSION = "0.1.0"
4
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_mailer_auto_previews
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew B. Jones
8
+ - Andrew Yi
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-04-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.11'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.11'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: actionmailer
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '4.1'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '4.1'
70
+ - !ruby/object:Gem::Dependency
71
+ name: launchy
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 2.4.3
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 2.4.3
84
+ description: "\n Enhances the ActionMailer Previews introduced in 4.1 by automatically
85
+ creating ActionMailer Previews at runtime in development mode.\n See automatic
86
+ previews of your ActionMailer emails, with no extra effort or mock data. Install
87
+ the action_mailer_auto_previews gem\n into your :development group, and it'll
88
+ 'just work' with sensible defaults. Each ActionMailer email object that has .deliver
89
+ or\n .deliver_later called will automatically launch your default browser right
90
+ to a ActionMailer Preview page with the real data\n passed to that email. Flexible
91
+ options allow you to alter this behavior as well.\n\n Warning: Since this is
92
+ dynamically creating Ruby classes/methods, you will want to make sure your web-server
93
+ is single threaded.\n For example, if you're using Puma, be sure to set the `workers`
94
+ configuration parameter to 1.\n "
95
+ email:
96
+ - matt@makifund.com
97
+ - andrew@makifund.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - ".yardoc/checksums"
103
+ - ".yardoc/object_types"
104
+ - ".yardoc/objects/root.dat"
105
+ - ".yardoc/proxy_types"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - action_mailer_auto_previews.gemspec
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/action_mailer_auto_previews.rb
114
+ - lib/action_mailer_auto_previews/version.rb
115
+ homepage: https://github.com/makifund/action_mailer_auto_previews
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.4.5.1
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Enhances the ActionMailer Previews introduced in 4.1 by automatically creating
139
+ ActionMailer Previews at runtime in development mode.
140
+ test_files: []
141
+ has_rdoc: