0.0
No commit activity in last 3 years
No release in over 3 years
A gem to provide HTML data attributes from model to view
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

< 6.0.0, >= 4.1.0
< 6.0.0, >= 4.1.0
< 10.0.0, >= 9.0.0
< 13.0.0, >= 12.0.0
< 3.6.0, >= 3.5.0

Runtime

< 6.0.0, >= 4.1.0
 Project Readme

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).