SimpleShow is to #show as SimpleForm is to #edit.
-
Rails 3
Add it to your Gemfile:
gem "simple_show"
Update your bundle:
bundle update simple_show
Run the generator:
rails generate simple_show:install
Assume we have a Person object with name, phone, email, born_on, etc. In our show action we can then do this:
<%= simple_show_for @johndoe do |s| %> <%= s.show :name %> <%= s.show :phone, :label => 'Telephone' %> <%= s.show(:email) {|o| o.email.downcase} %> <%= s.show :born_on, :value => '**censored**' %> <%= s.show :updated_at, :format => '%B %d' %> <%= s.show :created_at, :format => :short # Assumes you've defined a DATE_FORMAT for :short %> <%= s.show :cash_in_wallet, :to_currency => true %> <%= s.show :height, :if => :tall %> <%= s.show :weight, :unless => :sensitive %> <%= s.show :title, :unless_attr => :nil? %> <%= s.show_collection :friends do |friend| %> <%= friend.name %> - <%= friend.age %> <% end %> <% end %>
Which would generate (using the defaults) output like this, assuming that @johndoe.tall returns false, @johndoe.sensitive returns true, and @johndoe.title is nil:
<div class="simple_show person" id="person_123"> <div class="show"> <label>Name:</label> <span class="value">John Doe</span> </div> <div class="show"> <label>Telephone:</label> <span class="value">867-5309</span> </div> <div class="show"> <label>Email:</label> <span class="value">johndoe@example.com</span> </div> <div class="show"> <label>Born on:</label> <span class="value">**censored**</span> </div> <div class="show"> <label>Updated at:</label> <span class="value">August 2</span> </div> <div class="show"> <label>Created at:</label> <span class="value">Aug 1 2010</span> </div> <div class="show"> <label>Cash in wallet:</label> <span class="value">$123.45</span> </div> <div class="show"> <label>Friends:</label> <ul class="show_collection"> <li class="show_collection_item">Jim - 32</li> <li class="show_collection_item">Tina - 23</li> </ul> </div> <br clear="all"> </div>
There are a number of options that control the resulting HTML. See lib/simple_show.rb for what they are. For example, if you don’t want that BR tag then set SimpleShow.clear_on_close to false.
-
Add options for booleans.
(The MIT License)
Copyright © 2011 Philip Hallstrom <philip@pjkh.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.