remove_data_attributes 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/MIT-LICENSE +1 -1
- data/README.md +72 -9
- data/Rakefile +3 -4
- data/lib/generators/remove_data_attributes/install/USAGE +8 -0
- data/lib/generators/remove_data_attributes/install/install_generator.rb +15 -0
- data/lib/generators/remove_data_attributes/install/templates/remove_data_attributes.yml +3 -0
- data/lib/remove_data_attributes.rb +0 -1
- data/lib/remove_data_attributes/configuration.rb +39 -10
- data/lib/remove_data_attributes/errors.rb +15 -0
- data/lib/remove_data_attributes/railtie.rb +16 -5
- data/lib/remove_data_attributes/tag_options_filter.rb +1 -1
- data/lib/remove_data_attributes/version.rb +1 -1
- metadata +22 -5
- data/lib/tasks/remove_data_attributes_tasks.rake +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69172fe3cb093195772529974892d07ac0b1d025
|
4
|
+
data.tar.gz: fbb4df2602315e76d4dea5bda7b5e57eacea462f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c24bd87c412edf0fec11d8eef4d1211b94ee4b4c73877ca4a66c8eddf91001a1604ebd145749dcb255918228ea5b9d3b9ad285b38fe47ace7df0ce9fb827b290
|
7
|
+
data.tar.gz: 62dc161345a0f579baad575c02537dad0bf4a9c05c01db61b9a5c1dc3fb0a4acf68b48807755e03d6e8b56752fbcd894558f49bb7d8a0c22e45c0f36ae589181
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
1
|
# CHANGELOG
|
2
|
+
## [0.2.0](https://github.com/yasaichi/remove_data_attributes/releases/tag/v0.2.0) (December 26, 2017)
|
3
|
+
* [Write README](https://github.com/yasaichi/remove_data_attributes/pull/8)
|
4
|
+
* [Use YAML for the configuration](https://github.com/yasaichi/remove_data_attributes/pull/7)
|
5
|
+
* [Implement a generator of the configuration file](https://github.com/yasaichi/remove_data_attributes/pull/6)
|
6
|
+
* [Change dependency from `rails` to `railties`](https://github.com/yasaichi/remove_data_attributes/pull/5)
|
7
|
+
|
2
8
|
## [0.1.0](https://github.com/yasaichi/remove_data_attributes/releases/tag/v0.1.0) (December 10, 2017)
|
3
9
|
* The initial release to reserve the gem name
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,22 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# remove\_data\_attributes
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/remove_data_attributes.svg)](https://badge.fury.io/rb/remove_data_attributes)
|
3
|
+
[![Build Status](https://travis-ci.org/yasaichi/remove_data_attributes.svg?branch=master)](https://travis-ci.org/yasaichi/remove_data_attributes)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/eddaa55a25ccd62b9eb8/maintainability)](https://codeclimate.com/github/yasaichi/remove_data_attributes/maintainability)
|
5
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/eddaa55a25ccd62b9eb8/test_coverage)](https://codeclimate.com/github/yasaichi/remove_data_attributes/test_coverage)
|
3
6
|
|
4
|
-
##
|
5
|
-
|
7
|
+
## Motivation
|
8
|
+
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.
|
9
|
+
However, there is one little problem in this way: these attributes are useless when running your application in production.
|
10
|
+
|
11
|
+
## Solution (with limitations)
|
12
|
+
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.
|
13
|
+
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.
|
6
14
|
|
7
15
|
## Installation
|
8
16
|
Add this line to your application's Gemfile:
|
9
17
|
|
10
18
|
```ruby
|
11
|
-
gem 'remove_data_attributes'
|
19
|
+
gem 'remove_data_attributes', group: :production
|
12
20
|
```
|
13
21
|
|
14
22
|
And then execute:
|
@@ -16,13 +24,68 @@ And then execute:
|
|
16
24
|
$ bundle
|
17
25
|
```
|
18
26
|
|
19
|
-
|
20
|
-
|
21
|
-
|
27
|
+
## Usage
|
28
|
+
Run the installation generator with:
|
29
|
+
|
30
|
+
```sh
|
31
|
+
$ rails generate remove_data_attributes:install
|
32
|
+
```
|
33
|
+
|
34
|
+
And then specify `data-*` attributes you'd like to remove in the generated configuration file:
|
35
|
+
|
36
|
+
```yaml
|
37
|
+
data_attributes:
|
38
|
+
- data-test
|
39
|
+
```
|
40
|
+
|
41
|
+
This will remove `data-test` attributes.
|
42
|
+
|
43
|
+
## Example
|
44
|
+
Here is an example of removing `data-test` attributes.
|
45
|
+
|
46
|
+
### Template
|
47
|
+
```ERB
|
48
|
+
<%= form_for :user, url: session_path(:user) do |f| %>
|
49
|
+
<%= field_set_tag "Email" do %>
|
50
|
+
<%= f.email_field :email, "data-test": "email" %>
|
51
|
+
<% end %>
|
52
|
+
|
53
|
+
<%= field_set_tag "Password" do %>
|
54
|
+
<%= f.password_field :password, "data-test" => "password" %>
|
55
|
+
<% end %>
|
56
|
+
|
57
|
+
<%= f.submit "Sign in", data: { test: "submit" } %>
|
58
|
+
<% end %>
|
59
|
+
```
|
60
|
+
|
61
|
+
### Rendered HTML (formatted for readability)
|
62
|
+
```html
|
63
|
+
<form action="/users/sign_in" accept-charset="UTF-8" method="post">
|
64
|
+
<input name="utf8" type="hidden" value="✓" />
|
65
|
+
<input type="hidden" name="authenticity_token" value="HyOvLlyjIh7Sv7fFt2fKy5+uJNeKwnYobQPs49pl/H7CKSAVrw57jxpERJihR+B77GNSh2pZHG5mEWl0ieYQnQ==" />
|
66
|
+
|
67
|
+
<fieldset>
|
68
|
+
<legend>Email</legend>
|
69
|
+
<input type="email" value="" name="user[email]" id="user_email" />
|
70
|
+
</fieldset>
|
71
|
+
|
72
|
+
<fieldset>
|
73
|
+
<legend>Password</legend>
|
74
|
+
<input type="password" name="user[password]" id="user_password" />
|
75
|
+
</fieldset>
|
76
|
+
|
77
|
+
<input type="submit" name="commit" value="Sign in" data-disable-with="Sign in" />
|
78
|
+
</form>
|
22
79
|
```
|
23
80
|
|
24
81
|
## Contributing
|
25
|
-
|
82
|
+
You should follow the steps below:
|
83
|
+
|
84
|
+
1. [Fork the repository](https://help.github.com/articles/fork-a-repo/)
|
85
|
+
2. Create a feature branch: `git checkout -b add-new-feature`
|
86
|
+
3. Commit your changes: `git commit -am 'Add new feature'`
|
87
|
+
4. Push the branch: `git push origin add-new-feature`
|
88
|
+
4. [Send us a pull request](https://help.github.com/articles/about-pull-requests/)
|
26
89
|
|
27
90
|
## License
|
28
91
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/base"
|
4
|
+
|
5
|
+
module RemoveDataAttributes
|
6
|
+
module Generators
|
7
|
+
class InstallGenerator < ::Rails::Generators::Base
|
8
|
+
source_root ::File.expand_path("../templates", __FILE__)
|
9
|
+
|
10
|
+
def copy_configuration_file
|
11
|
+
copy_file "remove_data_attributes.yml", "config/remove_data_attributes.yml"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,20 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "remove_data_attributes/errors"
|
4
|
+
require "yaml"
|
5
|
+
|
3
6
|
module RemoveDataAttributes
|
4
|
-
class
|
5
|
-
|
7
|
+
class Configuration
|
8
|
+
SUPPORTED_FILE_FORMATS = {
|
9
|
+
"YAML" => [".yml", ".yaml"].freeze
|
10
|
+
}.freeze
|
6
11
|
|
7
|
-
|
8
|
-
|
9
|
-
|
12
|
+
attr_writer :data_attributes
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def from_file(filename)
|
16
|
+
loader = loader_for(::File.extname(filename))
|
17
|
+
|
18
|
+
unless loader
|
19
|
+
message = "This file format is not supported."
|
20
|
+
raise ::RemoveDataAttributes::InvalidConfiguration, message
|
21
|
+
end
|
22
|
+
|
23
|
+
::File.open(filename) { |file| new(loader.load(file)) }
|
24
|
+
end
|
10
25
|
|
11
|
-
|
12
|
-
|
26
|
+
def supported_file_extensions
|
27
|
+
@supported_file_extensions ||= SUPPORTED_FILE_FORMATS.values.flatten.sort
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# :reek:ControlParameter
|
33
|
+
def loader_for(extname)
|
34
|
+
case extname
|
35
|
+
when *SUPPORTED_FILE_FORMATS["YAML"] then ::YAML
|
36
|
+
end
|
37
|
+
end
|
13
38
|
end
|
14
|
-
end
|
15
39
|
|
16
|
-
|
17
|
-
|
40
|
+
# :reek:ManualDispatch
|
41
|
+
def initialize(attributes = {})
|
42
|
+
attributes.each do |attribute_name, value|
|
43
|
+
method_name = "#{attribute_name}="
|
44
|
+
public_send(method_name, value) if respond_to?(method_name)
|
45
|
+
end
|
46
|
+
end
|
18
47
|
|
19
48
|
def data_attributes
|
20
49
|
@data_attributes ||= []
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RemoveDataAttributes
|
4
|
+
class Error < ::StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class ConfigurationNotFound < ::RemoveDataAttributes::Error
|
8
|
+
def initialize(dirname)
|
9
|
+
super("No configuration file is found in #{dirname}.")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class InvalidConfiguration < ::RemoveDataAttributes::Error
|
14
|
+
end
|
15
|
+
end
|
@@ -8,18 +8,29 @@ require "remove_data_attributes/tag_options_filter"
|
|
8
8
|
|
9
9
|
module RemoveDataAttributes
|
10
10
|
class Railtie < ::Rails::Railtie
|
11
|
-
|
11
|
+
initializer "remove_data_attributes" do
|
12
|
+
configuration = ::RemoveDataAttributes::Configuration.from_file(find_configuration_file)
|
13
|
+
next if configuration.data_attributes.empty?
|
14
|
+
|
12
15
|
::ActiveSupport.on_load(:action_view) do
|
13
|
-
patch = ::RemoveDataAttributes::TagOptionsFilter[
|
14
|
-
::RemoveDataAttributes.configuration.data_attributes
|
15
|
-
]
|
16
|
+
patch = ::RemoveDataAttributes::TagOptionsFilter[configuration.data_attributes]
|
16
17
|
|
17
|
-
if Gem::Version.new(Rails.version) < Gem::Version.new("5.1")
|
18
|
+
if ::Gem::Version.new(::Rails.version) < ::Gem::Version.new("5.1")
|
18
19
|
::ActionView::Helpers::TagHelper.include(patch)
|
19
20
|
else
|
20
21
|
::ActionView::Helpers::TagHelper::TagBuilder.include(patch)
|
21
22
|
end
|
22
23
|
end
|
23
24
|
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def find_configuration_file
|
29
|
+
root = ::Rails.root.join("config")
|
30
|
+
extensions = ::RemoveDataAttributes::Configuration.supported_file_extensions
|
31
|
+
|
32
|
+
::Dir.glob(root.join("remove_data_attributes{#{extensions.join(',')}}")).first ||
|
33
|
+
raise(::RemoveDataAttributes::ConfigurationNotFound, root)
|
34
|
+
end
|
24
35
|
end
|
25
36
|
end
|
@@ -25,7 +25,7 @@ module RemoveDataAttributes
|
|
25
25
|
extend ::ActiveSupport::Concern
|
26
26
|
|
27
27
|
included do |klass|
|
28
|
-
patch = Module.new do
|
28
|
+
patch = ::Module.new do
|
29
29
|
define_method(method_name) do |options, escape = true|
|
30
30
|
options.is_a?(::Hash) ?
|
31
31
|
super(processor.call(options), escape) : super(options, escape)
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remove_data_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yasaichi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: railties
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ammeter
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: appraisal
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,7 +95,7 @@ dependencies:
|
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
98
|
+
name: simplecov
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - ">="
|
@@ -106,13 +120,16 @@ files:
|
|
106
120
|
- MIT-LICENSE
|
107
121
|
- README.md
|
108
122
|
- Rakefile
|
123
|
+
- lib/generators/remove_data_attributes/install/USAGE
|
124
|
+
- lib/generators/remove_data_attributes/install/install_generator.rb
|
125
|
+
- lib/generators/remove_data_attributes/install/templates/remove_data_attributes.yml
|
109
126
|
- lib/remove_data_attributes.rb
|
110
127
|
- lib/remove_data_attributes/configuration.rb
|
128
|
+
- lib/remove_data_attributes/errors.rb
|
111
129
|
- lib/remove_data_attributes/processor.rb
|
112
130
|
- lib/remove_data_attributes/railtie.rb
|
113
131
|
- lib/remove_data_attributes/tag_options_filter.rb
|
114
132
|
- lib/remove_data_attributes/version.rb
|
115
|
-
- lib/tasks/remove_data_attributes_tasks.rake
|
116
133
|
homepage: https://github.com/yasaichi/remove_data_attributes
|
117
134
|
licenses:
|
118
135
|
- MIT
|