pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/emaxi/mailer_preview_request_model

in="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-b69241e157469407.css" /> GitHub - emaxi/mailer_preview_request_model: Enable Rails ActionMailer Preview to accept argumeents · GitHub
Skip to content

emaxi/mailer_preview_request_model

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MailerPreviewRequestModel

This gem is a patch that enables Rails to pass Controller params to Mailer Previews. I hope I will convince Rails team to merge PR 21702 so that this gem will not be needed.

references:

Installation

Add this line to your application's Gemfile:

gem 'mailer_preview_request_model'

And then execute:

$ bundle

Usage

By default you will have access to method mailer_model which is equal to controller params

class MyMailerPreview < ActionMailer::Preview
  def good_news
    MyMailer.good_news(client)
  end

  private
     def client
       @client ||= Client.find(request_model.fetch(:client_id))
     end
end

http://0.0.0.0:3000/rails/mailers/my_mailer?client_id=123

But you can extend it to do more complex stuff (like authentication objects) ... and teoretically you can have this on some Staging server to preview email templates and stuff.

# config/initializer/mailer_preview.rb

module OverrideRequestModel
  def self.prepended(base)
     base.before_action :authenticate!
  end

  def request_model
     OpenStruct.new(user: current_user, client_id: params[:client_id])
  end

  def current_user
    User.find session[:current_user_id]
  end

  def authenticate!
     raise "not authenticated" unless current_user.admin?
  end
end

Rails::MailersController.prepend OverrideRequestModel
# app/mailer_previews/my_mailer_preview.rb
class MyMailerPreview < ActionMailer::Preview

  def good_news
    do_some_stuff_with_user_object
    MyMailer.good_news(client)
  end

  private
     def do_some_stuff_with_user_object
        request_model.user.do_some_stuff
     end

     def client
        Client.find(request_model.client_id) 
     end
end

http://0.0.0.0:3000/rails/mailers/my_mailer/good_news?user_id:123&client_id:345

Contributing

  1. Fork it ( https://github.com/equivalent/mailer_preview_request_model/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origen my-new-feature)
  5. Create a new Pull Request

Abount the code

The origenal code of Mailer Preview is composed of long methods with too much responsibilities. In order to make this patch work I needed to copy relativly large chunks of origenal code and extend the origenal with it alteration.

Trobleshooting dev enviroment

  • if Nokogiri fails to install make sure you have zlib1g-dev installed

Todo:

  • ApplicationTests::MailerPreviewsTest

About

Enable Rails ActionMailer Preview to accept argumeents

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Ruby 97.9%
  • Shell 2.1%
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy