DomFor
Helper for creating HTML wrappers ActiveRecord objects
Installation
Add this line to your application's Gemfile:
gem 'dom_for'
And then execute:
$ bundle
Or install it yourself as:
$ gem install dom_for
Usage
<%= dom_for User, attribute_1: 'value_1', attribute_2: 'value_2', attribute_3: 'value_3' do %>
<% @users.each do |user| %>
<%= dom_for user, admin: user.admin, blocked: user.blocked do %>
<%= content_tag(:span, user.name) %>
<% end %>
<% end %>
<% end %>
<div class="users" data-action="index" data-attribute-1="value_1" data-attribute-2="value_2" data-attribute-3="value_3" id="users">
<div class="user" data-admin="true" data-blocked="false" data-object-id="1" id="user_1">
<span>Mikhail</span>
</div>
<div class="user" data-admin="false" data-blocked="false" data-object-id="2" id="user_2">
<span>Yulia</span>
</div>
</div>
The first argument can be used as a model ActiveRecord:
<%= dom_for User do %>
<%= tag(:span) %>
<% end %>
<div class="users" data-action="index" id="users" />
<span />
</div>
And record ActiveRecord:
<%= dom_for @user do %>
<%= tag(:span) %>
<% end %>
<div class="user" data-action="show" data-object-id="1" id="user_1" />
<span />
</div>
The second argument passed to additional html-attributes (is optional):
<%= dom_for User, attribute_1: 'value_1', attribute_2: 'value_2' do %>
<%= tag(:span) %>
<% end %>
<div class="users" data-action="index" data-attribute-1="value_1" data-attribute-2="value_2" id="users" />
<span />
</div>
The third argument, the helper dom_for
, takes a block of code that will be wrapped in the tag <div>
(is optional):
<%= dom_for User %>
<div class="users" data-action="index" id="users" />
When defined instance variable with class name, the helper dom_for
creates the additional html-attributes for this object:
<% @user = User.last %>
<%= dom_for User do %>
<%= tag(:span) %>
<% end %>
<div class="user" data-action="show" data-object-id="1" data-object="users" id="user_1" >
<span />
</div>
For each request, the helper dom_for
creates additional attribute data-action
, which will be equal to the method of the controller handling the request:
<%= dom_for User do %>
<%= tag(:span) %>
<% end %>
<div class="users" data-action="index" id="users">
<span />
</div>
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request