• Stars
    star
    364
  • Rank 117,101 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 11 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Making HTML emails comfortable for the Rails rockstars

roadie-rails

Build history and status Code Climate Code coverage status Gem Passive maintenance

⚠️ This gem is now in passive maintenance mode together with roadie. (more)

Making HTML emails comfortable for the Rails rockstars.

This gem hooks up your Rails application with Roadie to help you generate HTML emails.

Installation

Add this gem to your Gemfile as recommended by Rubygems and run bundle install.

gem 'roadie-rails', '~> 3.0'

Usage

roadie-rails have two primary means of usage. The first on is the "Automatic usage", which does almost everything automatically. It's the easiest way to hit the ground running in order to see if roadie would be a good fit for your application.

As soon as you require some more "advanced" features (congratulations!), you should migrate to the "Manual usage" which entails that you do some things by yourself.

Automatic usage

Include the Roadie::Rails::Automatic module inside your mailer. Roadie will do its magic when you try to deliver the message:

class NewsletterMailer < ActionMailer::Base
  include Roadie::Rails::Automatic

  def user_newsletter(user)
    mail to: user.email, subject: subject_for_user(user)
  end

  private
  def subject_for_user(user)
    I18n.translate 'emails.user_newsletter.subject', name: user.name
  end
end

# email has the original body; Roadie has not been invoked yet
email = NewsletterMailer.user_newsletter(User.first)

# This triggers Roadie inlining and will deliver the email with inlined styles
email.deliver

By overriding the #roadie_options method in the mailer you can disable inlining in certain cases:

class NewsletterMailer < ActionMailer::Base
  include Roadie::Rails::Automatic

  private
  def roadie_options
    super unless Rails.env.test?
  end
end

Another way:

describe YourMailer do
  describe "email contents" do
    before do
      # Disable inlining
      YourMailer.any_instance.stub(:roadie_options).and_return(nil)
    end
    # ...
  end

  describe "inlined email contents" do
    # ...
  end
end

If you need the extra flexibility, look at the "Manual usage" below.

Manual usage

Include the Roadie::Rails::Mailer module inside your ActionMailer and call roadie_mail with the same options that you would pass to mail.

class NewsletterMailer < ActionMailer::Base
  include Roadie::Rails::Mailer

  def user_newsletter(user)
    roadie_mail to: user.email, subject: subject_for_user(user)
  end

  private
  def subject_for_user(user)
    I18n.translate 'emails.user_newsletter.subject', name: user.name
  end
end

This will inline the stylesheets right away, which sadly decreases performance for your tests where you might only want to inline in one of them. The upside is that you can selectively inline yourself.

class NewsletterMailer < ActionMailer::Base
  include Roadie::Rails::Mailer

  def subscriber_newsletter(subscriber, options = {})
    use_roadie = options.fetch :use_roadie, true
    mail_factory(use_roadie, normal_mail_options)
  end

  private
  def mail_factory(use_roadie, options)
    if use_roadie
      roadie_mail options
    else
      mail options
    end
  end
end

# tests
describe NewsletterMailer do
  it "is emailed to the subscriber's email" do
    email = NewsletterMailer.subscriber_newsletter(subscriber, use_roadie: false)
    email.to.should == subscriber.email
  end

  it "inlines the emails by default" do
    email = NewsletterMailer.subscriber_newsletter(subscriber)
    email.should be_good_and_cool_and_all_that_jazz
  end
end

Or, perhaps by doing this:

describe YourMailer do
  describe "email contents" do
    before do
      # Redirect all roadie mail calls to the normal mail method
      YourMailer.any_instance.stub(:roadie_mail) { |*args, &block| YourMailer.mail(*args, &block) }
    end
    # ...
  end

  describe "inlined email contents" do
    # ...
  end
end

Configuration

Roadie can be configured in three places, depending on how specific you want to be:

  1. Rails.application.config.roadie (global, static).
  2. YourMailer#roadie_options (mailer, dynamic).
  3. Second argument to the roadie_mail (mail, specific and custom).

You can override at any level in the chain, depending on how specific you need to be.

Only the first two methods are available to you if you use the Automatic module.

# config/environments/production.rb
config.roadie.url_options = {host: "my-app.com", scheme: "https"}

# app/mailer/my_mailer.rb
class MyMailer
  include Roadie::Rails::Mailer

  protected
  def roadie_options
    super.merge(url_options: {host: Product.current.host})
  end
end

# app/mailer/my_other_mailer.rb
class MyOtherMailer
  include Roadie::Rails::Mailer

  def some_mail(user)
    roadie_mail {to: "[email protected]"}, roadie_options_for(user)
  end

  private
  def roadie_options_for(user)
    roadie_options.combine({
      asset_providers: [MyCustomProvider.new(user)],
      url_options: {host: user.subdomain_with_host},
    })
  end
end

If you #merge you will replace the older value outright:

def roadie_options
  original = super
  original.url_options # => {protocol: "https", host: "foo.com"}
  new = original.merge(url_options: {host: "bar.com"})
  new.url_options # => {host: "bar.com"}
  new
end

