h4ck3r+=boi v 2.0

  1. Search
  2. About
  3. Subscribe
  4. Archive
  5. Random
  1. Posts about Rails
  2. Posts about Python
  3. Posts about Agile
  4. Posts about development
  5. Posts about startups

h4ck3r+=boi v 2.0

Newer
Older
  • Presenter Pattern, Rails 3 and html_safe

    I’ve been playing with the Presenter pattern lately, and ran across an issue: it felt like I was writing a lot of HTML in a .rb file.

    First off: there doesn’t seem to be any real resources into getting into Presenter patterns in Rails. Or rather, there are a few from 2008, but nothing modern. (Except draper, which I think is too complex. Or I don’t yet understand why it needs to be the way it is). Anyway, I’m winging it here: it’s possible I’m doing it wrong.

    I was creating a presenter for an object with comments: a little titlebar for the most recent comment, which use a HTML5 DETAILS widget to show the other comments.

    I started with something like this:

    Which is horrible, and ugly. Big, kind of clumsy, with a lot of string concatenation going on.

    Jim Gay suggested that I break my method into smaller methods, but still keep the string concatenation roots. (Our gist comment conversation)

    Which I did… then I realized why string concatenation felt bad to me in the first place: because Rails 3 changes the rules

    As of Rails 3, all HTML content inside strings is HTML escaped (meaning the browser says, “oh, this isn’t meant for me to read, to construct a page, but for a human to read”.

    Except, in my specific case (creating HTML snippets in a Presenter pattern) I really want the browser to construct the page with that content.

    So I needed a way to avoid that, and avoid that without too much pain. Because the last thing I want to do when writing Presenter functions is to worry about putting .html_safe behind every.html_safe, single.html_safe, bloody.html_safe, string.html_safe.

    At the end of the night, I had refactored everything to:

    What, WTF is s”something”?

    Glad you asked. This presenter inherits from an ApplicationPresenter class (which itself subclasses from SimpleDelegator).

    ApplicationPresenter implements one interesting method:

    The revised primary_comment presenter does some other things better:

    1. Uses content_tag in some strategic parts to clean up the HTML output.
    2. Calls other presenter functions as appropriate

    Also, this method has two interesting properties:

    1. Because of Ruby’s function calling semantics, in the case of s"something" that s looks like some clever magic prefix (like 0x for hex numbers) — but it is not. It’s just a method call.
    2. When given a list, will call html_safe on all the things! parameters, then concatenate them together

    Also, on line 14 we try to slip some bad HTML into the page. Perhaps the second parameter to s is a user inputted description text, and their trying to hijack the site. The h helper says, “Wait a minute, I actually do want to escape that”

    Conclusion

    I need to spend some time and talk more about my structure for Presenters, but that’s fodder for another article.

    Tagged: rails

    Posted on August 9, 2012 with 2 notes

    1. blinni-elliott reblogged this from rwilcox
    2. trivuong reblogged this from rwilcox
    3. rwilcox posted this
  • staff

Field Notes Theme. Designed by Manasto Jones. Powered by Tumblr.