actionview-component 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7e2a46384747c4c68bc6fe698a1b98c54a9bcbf6
4
- data.tar.gz: c0aafbee37e5a442b0fd9e2b2fc0fecb11dcfb8a
2
+ SHA256:
3
+ metadata.gz: 59fec154eafa7bbeab9aa226860751bf57f00d454335865f5c746b562969be03
4
+ data.tar.gz: 92a23a067a1c5d03a85935a137f8d1916132a6b6dcc01b86642e12c242a43bf7
5
5
  SHA512:
6
- metadata.gz: 2c0d364a6e93420fb2a25fdf7f0effcc772db775111ae2b1635fd53bb6f082802c1e331b2135de879c65ec4e463f65fb1b24372858a25a4ce7e7a83d152200a2
7
- data.tar.gz: 88f836465727a04b2980d786547b3d95e0e2c656fcb8c7dc408f7a185ec02da51f99aa24a27dd8b2ca2b147d9de71bf9d739bc156ee4ada81c4a329d751e5221
6
+ metadata.gz: 7c2fde6da24f0685ee606278f714bfadeceacdfe4f95e0696840b8c35da32d7e5ebb1fd8b1bdf510fa6b0cbe6eabbd16adef418eb095faf90d6a1b6923cae97a
7
+ data.tar.gz: 8afae7c9c555d7c358d039c98160e9b5cc3c882c776936d4a3d8ae5ca15a7bfa66975a86871cb0a1dd1ed538eee321ed3f26a2b9e978b8d391d3a45c18210c8d
@@ -0,0 +1,26 @@
1
+ name: Ruby on Rails
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ rails_version: ["5.2.3", "6.0.0", "master"]
11
+ ruby_version: ["2.5.x", "2.6.x"]
12
+ steps:
13
+ - uses: actions/checkout@master
14
+ - name: Setup Ruby
15
+ uses: actions/setup-ruby@v1
16
+ with:
17
+ version: ${{ matrix.ruby_version }}
18
+ - name: Build and test with Rake
19
+ run: |
20
+ gem install bundler
21
+ bundle update
22
+ bundle install --jobs 4 --retry 3
23
+ bundle exec rake
24
+ bundle exec rubocop
25
+ env:
26
+ RAILS_VERSION: ${{ matrix.rails_version }}
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+ /test/log/*
13
+
14
+ # Used by dotenv library to load environment variables.
15
+ # .env
16
+
17
+ ## Specific to RubyMotion:
18
+ .dat*
19
+ .repl_history
20
+ build/
21
+ *.bridgesupport
22
+ build-iPhoneOS/
23
+ build-iPhoneSimulator/
24
+
25
+ ## Specific to RubyMotion (use of CocoaPods):
26
+ #
27
+ # We recommend against adding the Pods directory to your .gitignore. However
28
+ # you should judge for yourself, the pros and cons are mentioned at:
29
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
30
+ #
31
+ # vendor/Pods/
32
+
33
+ ## Documentation cache and generated files:
34
+ /.yardoc/
35
+ /_yardoc/
36
+ /doc/
37
+ /rdoc/
38
+
39
+ ## Environment normalization:
40
+ /.bundle/
41
+ /vendor/bundle
42
+ /lib/bundler/man/
43
+
44
+ # for a library or gem, you might want to ignore these files since the code is
45
+ # intended to run in multiple environments; otherwise, check them in:
46
+ # Gemfile.lock
47
+ # .ruby-version
48
+ # .ruby-gemset
49
+
50
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
51
+ .rvmrc
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ inherit_gem:
2
+ rubocop-github:
3
+ - config/default.yml
4
+ - config/rails.yml
@@ -0,0 +1,76 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies within all project spaces, and it also applies when
49
+ an individual is representing the project or its community in public spaces.
50
+ Examples of representing a project or community include using an official
51
+ project e-mail address, posting via an official social media account, or acting
52
+ as an appointed representative at an online or offline event. Representation of
53
+ a project may be further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at opensource@github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,55 @@
1
+ ## Contributing
2
+
3
+ [fork]: https://github.com/github/actionview-component/fork
4
+ [pr]: https://github.com/github/actionview-component/compare
5
+ [style]: https://github.com/styleguide/ruby
6
+ [code-of-conduct]: CODE_OF_CONDUCT.md
7
+
8
+ Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great.
9
+
10
+ Contributions to this project are [released](https://help.github.com/articles/github-terms-of-service/#6-contributions-under-repository-license) to the public under the [project's open source license](LICENSE.txt).
11
+
12
+ Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms.
13
+
14
+ ## Submitting a pull request
15
+
16
+ 0. [Fork][fork] and clone the repository
17
+ 0. Configure and install the dependencies: `bundle`
18
+ 0. Make sure the tests pass on your machine: `rake`
19
+ 0. Create a new branch: `git checkout -b my-branch-name`
20
+ 0. Make your change, add tests, and make sure the tests still pass
21
+ 0. Push to your fork and [submit a pull request][pr]
22
+ 0. Pat your self on the back and wait for your pull request to be reviewed and merged.
23
+
24
+ Here are a few things you can do that will increase the likelihood of your pull request being accepted:
25
+
26
+ - Write tests.
27
+ - Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
28
+ - Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).
29
+
30
+ ## Releasing
31
+
32
+ If you are the current maintainer of this gem:
33
+
34
+ 1. Create a branch for the release: `git checkout -b release-vxx.xx.xx`
35
+ 1. Make sure your local dependencies are up to date: `bundle`
36
+ 1. Ensure that tests are green: `bundle exec rake`
37
+ 1. Build a test gem `GEM_VERSION=$(git describe --tags 2>/dev/null | sed 's/-/./g' | sed 's/v//') gem build actionview-component.gemspec`
38
+ 1. Test the test gem:
39
+ 1. Bump the Gemfile and Gemfile.lock versions for an app which relies on this gem
40
+ 1. Install the new gem locally
41
+ 1. Test behavior locally, branch deploy, whatever needs to happen
42
+ 1. Bump gem version in `action_view/component`.
43
+ 1. Make a PR to github/actionview-component.
44
+ 1. Build a local gem: `gem build actionview-component.gemspec`
45
+ 1. Merge github/actionview-component PR
46
+ 1. Tag and push: `git tag vx.xx.xx; git push --tags`
47
+ 1. Create a GitHub release with the pushed tag (https://github.com/github/actionview-component/releases/new) and populate it with a list of the commits from `git log --pretty=format:"- %s" --reverse refs/tags/[OLD TAG]...refs/tags/[NEW TAG]`
48
+ 1. Push to rubygems.pkg.github.com -- `gem push --key github --host https://rubygems.pkg.github.com/github actionview-component-VERSION.gem`. See [Configuring RubyGems for use with GitHub Package Registry][gpr] for more details.
49
+ 1. Push to rubygems.org -- `gem push actionview-component-VERSION.gem`
50
+
51
+ ## Resources
52
+
53
+ - [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
54
+ - [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
55
+ - [GitHub Help](https://help.github.com)
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
5
+
6
+ rails_version = "#{ENV['RAILS_VERSION'] || '5.2.3'}"
7
+
8
+ gem "rails", rails_version == "master" ? { github: "rails/rails" } : rails_version
data/Gemfile.lock ADDED
@@ -0,0 +1,160 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ actionview-component (0.2.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (5.2.3)
10
+ actionpack (= 5.2.3)
11
+ nio4r (~> 2.0)
12
+ websocket-driver (>= 0.6.1)
13
+ actionmailer (5.2.3)
14
+ actionpack (= 5.2.3)
15
+ actionview (= 5.2.3)
16
+ activejob (= 5.2.3)
17
+ mail (~> 2.5, >= 2.5.4)
18
+ rails-dom-testing (~> 2.0)
19
+ actionpack (5.2.3)
20
+ actionview (= 5.2.3)
21
+ activesupport (= 5.2.3)
22
+ rack (~> 2.0)
23
+ rack-test (>= 0.6.3)
24
+ rails-dom-testing (~> 2.0)
25
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
26
+ actionview (5.2.3)
27
+ activesupport (= 5.2.3)
28
+ builder (~> 3.1)
29
+ erubi (~> 1.4)
30
+ rails-dom-testing (~> 2.0)
31
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
32
+ activejob (5.2.3)
33
+ activesupport (= 5.2.3)
34
+ globalid (>= 0.3.6)
35
+ activemodel (5.2.3)
36
+ activesupport (= 5.2.3)
37
+ activerecord (5.2.3)
38
+ activemodel (= 5.2.3)
39
+ activesupport (= 5.2.3)
40
+ arel (>= 9.0)
41
+ activestorage (5.2.3)
42
+ actionpack (= 5.2.3)
43
+ activerecord (= 5.2.3)
44
+ marcel (~> 0.3.1)
45
+ activesupport (5.2.3)
46
+ concurrent-ruby (~> 1.0, >= 1.0.2)
47
+ i18n (>= 0.7, < 2)
48
+ minitest (~> 5.1)
49
+ tzinfo (~> 1.1)
50
+ arel (9.0.0)
51
+ ast (2.4.0)
52
+ builder (3.2.3)
53
+ concurrent-ruby (1.1.5)
54
+ crass (1.0.4)
55
+ erubi (1.8.0)
56
+ globalid (0.4.2)
57
+ activesupport (>= 4.2.0)
58
+ haml (5.1.2)
59
+ temple (>= 0.8.0)
60
+ tilt
61
+ i18n (1.6.0)
62
+ concurrent-ruby (~> 1.0)
63
+ jaro_winkler (1.5.3)
64
+ loofah (2.2.3)
65
+ crass (~> 1.0.2)
66
+ nokogiri (>= 1.5.9)
67
+ mail (2.7.1)
68
+ mini_mime (>= 0.1.1)
69
+ marcel (0.3.3)
70
+ mimemagic (~> 0.3.2)
71
+ method_source (0.9.2)
72
+ mimemagic (0.3.3)
73
+ mini_mime (1.0.2)
74
+ mini_portile2 (2.4.0)
75
+ minitest (5.11.3)
76
+ nio4r (2.4.0)
77
+ nokogiri (1.10.3)
78
+ mini_portile2 (~> 2.4.0)
79
+ parallel (1.17.0)
80
+ parser (2.6.3.0)
81
+ ast (~> 2.4.0)
82
+ rack (2.0.7)
83
+ rack-test (1.1.0)
84
+ rack (>= 1.0, < 3)
85
+ rails (5.2.3)
86
+ actioncable (= 5.2.3)
87
+ actionmailer (= 5.2.3)
88
+ actionpack (= 5.2.3)
89
+ actionview (= 5.2.3)
90
+ activejob (= 5.2.3)
91
+ activemodel (= 5.2.3)
92
+ activerecord (= 5.2.3)
93
+ activestorage (= 5.2.3)
94
+ activesupport (= 5.2.3)
95
+ bundler (>= 1.3.0)
96
+ railties (= 5.2.3)
97
+ sprockets-rails (>= 2.0.0)
98
+ rails-dom-testing (2.0.3)
99
+ activesupport (>= 4.2.0)
100
+ nokogiri (>= 1.6)
101
+ rails-html-sanitizer (1.2.0)
102
+ loofah (~> 2.2, >= 2.2.2)
103
+ railties (5.2.3)
104
+ actionpack (= 5.2.3)
105
+ activesupport (= 5.2.3)
106
+ method_source
107
+ rake (>= 0.8.7)
108
+ thor (>= 0.19.0, < 2.0)
109
+ rainbow (3.0.0)
110
+ rake (10.5.0)
111
+ rubocop (0.74.0)
112
+ jaro_winkler (~> 1.5.1)
113
+ parallel (~> 1.10)
114
+ parser (>= 2.6)
115
+ rainbow (>= 2.2.2, < 4.0)
116
+ ruby-progressbar (~> 1.7)
117
+ unicode-display_width (>= 1.4.0, < 1.7)
118
+ rubocop-github (0.13.0)
119
+ rubocop (~> 0.70)
120
+ rubocop-performance (~> 1.3.0)
121
+ rubocop-performance (1.3.0)
122
+ rubocop (>= 0.68.0)
123
+ ruby-progressbar (1.10.1)
124
+ slim (4.0.1)
125
+ temple (>= 0.7.6, < 0.9)
126
+ tilt (>= 2.0.6, < 2.1)
127
+ sprockets (3.7.2)
128
+ concurrent-ruby (~> 1.0)
129
+ rack (> 1, < 3)
130
+ sprockets-rails (3.2.1)
131
+ actionpack (>= 4.0)
132
+ activesupport (>= 4.0)
133
+ sprockets (>= 3.0.0)
134
+ temple (0.8.1)
135
+ thor (0.20.3)
136
+ thread_safe (0.3.6)
137
+ tilt (2.0.9)
138
+ tzinfo (1.2.5)
139
+ thread_safe (~> 0.1)
140
+ unicode-display_width (1.6.0)
141
+ websocket-driver (0.7.1)
142
+ websocket-extensions (>= 0.1.0)
143
+ websocket-extensions (0.1.4)
144
+
145
+ PLATFORMS
146
+ ruby
147
+
148
+ DEPENDENCIES
149
+ actionview-component!
150
+ bundler (>= 1.14)
151
+ haml (~> 5)
152
+ minitest (~> 5.0)
153
+ rails (= 5.2.3)
154
+ rake (~> 10.0)
155
+ rubocop (~> 0.59)
156
+ rubocop-github (~> 0.13.0)
157
+ slim (~> 4.0)
158
+
159
+ BUNDLED WITH
160
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 GitHub
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,14 +1,23 @@
1
1
  # ActionView::Component
2
- Short description and motivation.
2
+ `ActionView::Component` is a framework for building view components in Rails.
3
3
 
4
- ## Usage
5
- How to use my plugin.
4
+ **Current Status**: Used in production at GitHub. Because of this, all changes will be thoroughly vetted, which could slow down the process of contributing. We will do our best to actively communicate status of pull requests with any contributors. If you have any substantial changes that you would like to make, it would be great to first [open an issue](http://github.com/github/actionview-component/issues/new) to discuss them with us.
5
+
6
+ ## Roadmap
7
+
8
+ This gem is meant to serve as a precursor to upstreaming the `ActionView::Component` class into Rails. It also serves to enable the usage of `ActionView::Component` in older versions of Rails.
9
+
10
+ Preliminary support for rendering components was merged into Rails `6.1.0.alpha` in https://github.com/rails/rails/pull/36388. Assuming `ActionView::Component` makes it into Rails `6.1`, this gem will then exist to serve as a backport.
11
+
12
+ ## Compatibility
13
+
14
+ `actionview-component` is tested for compatibility with all combinations of Ruby `2.5`/`2.6` and Rails `5.2.3`/`6.0.0`/`6.1.0.alpha`.
6
15
 
7
16
  ## Installation
8
17
  Add this line to your application's Gemfile:
9
18
 
10
19
  ```ruby
11
- gem 'actionview-component'
20
+ gem "actionview-component"
12
21
  ```
13
22
 
14
23
  And then execute:
@@ -16,13 +25,180 @@ And then execute:
16
25
  $ bundle
17
26
  ```
18
27
 
19
- Or install it yourself as:
28
+ In `config/application.rb`, add:
29
+
20
30
  ```bash
21
- $ gem install actionview-component
31
+ require "action_view/component"
32
+ ```
33
+
34
+ ## Guide
35
+
36
+ ### What are components?
37
+
38
+ `ActionView::Component`s are Ruby classes that are used to render views. They take data as input and return output-safe HTML. Think of them as an evolution of the presenter/decorator/view model pattern, inspired by [React Components](https://reactjs.org/docs/react-component.html).
39
+
40
+ ### Why components?
41
+
42
+ In working on views in the Rails monolith at GitHub (which has over 3700 templates), we have run into several key pain points:
43
+
44
+ #### Testing
45
+
46
+ Currently, Rails encourages testing views via integration or system tests. This discourages us from testing our views thoroughly, due to the costly overhead of exercising the routing/controller layer, instead of just the view. It also often leads to partials being tested for each view they are included in, cheapening the benefit of DRYing up our views.
47
+
48
+ #### Code Coverage
49
+
50
+ Many common Ruby code coverage tools cannot properly handle coverage of views, making it difficult to audit how thorough our tests are and leading to gaps in our test suite.
51
+
52
+ #### Data Flow
53
+
54
+ Unlike a method declaration on an object, views do not declare the values they are expected to receive, making it hard to figure out what context is necessary to render them. This often leads to subtle bugs when we reuse a view across different contexts.
55
+
56
+ #### Standards
57
+
58
+ Our views often fail even the most basic standards of code quality we expect out of our Ruby classes: long methods, deep conditional nesting, and mystery guests abound.
59
+
60
+ ### What are the benefits?
61
+
62
+ #### Testing
63
+
64
+ `ActionView::Component` allows views to be unit-tested. In the main GitHub codebase, our unit tests run in around 25ms/test, vs. ~6s/test for integration tests.
65
+
66
+ #### Code Coverage
67
+
68
+ `ActionView::Component` is at least partially compatible with code coverage tools. We’ve seen some success with SimpleCov.
69
+
70
+ #### Data flow
71
+
72
+ By clearly defining the context necessary to render a component, we’ve found them to be easier to reuse than partials.
73
+
74
+ #### Performance
75
+
76
+ In early benchmarks, we’ve seen performance improvements over the existing rendering pipeline. For a test page with nested renders 10 levels deep, we’re seeing around a 5x increase in speed over partials:
77
+
78
+ ```
79
+ Comparison:
80
+ component: 6515.4 i/s
81
+ partial: 1251.2 i/s - 5.21x slower
82
+ ```
83
+
84
+ _Rails 6.1.0.alpha, [joelhawksley/actionview-component-demo](https://github.com/joelhawksley/actionview-component-demo), /benchmark route, via `RAILS_ENV=production rails s`, measured with [evanphx/benchmark-ips](https://github.com/evanphx/benchmark-ips)_
85
+
86
+ ### When should I use components?
87
+
88
+ Components are most effective in cases where view code is reused or needs to be tested directly.
89
+
90
+ ### Building components
91
+
92
+ Components are subclasses of `ActionView::Component` and live in `app/components`. You may wish to create an `ApplicationComponent` that is a subclass of `ActionView::Component` and inherit from that instead.
93
+
94
+ Components support ActiveModel validations. Components are validated after initialization, but before rendering.
95
+
96
+ Content passed to an `ActionView::Component` as a block is captured and assigned to the `content` accessor.
97
+
98
+ #### Implementation
99
+
100
+ An `ActionView::Component` is a Ruby file and corresponding template file (in any format supported by Rails) with the same base name:
101
+
102
+ `app/components/test_component.rb`:
103
+ ```ruby
104
+ class TestComponent < ActionView::Component
105
+ validates :content, :title, presence: true
106
+
107
+ def initialize(title:)
108
+ @title = title
109
+ end
110
+
111
+ private
112
+
113
+ attr_reader :title
114
+ end
115
+ ```
116
+
117
+ `app/components/test_component.html.erb`:
118
+ ```erb
119
+ <span title="<%= title %>"><%= content %></span>
120
+ ```
121
+
122
+ We can render it in a view as:
123
+
124
+ ```erb
125
+ <%= render(TestComponent.new(title: "my title")) do %>
126
+ Hello, World!
127
+ <% end %>
128
+ ```
129
+
130
+ Which returns:
131
+
132
+ ```html
133
+ <span title="my title">Hello, World!</span>
134
+ ```
135
+
136
+ #### Error case
137
+
138
+ If the component is rendered with a blank title:
139
+
140
+ ```erb
141
+ <%= render(TestComponent.new(title: "")) do %>
142
+ Hello, World!
143
+ <% end %>
144
+ ```
145
+
146
+ An error will be raised:
147
+
148
+ `ActiveModel::ValidationError: Validation failed: Title can't be blank`
149
+
150
+ ### Testing
151
+
152
+ Components are unit tested directly. The `render_component` test helper renders a component and wraps the result in `Nokogiri.HTML`, allowing us to test the component above as:
153
+
154
+ ```ruby
155
+ require "action_view/component_test_helpers"
156
+
157
+ class MyComponentTest < Minitest::Test
158
+ include ActionView::ComponentTestHelpers
159
+
160
+ def test_render_component
161
+ assert_equal(
162
+ %(<span title="my title">Hello, World!</span>),
163
+ render_component(TestComponent.new(title: "my title")) { "Hello, World!" }.css("span").to_html
164
+ )
165
+ end
166
+ end
22
167
  ```
23
168
 
169
+ In general, we’ve found it makes the most sense to test components based on their rendered HTML.
170
+
171
+ ## Frequently Asked Questions
172
+
173
+ ### Can I use other templating languages besides ERB?
174
+
175
+ Yes. This gem is tested against ERB, Haml, and Slim, but it should support most Rails template handlers.
176
+
177
+ ### What happened to inline templates?
178
+
179
+ Inline templates have been removed (for now) due to concerns raised by [@soutaro](https://github.com/soutaro) regarding compatibility with the type systems being developed for Ruby 3.
180
+
181
+ ### Isn't this just like X library?
182
+
183
+ `ActionView::Component` is far from a novel idea! Popular implementations of view components in Ruby include, but are not limited to:
184
+
185
+ - [trailblazer/cells](https://github.com/trailblazer/cells)
186
+ - [dry-rb/dry-view](https://github.com/dry-rb/dry-view)
187
+ - [komposable/komponent](https://github.com/komposable/komponent)
188
+ - [activeadmin/arbre](https://github.com/activeadmin/arbre)
189
+
190
+ ## Resources
191
+
192
+ - [Rethinking the View Layer with Components, RailsConf 2019](https://www.youtube.com/watch?v=y5Z5a6QdA-M)
193
+ - [Introducing ActionView::Component with Joel Hawksley, Ruby on Rails Podcast](http://5by5.tv/rubyonrails/276)
194
+ - [Rails to Introduce View Components, Dev.to](https://dev.to/andy/rails-to-introduce-view-components-3ome)
195
+ - [ActionView::Components in Rails 6.1, Drifting Ruby](https://www.driftingruby.com/episodes/actionview-components-in-rails-6-1)
196
+ - [Demo repository, actionview-component-demo](https://github.com/joelhawksley/actionview-component-demo)
197
+
24
198
  ## Contributing
25
- Contribution directions go here.
199
+
200
+ Bug reports and pull requests are welcome on GitHub at https://github.com/github/actionview-component. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. We recommend reading the [contributing guide](./CONTRIBUTING.md) as well.
26
201
 
27
202
  ## License
203
+
28
204
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,34 +1,12 @@
1
- begin
2
- require 'bundler/setup'
3
- rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
- end
6
-
7
- require 'rdoc/task'
8
-
9
- RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'ActionView::Component'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.md')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
15
- end
16
-
17
-
1
+ # frozen_string_literal: true
18
2
 
19
-
20
-
21
-
22
- require 'bundler/gem_tasks'
23
-
24
- require 'rake/testtask'
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
25
5
 
26
6
  Rake::TestTask.new(:test) do |t|
27
- t.libs << 'lib'
28
- t.libs << 'test'
29
- t.pattern = 'test/**/*_test.rb'
30
- t.verbose = false
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
31
10
  end
