DataAttributes
A gem to provide HTML data attributes easily from model to view.
Setup
Just add this into your Gemfile
:
gem 'data_attributes'
If you want to take benefits of content_tag_for
and div_for
methods, add
also record_tag_helper into
your Gemfile
:
gem 'record_tag_helper'
Then, just run a bundle install
.
Usage
In your model just add DataAttributes::Model
mixin:
class Article
include DataAttributes::Model
end
Then, you can define you model's data attributes that are included into view:
class Article
include DataAttributes::Model
data_attribute :id, :title
end
Note that DataAttributes::Model
mixin is automatically added into
ActiveRecord::Base
(if present).
Then content_tag_for
and div_for
methods from record_tag_helper
gem will add model's data attributes into view:
<%= div_for @article do %>
<!-- <div class="article" data-id="42" data-title="Hello!"> -->
You can also pass data attributes directly to content_tag
method:
<%= content_tag :div, id: 'articles', data: { count: 28 } do %>
Executing test suite
This project is fully tested with Rspec 3.
Just run bundle exec rake
(after a bundle install
).