Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yasaichi/remove_data_attributes
Rails plugin for removing data attributes from views :dash:
https://github.com/yasaichi/remove_data_attributes
data-attributes rails
Last synced: about 1 month ago
JSON representation
Rails plugin for removing data attributes from views :dash:
- Host: GitHub
- URL: https://github.com/yasaichi/remove_data_attributes
- Owner: yasaichi
- License: mit
- Created: 2017-12-02T07:28:19.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T08:29:43.000Z (about 1 year ago)
- Last Synced: 2024-03-15T12:13:07.206Z (10 months ago)
- Topics: data-attributes, rails
- Language: Ruby
- Homepage:
- Size: 65.4 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# remove\_data\_attributes
[![Gem Version](https://badge.fury.io/rb/remove_data_attributes.svg)](https://badge.fury.io/rb/remove_data_attributes)
[![Build Status](https://travis-ci.org/yasaichi/remove_data_attributes.svg?branch=master)](https://travis-ci.org/yasaichi/remove_data_attributes)
[![Maintainability](https://api.codeclimate.com/v1/badges/eddaa55a25ccd62b9eb8/maintainability)](https://codeclimate.com/github/yasaichi/remove_data_attributes/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/eddaa55a25ccd62b9eb8/test_coverage)](https://codeclimate.com/github/yasaichi/remove_data_attributes/test_coverage)## Motivation
As described in [this post](https://blog.kentcdodds.com/making-your-ui-tests-resilient-to-change-d37a6ee37269), using data attributes as selector makes your UI tests more resilient to change.
However, there is one little problem in this way: these attributes are useless when running your application in production.## Solution (with limitations)
remove_data_attributes is a plugin for Ruby on Rails like [babel-plugin-react-remove-properties](https://github.com/oliviertassinari/babel-plugin-react-remove-properties) for Babel.
Note that the plugin can remove __only the data attributes passed into the view helpers__, because there is no room to parse and remove all attributes (including ones embedded in HTML tags) in advance unlike the Babel plugin.## Installation
Add this line to your application's Gemfile:```ruby
gem 'remove_data_attributes', group: :production
```And then execute:
```bash
$ bundle
```## Usage
Run the installation generator with:```sh
$ rails generate remove_data_attributes:install
```And then specify `data-*` attributes you'd like to remove in the generated configuration file:
```yaml
data_attributes:
- data-testid
```This will remove `data-testid` attributes.
## Example
Here is an example of removing `data-testid` attributes.### Template
```ERB
<%= form_with scope: :user, url: session_path(:user) do |f| %>
<%= field_set_tag "Email" do %>
<%= f.email_field :email, "data-testid": "email" %>
<% end %><%= field_set_tag "Password" do %>
<%= f.password_field :password, "data-testid" => "password" %>
<% end %><%= f.submit "Sign in", data: { testid: "submit" } %>
<% end %>
```### Rendered HTML (formatted for readability)
```html
Password
```
## Contributing
You should follow the steps below:1. [Fork the repository](https://help.github.com/articles/fork-a-repo/)
2. Create a feature branch: `git checkout -b add-new-feature`
3. Commit your changes: `git commit -am 'Add new feature'`
4. Push the branch: `git push origin add-new-feature`
4. [Send us a pull request](https://help.github.com/articles/about-pull-requests/)## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).