32
11
 
33
-
34
12
  task default: :test
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path("../lib", __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "actionview-component"
9
+ spec.version = "1.0.0"
10
+ spec.authors = ["GitHub Open Source"]
11
+ spec.email = ["opensource+actionview-component@github.com"]
12
+
13
+ spec.summary = %q{View components for Rails}
14
+ spec.description = %q{View components for Rails, intended for upstreaming in Rails 6.1}
15
+ spec.homepage = "https://github.com/github/actionview-component"
16
+ spec.license = "MIT"
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{^(test|spec|features)/})
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.required_ruby_version = ">= 2.5.0"
35
+
36
+ spec.add_development_dependency "bundler", ">= 1.14"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "minitest", "~> 5.0"
39
+ spec.add_development_dependency "haml", "~> 5"
40
+ spec.add_development_dependency "slim", "~> 4.0"
41
+ spec.add_development_dependency "rubocop", "~> 0.59"
42
+ spec.add_development_dependency "rubocop-github", "~> 0.13.0"
43
+ end
@@ -1,23 +1,132 @@
1
- module ActionView
2
- class Component < Base
1
+ # frozen_string_literal: true
2
+
3
+ # Monkey patch ActionView::Base#render to support ActionView::Component
4
+ #
5
+ # Upstreamed in https://github.com/rails/rails/pull/36388
6
+ # Necessary for Rails versions < 6.1.0.alpha
7
+ class ActionView::Base
8
+ module RenderMonkeyPatch
9
+ def render(component, _ = nil, &block)
10
+ return super unless component.respond_to?(:render_in)
11
+
12
+ component.render_in(self, &block)
13
+ end
3
14
  end
