Sha256: fde4f13dee49eb51c1ae2fd3786f841470d5368f03c9503703d4a7ec3d704f7b

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

# DataAttributes

A gem to provide [HTML data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes)
easily from model to view.

## Setup

Just add this into your `Gemfile`:

```ruby
gem 'data_attributes'
```

If you want to take benefits of `content_tag_for` and `div_for` methods, add
also [record_tag_helper](https://rubygems.org/gems/record_tag_helper) into
your `Gemfile`:

```ruby
gem 'record_tag_helper'
```

Then, just run a `bundle install`.

## Usage

In your model just add `DataAttributes::Model` mixin:

```ruby
class Article

  include DataAttributes::Model

end
```

Then, you can define you model's data attributes that are included into view:

```ruby
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](https://rubygems.org/gems/record_tag_helper)
gem will add model's data attributes into view:

```erb
<%= div_for @article do %>
<!-- <div class="article" data-id="42" data-title="Hello!"> -->
```

You can also pass data attributes directly to `content_tag` method:

```erb
<%= content_tag :div, id: 'articles', data: { count: 28 } do %>
```

## Executing test suite

This project is fully tested with [Rspec 3](http://github.com/rspec/rspec).
Just run `bundle exec rake` (after a `bundle install`).

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
data_attributes-1.3.1 README.mdown
data_attributes-1.3.0 README.mdown
data_attributes-1.2.0 README.mdown
data_attributes-1.1.1 README.mdown
data_attributes-1.1.0 README.mdown