A simpel helper to display a nice message when there is no value
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec
README.textile
Rakefile
init.rb

README.textile

NoValueHelper

The Problem

This Ruby on Rails plugin tries to solve a common pattern when showing
values from the database. If you want to show a nice message like ‘no value’
when an optional attribute has been left empty, you usually need to do the
same thing over and over again:

<%= @user.name.blank? ? ‘no value’ : @user.name %>

It gets even worse when it’s about an optional relation, with some extra methods:

<%= user.daddy ? link_to(user.daddy.name, @user.daddy) : ‘no daddy’ %>

The Solution

So this plugin tries to shorten this:

<%= show(@user.name) %>

Or the second example:

<%= show(:link_to, @user.daddy) { @user.daddy.name } %>

Don’t worry, NoMethodErrors will be caught for you. That is why we use a block
in this case.

For the exact usage, read the specs.

Configuration

To translate the message, you can simply add the “no_value” key (no scope) to
your translation files.

nl-NL: no_value: geen waarde

By default the message is encapsulated by an em-tag with the class ‘no_value’.
To change this, set the class variable @@no_value_text with a lambda. This is
done so I18n.translate will work. Make an initializer
(in config/initializers/no_value_helper.rb), containing this:

module NoValueHelper @@no_value_text = lambda { “something more to your liking” } end

You can also change how this plugin checks for empty values. By default this is
done with the method blank?.
This means that empty strings are also treated as ‘no value’. To change this,
set the class variable @@no_Value_check_method to a lambda that does what you
want. Your initializer will look something like this:

module NoValueHelper @@no_value_check_method = lambda { |value| value.nil? } end

Copyright © 2008 Iain Hecker, released under the MIT license