4
15
 
5
- class TemplateRenderer
6
- module ComponentTemplates
7
- def render(context, options)
8
- if options.key?(:component)
9
- name = options[:component]
10
- klass = "#{name}_component".classify.constantize
11
- args = options.except(:component)
12
- context = args.empty? ? klass.new : klass.new(args)
16
+ prepend RenderMonkeyPatch unless Rails::VERSION::MINOR > 0 && Rails::VERSION::MAJOR == 6
17
+ end
18
+
19
+ module ActionView
20
+ class Component < ActionView::Base
21
+ VERSION = "1.0.0"
22
+
23
+ include ActiveModel::Validations
24
+
25
+ # Entrypoint for rendering components. Called by ActionView::Base#render.
26
+ #
27
+ # view_context: ActionView context from calling view
28
+ # args(hash): params to be passed to component being rendered
29
+ # block: optional block to be captured within the view context
30
+ #
31
+ # returns HTML that has been escaped by the respective template handler
32
+ #
33
+ # Example subclass:
34
+ #
35
+ # app/components/my_component.rb:
36
+ # class MyComponent < ActionView::Component
37
+ # def initialize(title:)
38
+ # @title = title
39
+ # end
40
+ # end
41
+ #
42
+ # app/components/my_component.html.erb
43
+ # <span title="<%= @title %>">Hello, <%= content %>!</span>
44
+ #
45
+ # In use:
46
+ # <%= render MyComponent.new(title: "greeting") do %>world<% end %>
47
+ # returns:
48
+ # <span title="greeting">Hello, world!</span>
49
+ #
50
+ def render_in(view_context, *args, &block)
51
+ self.class.compile
52
+ @content = view_context.capture(&block) if block_given?
53
+ validate!
54
+ call
55
+ end
56
+
57
+ def initialize(*); end
58
+
59
+ class << self
60
+ def inherited(child)
61
+ child.include Rails.application.routes.url_helpers unless child < Rails.application.routes.url_helpers
62
+
63
+ super
64
+ end
65
+
66
+ # Compile template to #call instance method, assuming it hasn't been compiled already.
67
+ # We could in theory do this on app boot, at least in production environments.
68
+ # Right now this just compiles the template the first time the component is rendered.
69
+ def compile
70
+ return if @compiled
71
+
72
+ class_eval("def call; @output_buffer = ActionView::OutputBuffer.new; #{compiled_template}; end")
73
+
74
+ @compiled = true
75
+ end
76
+
77
+ private
13
78
 
