We have been having some projects lately that needed a lot of admin CRUDs and pages showing a list of attributes, and we were getting bored of copy and paste code like this in our show pages, for every single attribute:
<%= @person.name %>
We had already created some helper to do the work for us, but having this helper being copied from one project to another wasn’t that DRY. That’s when we decided to create ShowFor.
ShowFor is a DSL to help you showing a list of attributes, with I18n, perfect for show pages in CRUD interfaces. It allows you to replace code/html duplication in your views using a nice syntax. Let’s see what we can do.
Let’s imagine we have a Person model, which has first_name, last_name, age, photo, and confirmed attributes. The following lines show a list of values for a specific person:
<% show_for @person do |p| %> <%= p.attribute :first_name %> <%= p.attribute :last_name %> <%= p.attribute :confirmed? %> <%= p.attribute :created_at, :format => :short %> <%= p.attribute :age, :if_blank => "No age" %> <% p.attribute :photo do %> <%= image_tag(@person.photo_url) %> <% end %> <% end %>
Here is an example output you will get:
08 Mar 11:30
As you can see, you are going to get a default html markup, with classes and ids to help you design with CSS. And if you noticed, we are using extra options in some attributes, lets take a look at some of them:
:formatlets you pass any format that will be used together with i18n, for date/time attributes only, as you would use with the
:if_blankspecifies what to show if the attribute is blank. It may have a default value from i18n.
do...end: using blocks you can handle the content for that specific attribute by yourself, as we have done in this example for the
Boolean attributes, as our
:confirmed, also have a default for
false values, and can be configured through i18n. If you want to say “Yup” and “Nope” instead of “Yes” and “No” respectively, just change your i18n file. You can also pass a
:escape option to not escape the value (
true by default).
ShowFor also works with associations. For instance, we can easily add that a Person belongs to a City with a name attribute and that it has and belongs to many Tags. To handle the former, we can do:
<%= p.association :city %>
ShowFor will guess the right attribute to show by looking into all possibilities configured in
ShowFor.association_methods and in this case choosing :name. But of course, you can change it as you need, on-the-fly:
<%= p.association :city, :using => :full_name %> <%= p.attribute :full_name, :in => :city %>
Both calls above will have the same output, just choose the one which suits you better.
Handling collections is easy as belongs_to associations. You can just pass the association to ShowFor and it will know whether it’s a collection or not, generating a list of elements using
<%= p.association :tags %>
However, if you want to render the collection inline, you can use :to_sentence or :join as options:
<%= p.association :tags, :to_sentence => true %> <%= p.association :tags, :join => ',' %>
It’s also possible to pass a block to the collection. ShowFor will create the wrapper tag (
ul by default in this case) and will
yield each element in the collection for you to handle it:
<% a.association :tags do |tag| %>
You may have noticed ShowFor has a default label using the
strong html tag. It also exposes you this as a helper, so you can use it whenever you wish:
<%= p.label :first_name %> <%= p.label :age, :id => 'person_age' %> First name Age
Please don’t forget to run the generator and take a look at the initializer, it will allows you configure several parts of ShowFor.
ShowFor helps you to show your objects easily with a default html markup, and can be totally configured to suit your needs. It has been helping us in every project, and we hope it may help you too. If you have any doubt, please take a look at the README, there are a lot of examples and documentation there.
And what about you? Do you have any helper you use every single day that might be transformed in a plugin/gem? Do not hesitate on doing this, we would be glad to see your work.