Permalink
Browse files

Version 0.0.1

  • Loading branch information...
0 parents commit ae3ba581ceebbe4f94eb4235b9f277b9de61447f @glebm committed Jan 30, 2012
8 .gitignore
@@ -0,0 +1,8 @@
+.idea/
+.rvmrc
+.bundle/
+log/*.log
+pkg/
+test/dummy/db/*.sqlite3
+test/dummy/log/*.log
+test/dummy/tmp/
6 Gemfile
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+gemspec
+
+gem "capybara", ">= 0.4.0"
+gem "sqlite3"
118 Gemfile.lock
@@ -0,0 +1,118 @@
+PATH
+ remote: .
+ specs:
+ rails_email_preview (0.0.1)
+ activesupport (~> 3.2)
+ haml
+ rails (~> 3.2)
+ sass
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.2.1)
+ actionpack (= 3.2.1)
+ mail (~> 2.4.0)
+ actionpack (3.2.1)
+ activemodel (= 3.2.1)
+ activesupport (= 3.2.1)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.1)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.1)
+ activesupport (= 3.2.1)
+ builder (~> 3.0.0)
+ activerecord (3.2.1)
+ activemodel (= 3.2.1)
+ activesupport (= 3.2.1)
+ arel (~> 3.0.0)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.1)
+ activemodel (= 3.2.1)
+ activesupport (= 3.2.1)
+ activesupport (3.2.1)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.0)
+ builder (3.0.0)
+ capybara (1.1.2)
+ mime-types (>= 1.16)
+ nokogiri (>= 1.3.3)
+ rack (>= 1.0.0)
+ rack-test (>= 0.5.4)
+ selenium-webdriver (~> 2.0)
+ xpath (~> 0.1.4)
+ childprocess (0.3.0)
+ ffi (~> 1.0.6)
+ erubis (2.7.0)
+ ffi (1.0.11)
+ haml (3.1.4)
+ hike (1.2.1)
+ i18n (0.6.0)
+ journey (1.0.1)
+ json (1.6.5)
+ mail (2.4.1)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.17.2)
+ multi_json (1.0.4)
+ nokogiri (1.5.0)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.1)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.2.1)
+ actionmailer (= 3.2.1)
+ actionpack (= 3.2.1)
+ activerecord (= 3.2.1)
+ activeresource (= 3.2.1)
+ activesupport (= 3.2.1)
+ bundler (~> 1.0)
+ railties (= 3.2.1)
+ railties (3.2.1)
+ actionpack (= 3.2.1)
+ activesupport (= 3.2.1)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ rubyzip (0.9.5)
+ sass (3.1.12)
+ selenium-webdriver (2.18.0)
+ childprocess (>= 0.2.5)
+ ffi (~> 1.0.9)
+ multi_json (~> 1.0.4)
+ rubyzip
+ sprockets (2.1.2)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.5)
+ thor (0.14.6)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.31)
+ xpath (0.1.4)
+ nokogiri (~> 1.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ capybara (>= 0.4.0)
+ rails_email_preview!
+ sqlite3
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright 2012 YOURNAME
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
77 README.md
@@ -0,0 +1,77 @@
+RailsEmailPreview -- Visual email testing
+================================
+
+Preview plain text and html mail templates in your browser without redelivering it every time you make a change.
+
+Inspired by MailView: https://github.com/37signals/mail_view/
+However, implemented as a Rails engine, with layouts and multiple mailers support.
+
+Compatible with Rails version >= 3 only.
+
+Usage
+-----
+
+Since most emails do something interesting with database data, you'll need to write some scenarios to load messages with fake data. Its similar to writing mailer unit tests but you see a visual representation of the output instead.
+
+ class Notifier < ActionMailer::Base
+ def invitation(inviter, invitee)
+ # ...
+ end
+
+ def welcome(user)
+ # ...
+ end
+
+ class Preview
+ # Pull data from existing fixtures
+ def invitation
+ account = Account.first
+ inviter, invitee = account.users[0, 2]
+ Notifier.invitation(inviter, invitee)
+ end
+
+ # Factory-like pattern
+ def welcome
+ user = User.create!
+ mail = Notifier.welcome(user)
+ user.destory
+ mail
+ end
+ end
+ end
+
+The methods must return a Mail object.
+
+Since this is a Rails engine, it can be configured in an initializer:
+
+In config/initializers/rails_email_preview.rb
+
+ require 'rails_email_preview'
+ RailsEmailPreview.setup do |config|
+ config.preview_classes = [ ListingMessageMailer::Preview ]
+ end
+
+ # If you want to render it within the application layout, uncomment the following lines:
+ # Rails.application.config.to_prepare do
+ # RailsEmailPreview::ApplicationController.layout "application"
+ # end
+ # Note that if you do use it with your main_app layout, all the main_app URLs must be generated explicitly
+ # E.g. you would need to change links like login_url to main_app.login_url
+
+
+Routing is very simple
+
+In config/routes.rb add:
+
+ if Rails.env.development?
+ mount RailsEmailPreview::Engine, :at => 'email_preview' # You can choose any URL here
+ end
+
+To get the url of RailsEmailPreview in your main app you can call rails_email_preview.root_url
+
+Interface
+---------
+
+ToDo: Add a screenshot
+
+This project rocks and uses MIT-LICENSE.
29 Rakefile
@@ -0,0 +1,29 @@
+# encoding: UTF-8
+require 'rubygems'
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+
+require 'rake'
+require 'rake/rdoctask'
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+task :default => :test
+
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'RailsEmailPreview'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
38 app/assets/stylesheets/rails_email_preview/application.css.sass
@@ -0,0 +1,38 @@
+#message_headers
+ height: 85px
+ padding: 10px 0 0 0
+ margin: 0
+ background: #fff
+ font-size: 12px
+ font-family: "Lucida Grande"
+ border-bottom: 1px solid #dedede
+ overflow: hidden
+
+ dl
+ margin: 0
+ padding: 0
+
+
+ dt
+ width: 60px
+ padding: 1px
+ float: left
+ text-align: right
+ font-weight: bold
+ color: #7f7f7f
+
+
+ dd
+ margin-left: 70px
+ padding: 1px
+
+
+ p.alternate
+ position: absolute
+ top: 0
+ right: 15px
+
+
+ p.alternate a
+ color: #09c
+
5 app/controllers/rails_email_preview/application_controller.rb
@@ -0,0 +1,5 @@
+module RailsEmailPreview
+ class ApplicationController < ::ApplicationController
+
+ end
+end
29 app/controllers/rails_email_preview/emails_controller.rb
@@ -0,0 +1,29 @@
+class RailsEmailPreview::EmailsController < RailsEmailPreview::ApplicationController
+ before_filter :load_preview_class, :except => :index
+ def index
+ @preview_classes = (RailsEmailPreview.preview_classes || [])
+ end
+
+ def show
+ @mail = @preview_class.new.send(params[:mail_action])
+ end
+
+ def show_raw
+ @mail = @preview_class.new.send(params[:mail_action])
+ body_part = @mail
+ if @mail.multipart?
+ content_type = Rack::Mime.mime_type(params[:format])
+ body_part = @mail.parts.find { |part| part.content_type.match(content_type) } || @mail.parts.first
+ end
+ body = body_part.body
+ if body_part.content_type =~ /plain/
+ body = "<pre id='message_body'>#{body}</body>"
+ end
+ render :text => body, :layout => false
+ end
+
+ private
+ def load_preview_class
+ @preview_class = (RailsEmailPreview.preview_classes || []).find {|pc| pc.name.underscore == params[:mail_class]}
+ end
+end
9 app/views/layouts/rails_email_preview/application.html.haml
@@ -0,0 +1,9 @@
+!!! HTML
+%html
+ %head
+ %title Email Preview
+ = stylesheet_link_tag 'rails_email_preview/application'
+ = yield(:head)
+ %body
+ #content
+ = yield
9 app/views/rails_email_preview/emails/index.html.haml
@@ -0,0 +1,9 @@
+%h1 Available Mailers
+%ul
+ - @preview_classes.each do |m|
+ %li
+ %h2= m.name
+ %ul
+ - m.instance_methods(false).map(&:to_s).each do |method|
+ %li
+ %a{:href => rails_email_preview.email_url(:mail_class => m.name.underscore, :mail_action => method)}= method
33 app/views/rails_email_preview/emails/show.html.haml
@@ -0,0 +1,33 @@
+%a#back-to-all{:href => rails_email_preview.root_url} &laquo; Back to list
+#message_headers
+ %dl
+ %dt From:
+ %dd= @mail.from
+
+ %dt Subject:
+ %dd
+ %strong= @mail.subject
+
+ %dt Date:
+ %dd= Time.now.strftime("%b %e, %Y %I:%M:%S %p %Z")
+
+ %dt To:
+ %dd= @mail.to
+
+ - if @mail.multipart?
+ %p.alternate
+ - if body_part.content_type && body_part.content_type.match(/text\/html/)
+ %a{:href => rails_email_preview.email_url(params.merge(:format => 'txt'))}} View plain text version
+ - else
+ %a{:href => rails_email_preview.email_url(params.merge(:format => 'html'))}} View HTML version
+
+
+%iframe#src-iframe{:src => rails_email_preview.raw_email_url(params),
+ :width => "100%", :height => "800", :onload => 'emailLoaded()', :style => "display:none"}
+%pre#loading Loading...
+
+:javascript
+ window.emailLoaded = function () {
+ document.getElementById('src-iframe').style.display = 'block';
+ document.getElementById('loading').style.display = 'none';
+ };
13 config/routes.rb
@@ -0,0 +1,13 @@
+RailsEmailPreview::Engine.routes.draw do
+ root :to => 'emails#index'
+ match 'raw/:mail_class/:mail_action(.:format)',
+ :as => :raw_email,
+ :to => 'emails#show_raw',
+ :defaults => {:format => 'html'},
+ :mail_class => %r([\w/]+)
+ match ':mail_class/:mail_action(.:format)',
+ :as => :email,
+ :to => 'emails#show',
+ :defaults => {:format => 'html'},
+ :mail_class => %r([\w/]+)
+end
9 lib/rails_email_preview.rb
@@ -0,0 +1,9 @@
+require 'rails_email_preview/engine'
+
+module RailsEmailPreview
+ mattr_accessor :preview_classes
+
+ def self.setup
+ yield self
+ end
+end
13 lib/rails_email_preview/engine.rb
@@ -0,0 +1,13 @@
+module ::RailsEmailPreview
+ class Engine < Rails::Engine
+ isolate_namespace RailsEmailPreview
+
+ class << self
+ attr_accessor :root
+ def root
+ @root ||= Pathname.new(File.expand_path('../../', __FILE__))
+ end
+ end
+
+ end
+end
3 lib/rails_email_preview/version.rb
@@ -0,0 +1,3 @@
+module RailsEmailPreview
+ VERSION = '0.0.1'
+end
19 rails_email_preview.gemspec
@@ -0,0 +1,19 @@
+require File.expand_path("../lib/rails_email_preview/version", __FILE__)
+
+
+Gem::Specification.new do |s|
+ s.name = "rails_email_preview"
+ s.author = 'Gleb Mazovetskiy'
+ s.email = 'glex.spb@gmail.com'
+ s.summary = "Visual Email Preview for Rails >= 3, implemented as a Rails Engine. v#{RailsEmailPreview::VERSION}"
+ s.description = "Insert RailsEmailPreview description."
+
+ s.files = Dir["{app,lib,config}/**/*"] + ["MIT-LICENSE", "Rakefile", "Gemfile", "README.rdoc"]
+
+ s.add_dependency "activesupport", "~> 3.2"
+ s.add_dependency "rails", "~> 3.2"
+ s.add_dependency "haml"
+ s.add_dependency "sass"
+
+ s.version = RailsEmailPreview::VERSION
+end

0 comments on commit ae3ba58

Please sign in to comment.