How to Preview All ActionMailer Mails in the Browser

Notice: this needs to be admin/development only, since it can be really dangerous in the wrong hands… (uses eval or param…)

  • Developers see what they are dooing (just hit Refresh)
  • Livesaver for HTML Emails
  • Non-developers can check alignment/spelling/… sooo easy
  • Generated mails can be shared through url so no need for forwarding/screenshots

View
Just make a form that submits:

  • the method you want to call
  • list of arguments to use
  <%form_tag({:action=>'make_mail'}, :method=>:get) do%>
    <%=select_tag :method_name, options_for_select(UserMailer.public_instance_methods(false).sort)%>
    Models: Product.find(12322) / String: "abc" / Numbers: 123 / Array: [1,2,3]
    <table>
      <%5.times do |i|%>
        <tr>
          <td><%=i%></td>
          <td><%=text_field_tag "args[]", '', :style=>'width:300px'%></td>
        </tr>
      <%end%>
    </table>
    <%=submit_tag 'Preview'%>
    <%=submit_tag 'Send'%>
  <%end%>
<%end%>

Controller
Evals the ‘args’ and send or previews the mail.

def make_mail
  args = params[:args].map{|arg| eval arg}.compact

  if params[:commit]=='Preview' #preview button
    @text = UserMailer.send("create_#{params[:method_name]}", *args).body
    if @text =~ /<body>/
      render :text=>@text
    else
      render :text=>"<pre>#{@text}</pre>"
    end
  else #Send button
    UserMailer.send("deliver_#{params[:method_name]}", *args)
    flash[:notice] = 'Mail sent!'
    redirect_to params.merge(:action=>:index)
  end
end

Happy mail preview too you 😀

Quick Markdown Preview For Github Readme

I write my Readme in marddown, and often it gets messed up and then I have to commit & push until its finally right, so I wrote a small script to get a preview (using githubs markdown library)!

Usage

markdown README.markdown
... chrome pops up with the html preview

Install
gem install redcarpet

Put this into e.g. ~/bin/markdown and do a chmod +x on it

#!/usr/bin/env ruby
raise "gime a file!!" unless ARGV[0]
exec "redcarpet #{ARGV[0]} > /tmp/markdown.html && chromium-browser /tmp/markdown.html"

Alternatively: Interactive Live Preview<

Automated SSH Login with Password and Additional Commands

The normal answer to most ssh related problems is: “use public/private keys”
but sometimes this does not work…

What i did regularly was this:

  • ssh to xxx@asdads.com
  • enter password
  • sudo su admin
  • cd /apps/hosts/some_project

Thats not very complicated, but its frustrating. What I do now is: “sshxx”, which executes this excpect script in ~/bin/sshxx

#!/usr/bin/expect
set pass [lindex $argv 0] #get first argument
spawn ssh xxx@asdads.com
expect "assword" #matches Password and password
send "$pass\r"
expect "xxx@" #wait for the prompt
send "sudo su admin\r"
send "cd /apps/hosts/some_project\r"
send "clear\r" #clean up the mess
interact

(For Ubuntu you need to “sudo apt get install expect” first)

Whats your Namespace-Fooptrint?

Could it be that you pollute the global namespace ?

If you ask your users to ‘include’ your library this can happen easily!

Calculate footprint

initial = methods.count + Module.constants.count
require 'my_library'
include MyLibrary

puts "Namespace-Footprint"
puts methods.count + Module.constants.count - initial

How to clean up ?

A simple solution is adding a separate Inclusion module, that does not live in your main
namespace and redirect all methods you want to share with the user from your main namespace.

#my_library/inclusion.rb
module MyLibrary
  module Inclusion
    ...
  end
end

#my_library.rb
module MyLibrary
  ..
  Inclusion.public_instance_methods.each do |method|
    define_method method do |*args|
      Inclusion.send(method,*args)
    end
  end
end