14
- super context, template: "components/#{name}"
79
+ def compiled_template
80
+ handler = ActionView::Template.handler_for_extension(File.extname(template_file_path).gsub(".", ""))
81
+ template = File.read(template_file_path)
82
+
83
+ if handler.method(:call).parameters.length > 1
84
+ handler.call(DummyTemplate.new, template)
15
85
  else
16
- super
86
+ handler.call(DummyTemplate.new(template))
17
87
  end
18
88
  end
89
+
90
+ def template_file_path
91
+ raise NotImplementedError.new("#{self} must implement #initialize.") unless self.instance_method(:initialize).owner == self
92
+
93
+ filename = self.instance_method(:initialize).source_location[0]
94
+ filename_without_extension = filename[0..-(File.extname(filename).length + 1)]
95
+ sibling_files = Dir["#{filename_without_extension}.*"] - [filename]
96
+
97
+ if sibling_files.length > 1
98
+ raise StandardError.new("More than one template found for #{self}. There can only be one sidecar template file per component.")
99
+ end
100
+
101
+ if sibling_files.length == 0
102
+ raise NotImplementedError.new(
103
+ "Could not find a template for #{self}. Either define a .template method or add a sidecar template file."
104
+ )
105
+ end
106
+
107
+ sibling_files[0]
108
+ end
19
109
  end
