Search code examples
ruby-on-railsrubyscopehelperdynamic-scope

Making dynamic scope helpers in Rails


Several of my partials can be rendered in two "modes". If full_display is false, I don't render several fields. To make things easy I wanted to make one of the "modes" default - if full_display is not defined, treat it as false. I came up with this code:

(((not defined?(full_display).nil?) && full_display) || false)

Quite a lot to move around. It would be nice to put it inside a helper or something, but since Ruby has only lexical scope I can't think of any good way to do it.

Bad ideas that I've already tried:

  • on the top of the partial do <% display = long code that is above %> and use display through the code, but creating local variables in a view looks bad and has to be copied into every partial using full_display.
  • wrap it inside a string, put it into a helper and use eval(display_helper) in view, but obviously this creates security concerns.

Solution

  • That's quite a convoluted way of saying something as simple as:

    defined?(full_display) && full_display
    

    In Ruby there are two values that are non-true, nil and false, all others evaluate as true, which includes 0, empty string, among others, that would otherwise evaluate as false in Perl, PHP, and C. Testing with .nil? is usually reserved for those rare cases where you want to differentiate between false and undefined, and this is sometimes the case with boolean fields where a missing value is different from a false value.

    In any case, in the view space it is a lot easier to assign defaults using the or-equals operator ||= like this:

    <% full_display ||= false %>
    

    That is equivalent to full_display = full_display || false. This does two things. First, it defines the full_display variable even if it was not previously created, and secondly it ensures that the it will contain a value that is at least false, never undefined.

    You will see the ||= default pattern a lot in Ruby code as it's an easy way to assign something in the case where it will be nil otherwise.