mailmodel rails plugin

Here is the first cut at a plugin I created, called Mailmodel. It’s pretty simple, you install it, configure the email sender/destination addresses, and then, in your ActiveRecord objects:

after_save :mail_model

For those objects that you want to be notified of when they change. Think of it as ‘scaffolding’ for email notification of changes in your database (new comments, new users, new payments) when you aren’t quite ready to set up notification for each individual thing.

Available from:

Some improvements that could be made:

  • Customize behavior for some objects: if it’s a this, then include that and that in the object dump.

  • Customized templates depending on object type in order to take the scaffoling analogy further.

Pretty Print Active Record

I wanted to be able to pretty print ActiveRecord objects, and do so in a way that’s a little bit more involved than what pp gives you:

module ActiveRecordPrettyPrint

  def pprint(ar, maxdepth=1)
    pp_helper(ar, "", 0, [], maxdepth - 1)

  def pp_helper(ar, buf, level, seen, maxdepth)
    if level > maxdepth || seen.member?(ar)
      buf << (" " * level) + "#{ar}n"
      return buf

    seen << ar

    reflections = ar.class.reflections
    rcolumns = reflections.collect { |k, v| v.primary_key_name }

    buf << "#{ar.class}:n"
    ar.attributes.keys.reject { |k| rcolumns.member? k }.each do |k|
      buf << (" " * level) + "#{k}  =>  " + ar.send(k).to_s + "n"

    reflections.keys.each do |k|

      buf << (" " * level) + k.to_s + " =>  "
      res = ar.send(k)
      if res.nil?
        buf << "[nil]n"
      elsif res.class == Array
        res.each do |o|
          pp_helper(o, buf, level + 1, seen, maxdepth)
        pp_helper(res, buf, level + 1, seen, maxdepth)
        buf << "n"

    return buf


I suspect I’ll fix it up more as I go along, but this is the basic idea… Be careful with maxdepth, though, or else you risk hitting the database too much.

Rails ExceptionNotifier plugin

I’m irritated with myself that I hadn’t found this before. If you aren’t using it now on your rails sites, you need to install it right away:

Indeed, if it were up to me, I would include it as part of the standard distribution. It’s small, quick to set up, and a huge help in tracking down and eliminating problems.