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:
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’ %>
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.
To translate the message, you can simply add the “no_value” key (no scope) to
your translation files.
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:
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:
Copyright © 2008 Iain Hecker, released under the MIT license