In Progress
Unit 1, Lesson 21
In Progress

Awesome Print and Rails

Video transcript & code

In episode #372, we delved into the awesome_print gem. We saw how it can not only format standard Ruby objects beautifully, but that it also has support for Nokogiri DOM elements.

Nokogiri is just one of a number of third-party libraries that awesome_print has extensions for. Let's check out its support for another very popular Ruby library: ActiveRecord.

I have a simple Rails app here. Let's add awesome_print to the Gemfile.

gem "awesome_print"

Now lets jump into the Rails console, and dump some data. First, we'll do it with no special formatting.

irb(main):001:0> Task.all
  Task Load (1.7ms)  SELECT "tasks".* FROM "tasks"
=> #<ActiveRecord::Relation [#<Task id: 1, title: "Pick a topic", completed: false, created_at: "2015-12
-10 16:41:51", updated_at: "2015-12-10 16:41:51">, #<Task id: 2, title: "Compose script", completed: fal
se, created_at: "2015-12-10 16:42:05", updated_at: "2015-12-10 16:42:05">, #<Task id: 3, title: "Record
screen", completed: false, created_at: "2015-12-10 16:42:19", updated_at: "2015-12-10 16:42:19">, #<Task
 id: 4, title: "Record VO", completed: false, created_at: "2015-12-10 16:42:31", updated_at: "2015-12-10
 16:42:31">, #<Task id: 5, title: "Produce video", completed: false, created_at: "2015-12-10 16:42:41",
updated_at: "2015-12-10 16:42:41">, #<Task id: 6, title: "Export video", completed: false, created_at: "
2015-12-10 16:42:59", updated_at: "2015-12-10 16:42:59">, #<Task id: 7, title: "Post episode", completed
: false, created_at: "2015-12-10 16:43:06", updated_at: "2015-12-10 16:43:06">]>

Now we'll integrate awesome_print, and try again.

irb(main):002:0> AwesomePrint.irb!
:output_value
irb(main):003:0> Task.all
  Task Load (0.2ms)  SELECT "tasks".* FROM "tasks"
[
    [0] #<Task:0x00558d18b87798> {
                :id => 1,
             :title => "Pick a topic",
         :completed => false,
        :created_at => Thu, 10 Dec 2015 16:41:51 UTC +00:00,
        :updated_at => Thu, 10 Dec 2015 16:41:51 UTC +00:00
    },
    [1] #<Task:0x00558d18b87658> {
                :id => 2,
             :title => "Compose script",
         :completed => false,
        :created_at => Thu, 10 Dec 2015 16:42:05 UTC +00:00,
        :updated_at => Thu, 10 Dec 2015 16:42:05 UTC +00:00
    },
    [2] #<Task:0x00558d18b87518> {
                :id => 3,
             :title => "Record screen",
         :completed => false,
        :created_at => Thu, 10 Dec 2015 16:42:19 UTC +00:00,
        :updated_at => Thu, 10 Dec 2015 16:42:19 UTC +00:00
    },
    [3] #<Task:0x00558d18b87360> {
                :id => 4,
             :title => "Record VO",
         :completed => false,
        :created_at => Thu, 10 Dec 2015 16:42:31 UTC +00:00,
        :updated_at => Thu, 10 Dec 2015 16:42:31 UTC +00:00
    },
    [4] #<Task:0x00558d18b871f8> {
                :id => 5,
             :title => "Produce video",
         :completed => false,
        :created_at => Thu, 10 Dec 2015 16:42:41 UTC +00:00,
        :updated_at => Thu, 10 Dec 2015 16:42:41 UTC +00:00
    },
    [5] #<Task:0x00558d18b87040> {
                :id => 6,
             :title => "Export video",
         :completed => false,
        :created_at => Thu, 10 Dec 2015 16:42:59 UTC +00:00,
        :updated_at => Thu, 10 Dec 2015 16:42:59 UTC +00:00
    },
    [6] #<Task:0x00558d18b86f00> {
                :id => 7,
             :title => "Post episode",
         :completed => false,
        :created_at => Thu, 10 Dec 2015 16:43:06 UTC +00:00,
        :updated_at => Thu, 10 Dec 2015 16:43:06 UTC +00:00
    }
]

awesome_print detects the ActiveRecord objects, and formats their database attributes accordingly.

There is even support for nicely formatting ActiveRecord class objects, which displays the database schema for that class.

irb(main):009:0* Task
class Task < ActiveRecord::Base {
            :id => :integer,
         :title => :string,
     :completed => :boolean,
    :created_at => :datetime,
    :updated_at => :datetime
}

We've only scratched the surface of awesome_print's Rails integration. Have you ever found yourself debugging a Rails controller, and wanted to dump some object to the log in a readable way? awesome_print can help with this.

Let's jump into a controller method. Let's say we want to see exactly what's in a task object when it is displayed. We'll add a logging line to log the @task variable, but instead of using logger.debug, we'll use logger.ap.

def show
  logger.ap @task
end

Then we'll tail the log, and request the page from a browser.

Started GET "/tasks/4" for 127.0.0.1 at 2015-12-10 15:22:39 -0500
Processing by TasksController#show as HTML
  Parameters: {"id"=>"4"}
  Task Load (0.1ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT 1  [["id", 4]]
#<Task:0x007f6a5dc276e8> {
            :id => 4,
         :title => "Record VO",
     :completed => false,
    :created_at => Thu, 10 Dec 2015 16:42:31 UTC +00:00,
    :updated_at => Thu, 10 Dec 2015 16:42:31 UTC +00:00
}
  Rendered tasks/show.html.erb within layouts/application (0.7ms)
Completed 200 OK in 47ms (Views: 39.0ms | ActiveRecord: 0.7ms)

We can see that the object is dumped to the log, complete with awesome_print's signature layout and syntax highlighting.

Sometimes we don't want to have to refer to the log. Sometimes we just want to do some quick-and-dirty temporary debug output directly to the browser.

awesome_print has us covered in that scenario as well. Let's open up the tasks/show.html.erb view file. At the end, we'll add a call to ap, dumping the current @task. We prefix it with raw in order to allow awesome_print to insert HTML into the page.

<%= raw ap @task %>

When we go to a task page in the browser, we can now see an embedded breakdown of the task object.

Obviously this is not code we'd want to leave in our application, but for quick debugging it can be a real timesaver.

In addition to Rails and ActiveRecord, awesome_print has support for a number of other popular libraries. Check out the documentation and the source code to find out more.

Happy hacking!

Responses