20
110
 
21
- prepend ComponentTemplates
111
+ class DummyTemplate
112
+ attr_reader :source
113
+
114
+ def initialize(source = nil)
115
+ @source = source
116
+ end
117
+
118
+ def identifier
119
+ ""
120
+ end
121
+
122
+ # we'll eventually want to update this to support other types
123
+ def type
124
+ "text/html"
125
+ end
126
+ end
127
+
128
+ private
129
+
130
+ attr_reader :content
22
131
  end
23
132
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionView
4
+ module ComponentTestHelpers
5
+ def render_component(component, &block)
6
+ Nokogiri::HTML(component.render_in(ApplicationController.new.view_context, &block))
7
+ end
8
+ end
9
+ end
data/script/bootstrap ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
data/script/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "action_view/component"
6
+
7
+ require "irb"
8
+ IRB.start(__FILE__)
data/script/install ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle exec rake install
data/script/release ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle exec rake release
data/script/test ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle exec rake
metadata CHANGED
@@ -1,87 +1,143 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionview-component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - Godfrey Chan
7
+ - GitHub Open Source
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-15 00:00:00.000000000 Z
11
+ date: 2019-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: actionview
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: 5.0.1
20
- type: :runtime
33
+ version: '10.0'
34
+ type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: 5.0.1
40
+ version: '10.0'
27
41
  - !ruby/object:Gem::Dependency