If you want to combine two values, use #combine. #combine is closer to Hash#deep_merge:

def roadie_options
  original = super
  original.url_options # => {protocol: "https", host: "foo.com"}
  new = original.combine(url_options: {host: "bar.com"})
  new.url_options # => {protocol: "https", host: "bar.com"}
  new
end

#combine is smarter than Hash#deep_merge, though. It can combine callback procs (so both get called) and Roadie::ProviderLists as well.

If you want to see the available configuration options, see the Roadie gem.

Templates

Use normal stylesheet_link_tag and foo_path methods when generating your email and Roadie will look for the precompiled files on your filesystem, or by asking the asset pipeline to compile the files for you if it cannot be found.

Previewing

You can create a controller that gets the email and then renders the body from it.

class Admin::EmailsController < AdminController
  def user_newsletter
    render_email NewsletterMailer.user_newsletter(current_user)
  end

  def subscriber_newsletter
    render_email NewsletterMailer.subscriber_newsletter(Subscriber.first || Subscriber.new)
  end

  private
  def render_email(email)
    respond_to do |format|
      format.html { render html: email.html_part.decoded.html_safe }
      format.text { render text: email.text_part.decoded }
    end
  end
end

Known issues

Roadie will not be able to find your stylesheets if you have an asset_host configured and will ignore those lines when inlining.

A workaround for this is to not use asset_host in your mailers:

config.action_controller.asset_host = # ...
config.action_mailer.asset_host = nil

# or

class MyMailer < ActionMailer::Base
  self.asset_host = nil
end

Build status

Tested with Github Actions on multiple Ruby and Rails versions:

  • Ruby:
    • MRI 2.6
    • MRI 2.7
    • MRI 3.0
    • MRI 3.1
  • Rails
    • 5.1
    • 5.2
    • 6.0
    • 6.1
    • 7.0

Please note that all Rails-versions are not compatible with all Ruby-versions. Let me know if you want any other combination supported officially.

Versioning

This project follows Semantic Versioning.

Documentation

Running specs

The default rake task will take care of the setup for you.

rake

After running rake for the first time and you want to keep running tests without having to install all dependencies, you may run guard, rspec or rake spec depending on what you prefer.

License

(The MIT License)

Copyright © 2013-2022 Magnus Bergmark [email protected], et. al.

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.

More Repositories

1

roadie

Making HTML emails comfortable for the Ruby rockstars
Ruby
1,342
star
2

rtl8192eu-linux-driver

Drivers for the rtl8192eu chipset for wireless adapters (D-Link DWA-131 rev E1 included!)
C
1,319
star
3

rofi-emoji

Emoji selector plugin for Rofi
C
557
star
4

mpris-rs

Idiomatic MPRIS D-Bus interface library for Rust
Rust
67
star
5

emoji-data

Machine-readable emoji data built from official Unicode reference files
Ruby
58
star
6

tydra

Shortcut menu-based task runner, inspired by Emacs Hydra
Rust
42
star
7

dotfiles

A collection of *NIX dotfiles for configuration
Lua
34
star
8

rofi-lpass

Lastpass integration with Rofi
Shell
19
star
9

DaysLeft

Small, simple android countdown widget
Java
9
star
10

prgnome

prgnome checks your PR so you don't accidentally merge something that isn't ready
Rust
5
star
11

googleprojection-rs

Project world- or screen-space coordinates for use in Google Maps tiles
Rust
4
star
12

swedish-pin-ruby

Validate, parse, and generate Swedish Personal Identity Numbers (PINs, "Personnummer")
Ruby
4
star
13

DBLite

A lightweight, K.I.S.S. graphical client for SQLite. Works on Mac, Linux and (probably) Windows!
C++
3
star
14

slack-tui

An experiment in trying to create a Slack application inside a terminal
Rust
3
star
15

cloudflare-dyndns-rs

Update a Cloudflare DNS record to the external IP of your network
Rust
2
star
16

miui

Quick and hacky image searcher in the terminal
Shell
2
star
17

clicketyclack

Play mechanical keyboard sounds while you type on your keyboard
Rust
2
star
18

mprisctl

CLI utility to control media players on Linux
Rust
2
star
19

cloudflare-dyndns-ruby

Supersceded by https://github.com/Mange/cloudflare-dyndns-rs
Ruby
2
star
20

autorandr

Maximize resolution and refresh rate of all connected monitors and order them in an automatic layout.
Rust
1
star
21

hehu_mf

CQB/mission framework for Arma 3 missions
SQF
1
star
22

git-trivia

WIP Git command to calculate fun (and useless) trivia about a given repo
Rust
1
star
23

i3bar-protocol

WIP Rust crate for i3bar (for i3 WM) protocol reading/writing
Rust
1
star
24

language-exploration

Trying out Rust and Go and comparing them with Ruby
Ruby
1
star
25

organ

Small app that move files around according to recipes
Ruby
1
star
26

graceful-shutdown

Terminate a list of processes and wait for them to exit.
Rust
1
star
27

hehu_tripflare

Trip-wire mines for Arma 3 which shoots flares instead of 'asploading you.
C++
1
star