remove_data_attributes 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f452b99f8cf397d4fe5847d5ba2acc358b0a4f9
4
- data.tar.gz: 6fd3924500e127fbea0e3e24a7aaafd10c1f0921
3
+ metadata.gz: 69172fe3cb093195772529974892d07ac0b1d025
4
+ data.tar.gz: fbb4df2602315e76d4dea5bda7b5e57eacea462f
5
5
  SHA512:
6
- metadata.gz: 1f9ef7647b704567c48f3f48e2b99754e14b5e8f3787c6aa64d19b8e183b2eec07eb345ccb28573f769172a57ca9fb815943d1cad51721c958962c3c8ee98278
7
- data.tar.gz: 2a8ed8b6044246b76c13d4d13e1ddfb92d0d7a605ba8e2597617199679db79735351de888a57c8012fc5892a743bd675c4c68196ed27b26fc0d948ce9f2061af
6
+ metadata.gz: c24bd87c412edf0fec11d8eef4d1211b94ee4b4c73877ca4a66c8eddf91001a1604ebd145749dcb255918228ea5b9d3b9ad285b38fe47ace7df0ce9fb827b290
7
+ data.tar.gz: 62dc161345a0f579baad575c02537dad0bf4a9c05c01db61b9a5c1dc3fb0a4acf68b48807755e03d6e8b56752fbcd894558f49bb7d8a0c22e45c0f36ae589181
@@ -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
@@ -1,4 +1,4 @@
1
- Copyright 2017 yasaichi
1
+ Copyright 2017 Yuichi Goto
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,14 +1,22 @@
1
- # RemoveDataAttributes
2
- Short description and motivation.
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
- ## Usage
5
- How to use my plugin.
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
- Or install it yourself as:
20
- ```bash
21
- $ gem install remove_data_attributes
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="&#x2713;" />
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
- Contribution directions go here.
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
@@ -16,9 +16,8 @@ RDoc::Task.new(:rdoc) do |rdoc|
16
16
  rdoc.rdoc_files.include("lib/**/*.rb")
17
17
  end
18
18
 
19
-
20
-
21
-
22
-
19
+ require "rspec/core/rake_task"
20
+ RSpec::Core::RakeTask.new(:spec)
21
+ task default: :spec
23
22
 
24
23
  require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Creates a configuration file of remove_data_attributes.
3
+
4
+ Example:
5
+ `rails generate remove_data_attributes:install`
6
+
7
+ This will create:
8
+ config/remove_data_attributes.yml
@@ -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
@@ -0,0 +1,3 @@
1
+ # Configure which data attributes should be removed.
2
+ data_attributes:
3
+ # - data-test
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "remove_data_attributes/configuration"
4
3
  require "remove_data_attributes/railtie"
5
4
  require "remove_data_attributes/version"
@@ -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 << self
5
- attr_writer :configuration
7
+ class Configuration
8
+ SUPPORTED_FILE_FORMATS = {
9
+ "YAML" => [".yml", ".yaml"].freeze
10
+ }.freeze
6
11
 
7
- def configuration
8
- @configuration ||= ::RemoveDataAttributes::Configuration.new
9
- end
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
- def configure
12
- yield(configuration) if block_given?
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
- class Configuration
17
- attr_writer :data_attributes
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
- config.after_initialize do
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RemoveDataAttributes
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
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.1.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-10 00:00:00.000000000 Z
11
+ date: 2017-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
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: sqlite3
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
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
- # desc "Explaining what the task does"
3
- # task :remove_data_attributes do
4
- # # Task goes here
5
- # end