28
- name: railties
42
+ name: minitest
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: 5.0.1
34
- type: :runtime
47
+ version: '5.0'
48
+ type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: 5.0.1
54
+ version: '5.0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: rails
56
+ name: haml
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: 5.0.1
61
+ version: '5'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: 5.0.1
68
+ version: '5'
55
69
  - !ruby/object:Gem::Dependency
56
- name: sqlite3
70
+ name: slim
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - ">="
73
+ - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: '0'
75
+ version: '4.0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - ">="
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.59'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.59'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-github
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.13.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
67
109
  - !ruby/object:Gem::Version
68
- version: '0'
69
- description:
110
+ version: 0.13.0
111
+ description: View components for Rails, intended for upstreaming in Rails 6.1
70
112
  email:
71
- - godfreykfc@gmail.com
113
+ - opensource+actionview-component@github.com
72
114
  executables: []
73
115
  extensions: []
74
116
  extra_rdoc_files: []
75
117
  files:
76
- - MIT-LICENSE
118
+ - ".github/workflows/ruby_on_rails.yml"
119
+ - ".gitignore"
120
+ - ".rubocop.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - CONTRIBUTING.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
77
126
  - README.md
78
127
  - Rakefile
128
+ - actionview-component.gemspec
79
129
  - lib/action_view/component.rb
