Git Product home page Git Product logo

list_fu's Introduction

ListFu
======

A common pattern I always have when listing models in my views is as follows.

1. Have rails generate my model's scaffold
2. Replace Rails generated html table in index.html.erb going from

 <table>
  <td>
    <th>column one</th>
    <th>column two</th>
  </td>
  <% for model in @models %>
    <td>
      <tr>column one</tr>
      <tr>column two</tr>
    <td>
    ... actions ...
  <% end %>
  </table

to

  <ul>
  <%- @models.each.do |model| -%>
    <li>
    <%= render :partial=> "model" %>
    </li>
  <%- end -%>
  </ul>
  
where my model has its own default partial _model.html.erb.

This is ruby. I can do better than that.

Examples
=======

ListFu adds list markup behavior to your views through the method list_of.

A simple unordered list.

index.html.erb

 <%= list_of @models %> 
 
generates

<ul id="model_list">
  <li id="1_model">
    <%= render :partial =>"model", :object=> model %>
  </li>
  <li id="2_model">
    <%= render :partial =>"model", :object=> model %>
  </li>
</ul>
 
 
To further customize list_of it takes two hashes. The first being targeted at the html list element
and the second being targeted at each html list item. These options are basically what you get in any rails 
view helpers except for two items. The list hash uses the symbol :ordered to indicate that the list should be an unordered
or ordered list. This defaults to false. The list item hash uses the symbol :partial to indicate which partial to render. This defaults to the name 
the model's class.

index.html.erb
 
 <%= list_of @models, {:ordered=>true, :class=>"my_model_list_class", :id=>"my_model_list_id"}, {:partial=>"my_partial"} %>

generates

<ol class="my_model_list_class" id="my_model_list_id">
  <li id="1_model">
    <%= render :partial =>"my_partial", :object=> model %>
  </li>
  <li id="2_model">
    <%= render :partial =>"my_partial", :object=> model %>
  </li>
</ol>

Blocks 
======

If you don't like using partials you can use the block form of list_of which takes the same parameters as above plus a block
that gets passed each model at it iterates over them.

index.html.erb
 
 <% list_of @models do |model| %>
   <%= link_to model.attribute, model %>
 <% end %>

generates

  <ul id="model_list">
    <li id="1_model">
      <a href="model/1"> model_attribute value</a>
    </li>
    <li id="2_model">
      <a href="model/1"> model_attribute value</a>
    </li>
  </ul>


Copyright (c) 2008 softprops, released under the MIT license

list_fu's People

Contributors

softprops avatar

Stargazers

Angus H. avatar  avatar  avatar  avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.