Sha256: 8d784952fadb220c8dee953879e58f7ce2e2b703ceff6cca84871499733fa9dc

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# DataAttributes

Convenience helpers to provide [HTML data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes)
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
```

Then you must add `DataAttribute::View` into Rails `ApplicationHelper`:

```ruby
module ApplicationHelper

  include DataAttribute::View

end
```

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

1 entries across 1 versions & 1 rubygems

Version Path
data_attributes-1.0.0 README.mdown