80
- - lib/actionview/component.rb
81
- homepage: http://github.com/chancancode/actionview-component
130
+ - lib/action_view/component_test_helpers.rb
131
+ - script/bootstrap
132
+ - script/console
133
+ - script/install
134
+ - script/release
135
+ - script/test
136
+ homepage: https://github.com/github/actionview-component
82
137
  licenses:
83
138
  - MIT
84
- metadata: {}
139
+ metadata:
140
+ allowed_push_host: https://rubygems.org
85
141
  post_install_message:
86
142
  rdoc_options: []
87
143
  require_paths:
@@ -90,17 +146,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
146
  requirements:
91
147
  - - ">="
92
148
  - !ruby/object:Gem::Version
93
- version: '0'
149
+ version: 2.5.0
94
150
  required_rubygems_version: !ruby/object:Gem::Requirement
95
151
  requirements:
96
152
  - - ">="
97
153
  - !ruby/object:Gem::Version
98
154
  version: '0'
99
155
  requirements: []
100
- rubyforge_project:
101
- rubygems_version: 2.5.1
156
+ rubygems_version: 3.0.3
102
157
  signing_key:
103
158
  specification_version: 4
104
- summary: ActionView::Component
159
+ summary: View components for Rails
105
160
  test_files: []
106
- has_rdoc:
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright 2017 Godfrey Chan
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,5 +0,0 @@
1
- module ActionView
2
- eager_autoload do
3
- autoload :Component
4
- end
5
- end