actionview-component 1.3.6 → 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby_on_rails.yml +2 -6
- data/CHANGELOG.md +64 -0
- data/Gemfile.lock +5 -5
- data/README.md +101 -3
- data/actionview-component.gemspec +1 -1
- data/lib/action_view/component.rb +5 -0
- data/lib/action_view/component/base.rb +110 -84
- data/lib/action_view/component/monkey_patch.rb +27 -0
- data/lib/action_view/component/preview.rb +106 -0
- data/lib/action_view/component/railtie.rb +51 -0
- data/lib/action_view/component/test_helpers.rb +9 -1
- data/lib/action_view/component/version.rb +2 -2
- data/lib/rails/generators/component/USAGE +13 -0
- data/lib/rails/generators/component/component_generator.rb +50 -0
- data/lib/rails/generators/component/templates/component.html.erb.tt +5 -0
- data/lib/rails/generators/component/templates/component.rb.tt +9 -0
- data/lib/rails/generators/rspec/component_generator.rb +19 -0
- data/lib/rails/generators/rspec/templates/component_spec.rb.tt +13 -0
- data/lib/rails/generators/test_unit/component_generator.rb +20 -0
- data/lib/rails/generators/test_unit/templates/component_test.rb.tt +12 -0
- data/lib/railties/lib/rails.rb +6 -0
- data/lib/railties/lib/rails/component_examples_controller.rb +9 -0
- data/lib/railties/lib/rails/components_controller.rb +55 -0
- data/lib/railties/lib/rails/templates/rails/components/index.html.erb +8 -0
- data/lib/railties/lib/rails/templates/rails/components/preview.html.erb +1 -0
- data/lib/railties/lib/rails/templates/rails/components/previews.html.erb +6 -0
- data/lib/railties/lib/rails/templates/rails/examples/show.html.erb +1 -0
- metadata +25 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00a684cf9aea1c6f1a94dd0c980fd7d1889e2b17fb165006817063de69b6f721
|
4
|
+
data.tar.gz: 30766fd4333fb8d708369e48192b000ba4a5136898bd82e98273d5fc9445fb67
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aeaf7ae830c1b2e099a974228c0c072c41481c9ddd0f000c488727066df837f872b9616f064962bc3e8adee2fd20d86e0894d97cbc1a6c049faa5b6eea4636b1
|
7
|
+
data.tar.gz: ef32ebd537e7d36ae192b29b7aa756a8bf2efca96ef19d87e751e0dea11369616e0fc6e8a5848c2ea45230eb7eb46fac18e6e6eb6778ee58749193f22d3f2a44
|
@@ -8,22 +8,18 @@ jobs:
|
|
8
8
|
strategy:
|
9
9
|
matrix:
|
10
10
|
rails_version: [5.0.0, 5.2.3, 6.0.0, master]
|
11
|
-
ruby_version: [2.
|
11
|
+
ruby_version: [2.4.x, 2.5.x, 2.6.x]
|
12
12
|
exclude:
|
13
13
|
- rails_version: master
|
14
14
|
ruby_version: 2.4.x
|
15
|
-
- rails_version: master
|
16
|
-
ruby_version: 2.3.x
|
17
15
|
- rails_version: 6.0.0
|
18
16
|
ruby_version: 2.4.x
|
19
|
-
- rails_version: 6.0.0
|
20
|
-
ruby_version: 2.3.x
|
21
17
|
steps:
|
22
18
|
- uses: actions/checkout@master
|
23
19
|
- name: Setup Ruby
|
24
20
|
uses: actions/setup-ruby@v1
|
25
21
|
with:
|
26
|
-
version: ${{ matrix.ruby_version }}
|
22
|
+
ruby-version: ${{ matrix.ruby_version }}
|
27
23
|
- name: Build and test with Rake
|
28
24
|
run: |
|
29
25
|
gem install bundler:1.14.0
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,67 @@
|
|
1
|
+
# v1.5.3
|
2
|
+
|
3
|
+
* Add support for RSpec to generators.
|
4
|
+
|
5
|
+
*Dylan Clark, Ryan Workman*
|
6
|
+
|
7
|
+
* Require controllers as part of setting autoload paths.
|
8
|
+
|
9
|
+
*Joel Hawksley*
|
10
|
+
|
11
|
+
# v1.5.2
|
12
|
+
|
13
|
+
* Disable eager loading initializer.
|
14
|
+
|
15
|
+
*Kasper Meyer*
|
16
|
+
|
17
|
+
# v1.5.1
|
18
|
+
|
19
|
+
* Update railties class to work with Rails 6.
|
20
|
+
|
21
|
+
*Juan Manuel Ramallo*
|
22
|
+
|
23
|
+
# v1.5.0
|
24
|
+
|
25
|
+
Note: `actionview-component` is now loaded by requiring `actionview/component`, not `actionview/component/base`.
|
26
|
+
|
27
|
+
* Fix issue with generating component method signatures.
|
28
|
+
|
29
|
+
*Ryan Workman, Dylan Clark*
|
30
|
+
|
31
|
+
* Create component generator.
|
32
|
+
|
33
|
+
*Vinicius Stock*
|
34
|
+
|
35
|
+
* Add helpers proxy.
|
36
|
+
|
37
|
+
*Kasper Meyer*
|
38
|
+
|
39
|
+
* Introduce ActionView::Component::Previews.
|
40
|
+
|
41
|
+
*Juan Manuel Ramallo*
|
42
|
+
|
43
|
+
# v1.4.0
|
44
|
+
|
45
|
+
* Fix bug where components broke in application paths with periods.
|
46
|
+
|
47
|
+
*Anton, Joel Hawksley*
|
48
|
+
|
49
|
+
* Add support for `cache_if` in component templates.
|
50
|
+
|
51
|
+
*Aaron Patterson, Joel Hawksley*
|
52
|
+
|
53
|
+
* Add support for variants.
|
54
|
+
|
55
|
+
*Juan Manuel Ramallo*
|
56
|
+
|
57
|
+
* Fix bug in virtual path lookup.
|
58
|
+
|
59
|
+
*Juan Manuel Ramallo*
|
60
|
+
|
61
|
+
* Preselect the rendered component in render_inline.
|
62
|
+
|
63
|
+
*Elia Schito*
|
64
|
+
|
1
65
|
# v1.3.6
|
2
66
|
|
3
67
|
* Allow template file names without format.
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
actionview-component (1.3
|
4
|
+
actionview-component (1.5.3)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -64,7 +64,7 @@ GEM
|
|
64
64
|
ast (2.4.0)
|
65
65
|
builder (3.2.3)
|
66
66
|
concurrent-ruby (1.1.5)
|
67
|
-
crass (1.0.
|
67
|
+
crass (1.0.5)
|
68
68
|
erubi (1.8.0)
|
69
69
|
globalid (0.4.2)
|
70
70
|
activesupport (>= 4.2.0)
|
@@ -74,7 +74,7 @@ GEM
|
|
74
74
|
i18n (1.6.0)
|
75
75
|
concurrent-ruby (~> 1.0)
|
76
76
|
jaro_winkler (1.5.3)
|
77
|
-
loofah (2.
|
77
|
+
loofah (2.3.1)
|
78
78
|
crass (~> 1.0.2)
|
79
79
|
nokogiri (>= 1.5.9)
|
80
80
|
mail (2.7.1)
|
@@ -87,7 +87,7 @@ GEM
|
|
87
87
|
mini_portile2 (2.4.0)
|
88
88
|
minitest (5.1.0)
|
89
89
|
nio4r (2.5.2)
|
90
|
-
nokogiri (1.10.
|
90
|
+
nokogiri (1.10.5)
|
91
91
|
mini_portile2 (~> 2.4.0)
|
92
92
|
parallel (1.17.0)
|
93
93
|
parser (2.6.3.0)
|
@@ -168,7 +168,7 @@ DEPENDENCIES
|
|
168
168
|
minitest (= 5.1.0)
|
169
169
|
rails (= 6.0.0)
|
170
170
|
rake (~> 10.0)
|
171
|
-
rubocop (
|
171
|
+
rubocop (= 0.74)
|
172
172
|
rubocop-github (~> 0.13.0)
|
173
173
|
slim (~> 4.0)
|
174
174
|
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ As the goal of this gem is to be upstreamed into Rails, it is designed to integr
|
|
15
15
|
|
16
16
|
## Compatibility
|
17
17
|
|
18
|
-
`actionview-component` is tested for compatibility with combinations of Ruby `2.
|
18
|
+
`actionview-component` is tested for compatibility with combinations of Ruby `2.4`/`2.5`/`2.6` and Rails `5.0.0`/`5.2.3`/`6.0.0`/`6.1.0.alpha`.
|
19
19
|
|
20
20
|
## Installation
|
21
21
|
Add this line to your application's Gemfile:
|
@@ -32,7 +32,7 @@ $ bundle
|
|
32
32
|
In `config/application.rb`, add:
|
33
33
|
|
34
34
|
```bash
|
35
|
-
require "action_view/component
|
35
|
+
require "action_view/component"
|
36
36
|
```
|
37
37
|
|
38
38
|
## Guide
|
@@ -97,10 +97,26 @@ Components are subclasses of `ActionView::Component::Base` and live in `app/comp
|
|
97
97
|
|
98
98
|
Component class names end in -`Component`.
|
99
99
|
|
100
|
+
Component module names are plural, as they are for controllers. (`Users::AvatarComponent`)
|
101
|
+
|
100
102
|
Components support ActiveModel validations. Components are validated after initialization, but before rendering.
|
101
103
|
|
102
104
|
Content passed to an `ActionView::Component` as a block is captured and assigned to the `content` accessor.
|
103
105
|
|
106
|
+
#### Quick start
|
107
|
+
|
108
|
+
Use the component generator to create a new `ActionView::Component`.
|
109
|
+
|
110
|
+
The generator accepts the component name and the list of accepted properties as arguments:
|
111
|
+
|
112
|
+
```bash
|
113
|
+
bin/rails generate component Example title content
|
114
|
+
invoke test_unit
|
115
|
+
create test/components/example_component_test.rb
|
116
|
+
create app/components/example_component.rb
|
117
|
+
create app/components/example_component.html.erb
|
118
|
+
```
|
119
|
+
|
104
120
|
#### Implementation
|
105
121
|
|
106
122
|
An `ActionView::Component` is a Ruby file and corresponding template file (in any format supported by Rails) with the same base name:
|
@@ -178,7 +194,7 @@ class MyComponentTest < Minitest::Test
|
|
178
194
|
def test_render_component
|
179
195
|
assert_equal(
|
180
196
|
%(<span title="my title">Hello, World!</span>),
|
181
|
-
render_inline(TestComponent, title: "my title") { "Hello, World!" }.
|
197
|
+
render_inline(TestComponent, title: "my title") { "Hello, World!" }.to_html
|
182
198
|
)
|
183
199
|
end
|
184
200
|
end
|
@@ -186,6 +202,88 @@ end
|
|
186
202
|
|
187
203
|
In general, we’ve found it makes the most sense to test components based on their rendered HTML.
|
188
204
|
|
205
|
+
#### Action Pack Variants
|
206
|
+
|
207
|
+
To test a specific variant you can wrap your test with the `with_variant` helper method as:
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
def test_render_component_for_tablet
|
211
|
+
with_variant :tablet do
|
212
|
+
assert_equal(
|
213
|
+
%(<span title="my title">Hello, tablets!</span>),
|
214
|
+
render_inline(TestComponent, title: "my title") { "Hello, tablets!" }.css("span").to_html
|
215
|
+
)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
```
|
219
|
+
|
220
|
+
### Previewing Components
|
221
|
+
`ActionView::Component::Preview`s provide a way to see how components look by visiting a special URL that renders them.
|
222
|
+
In the previous example, the preview class for `TestComponent` would be called `TestComponentPreview` and located in `test/components/previews/test_component_preview.rb`.
|
223
|
+
To see the preview of the component with a given title, implement a method that renders the component.
|
224
|
+
You can define as many examples as you want:
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
# test/components/previews/test_component_preview.rb
|
228
|
+
|
229
|
+
class TestComponentPreview < ActionView::Component::Preview
|
230
|
+
def with_default_title
|
231
|
+
render(TestComponent, title: "Test component default")
|
232
|
+
end
|
233
|
+
|
234
|
+
def with_long_title
|
235
|
+
render(TestComponent, title: "This is a really long title to see how the component renders this")
|
236
|
+
end
|
237
|
+
end
|
238
|
+
```
|
239
|
+
|
240
|
+
The previews will be available in <http://localhost:3000/rails/components/test_component/with_default_title>
|
241
|
+
and <http://localhost:3000/rails/components/test_component/with_long_title>.
|
242
|
+
|
243
|
+
Previews use the application layout by default, but you can also use other layouts from your app:
|
244
|
+
|
245
|
+
```ruby
|
246
|
+
# test/components/previews/test_component_preview.rb
|
247
|
+
|
248
|
+
class TestComponentPreview < ActionView::Component::Preview
|
249
|
+
layout "admin"
|
250
|
+
|
251
|
+
...
|
252
|
+
end
|
253
|
+
```
|
254
|
+
|
255
|
+
By default, the preview classes live in `test/components/previews`.
|
256
|
+
This can be configured using the `preview_path` option.
|
257
|
+
For example, if you want to use `lib/component_previews`, set the following in `config/application.rb`:
|
258
|
+
|
259
|
+
```ruby
|
260
|
+
config.action_view_component.preview_path = "#{Rails.root}/lib/component_previews"
|
261
|
+
```
|
262
|
+
|
263
|
+
### Setting up RSpec
|
264
|
+
|
265
|
+
If you're using RSpec, you can configure component specs to have access to test helpers. Add the following to
|
266
|
+
`spec/rails_helper.rb`:
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
require "action_view/component/test_helpers"
|
270
|
+
|
271
|
+
RSpec.configure do |config|
|
272
|
+
# ...
|
273
|
+
|
274
|
+
# Ensure that the test helpers are available in component specs
|
275
|
+
config.include ActionView::Component::TestHelpers, type: :component
|
276
|
+
end
|
277
|
+
```
|
278
|
+
|
279
|
+
Specs created by the generator should now have access to test helpers like `render_inline`.
|
280
|
+
|
281
|
+
To use component previews, set the following in `config/application.rb`:
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
config.action_view_component.preview_path = "#{Rails.root}/spec/components/previews"
|
285
|
+
```
|
286
|
+
|
189
287
|
## Frequently Asked Questions
|
190
288
|
|
191
289
|
### Can I use other templating languages besides ERB?
|
@@ -39,6 +39,6 @@ Gem::Specification.new do |spec|
|
|
39
39
|
spec.add_development_dependency "minitest", "= 5.1.0"
|
40
40
|
spec.add_development_dependency "haml", "~> 5"
|
41
41
|
spec.add_development_dependency "slim", "~> 4.0"
|
42
|
-
spec.add_development_dependency "rubocop", "
|
42
|
+
spec.add_development_dependency "rubocop", "= 0.74"
|
43
43
|
spec.add_development_dependency "rubocop-github", "~> 0.13.0"
|
44
44
|
end
|
@@ -1,37 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
class ActionView::Base
|
8
|
-
module RenderMonkeyPatch
|
9
|
-
def render(options = {}, args = {}, &block)
|
10
|
-
if options.respond_to?(:render_in)
|
11
|
-
ActiveSupport::Deprecation.warn(
|
12
|
-
"passing component instances to `render` has been deprecated and will be removed in v2.0.0. Use `render MyComponent, foo: :bar` instead."
|
13
|
-
)
|
14
|
-
|
15
|
-
options.render_in(self, &block)
|
16
|
-
elsif options.is_a?(Class) && options < ActionView::Component::Base
|
17
|
-
options.new(args).render_in(self, &block)
|
18
|
-
elsif options.is_a?(Hash) && options.has_key?(:component)
|
19
|
-
options[:component].new(options[:locals]).render_in(self, &block)
|
20
|
-
else
|
21
|
-
super
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
prepend RenderMonkeyPatch
|
27
|
-
end
|
3
|
+
require "active_model"
|
4
|
+
require "action_view"
|
5
|
+
require "active_support/configurable"
|
6
|
+
require_relative "preview"
|
28
7
|
|
29
8
|
module ActionView
|
30
9
|
module Component
|
31
10
|
class Base < ActionView::Base
|
32
11
|
include ActiveModel::Validations
|
33
12
|
include ActiveSupport::Configurable
|
34
|
-
include
|
13
|
+
include ActionView::Component::Previews
|
14
|
+
|
15
|
+
delegate :form_authenticity_token, :protect_against_forgery?, to: :helpers
|
16
|
+
|
17
|
+
validate :variant_exists
|
35
18
|
|
36
19
|
# Entrypoint for rendering components. Called by ActionView::Base#render.
|
37
20
|
#
|
@@ -65,10 +48,16 @@ module ActionView
|
|
65
48
|
@lookup_context ||= view_context.lookup_context
|
66
49
|
@view_flow ||= view_context.view_flow
|
67
50
|
@virtual_path ||= virtual_path
|
51
|
+
@variant = @lookup_context.variants.first
|
52
|
+
old_current_template = @current_template
|
53
|
+
@current_template = self
|
68
54
|
|
69
55
|
@content = view_context.capture(&block) if block_given?
|
70
56
|
validate!
|
71
|
-
|
57
|
+
|
58
|
+
send(self.class.call_method_name(@variant))
|
59
|
+
ensure
|
60
|
+
@current_template = old_current_template
|
72
61
|
end
|
73
62
|
|
74
63
|
def initialize(*); end
|
@@ -85,12 +74,38 @@ module ActionView
|
|
85
74
|
@controller ||= view_context.controller
|
86
75
|
end
|
87
76
|
|
88
|
-
#
|
77
|
+
# Provides a proxy to access helper methods through
|
78
|
+
def helpers
|
79
|
+
@helpers ||= view_context
|
80
|
+
end
|
81
|
+
|
89
82
|
# Removes the first part of the path and the extension.
|
90
83
|
def virtual_path
|
91
|
-
self.class.source_location.gsub(%r{(.*app/)|(
|
84
|
+
self.class.source_location.gsub(%r{(.*app/components)|(\.rb)}, "")
|
85
|
+
end
|
86
|
+
|
87
|
+
def view_cache_dependencies
|
88
|
+
[]
|
89
|
+
end
|
90
|
+
|
91
|
+
def format # :nodoc:
|
92
|
+
@variant
|
92
93
|
end
|
93
94
|
|
95
|
+
private
|
96
|
+
|
97
|
+
def variant_exists
|
98
|
+
return if self.class.variants.include?(@variant) || @variant.nil?
|
99
|
+
|
100
|
+
errors.add(:variant, "'#{@variant}' has no template defined")
|
101
|
+
end
|
102
|
+
|
103
|
+
def request
|
104
|
+
@request ||= controller.request
|
105
|
+
end
|
106
|
+
|
107
|
+
attr_reader :content, :view_context
|
108
|
+
|
94
109
|
class << self
|
95
110
|
def inherited(child)
|
96
111
|
child.include Rails.application.routes.url_helpers unless child < Rails.application.routes.url_helpers
|
@@ -98,91 +113,102 @@ module ActionView
|
|
98
113
|
super
|
99
114
|
end
|
100
115
|
|
116
|
+
def call_method_name(variant)
|
117
|
+
if variant.present?
|
118
|
+
"call_#{variant}"
|
119
|
+
else
|
120
|
+
"call"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
101
124
|
def source_location
|
125
|
+
# Require #initialize to be defined so that we can use
|
126
|
+
# method#source_location to look up the file name
|
127
|
+
# of the component.
|
128
|
+
#
|
129
|
+
# If we were able to only support Ruby 2.7+,
|
130
|
+
# We could just use Module#const_source_location,
|
131
|
+
# rendering this unnecessary.
|
132
|
+
raise NotImplementedError.new("#{self} must implement #initialize.") unless self.instance_method(:initialize).owner == self
|
133
|
+
|
102
134
|
instance_method(:initialize).source_location[0]
|
103
135
|
end
|
104
136
|
|
105
|
-
# Compile
|
137
|
+
# Compile templates to instance methods, assuming they haven't been compiled already.
|
106
138
|
# We could in theory do this on app boot, at least in production environments.
|
107
|
-
# Right now this just compiles the
|
139
|
+
# Right now this just compiles the first time the component is rendered.
|
108
140
|
def compile
|
109
141
|
return if @compiled && ActionView::Base.cache_template_loading
|
110
|
-
ensure_initializer_defined
|
111
142
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
143
|
+
validate_templates
|
144
|
+
|
145
|
+
templates.each do |template|
|
146
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
147
|
+
def #{call_method_name(template[:variant])}
|
148
|
+
@output_buffer = ActionView::OutputBuffer.new
|
149
|
+
#{compiled_template(template[:path])}
|
150
|
+
end
|
151
|
+
RUBY
|
152
|
+
end
|
118
153
|
|
119
154
|
@compiled = true
|
120
155
|
end
|
121
156
|
|
122
|
-
|
123
|
-
|
124
|
-
# Require #initialize to be defined so that we can use
|
125
|
-
# method#source_location to look up the file name
|
126
|
-
# of the component.
|
127
|
-
#
|
128
|
-
# If we were able to only support Ruby 2.7+,
|
129
|
-
# We could just use Module#const_source_location,
|
130
|
-
# rendering this unnecessary.
|
131
|
-
def ensure_initializer_defined
|
132
|
-
raise NotImplementedError.new("#{self} must implement #initialize.") unless self.instance_method(:initialize).owner == self
|
157
|
+
def variants
|
158
|
+
templates.map { |template| template[:variant] }
|
133
159
|
end
|
134
160
|
|
135
|
-
|
136
|
-
|
137
|
-
|
161
|
+
# we'll eventually want to update this to support other types
|
162
|
+
def type
|
163
|
+
"text/html"
|
164
|
+
end
|
138
165
|
|
139
|
-
|
140
|
-
|
141
|
-
else
|
142
|
-
handler.call(DummyTemplate.new(template))
|
143
|
-
end
|
166
|
+
def identifier
|
167
|
+
""
|
144
168
|
end
|
145
169
|
|
146
|
-
|
147
|
-
sibling_template_files =
|
148
|
-
Dir["#{source_location.split(".")[0]}.*{#{ActionView::Template.template_handler_extensions.join(',')}}"] - [source_location]
|
170
|
+
private
|
149
171
|
|
150
|
-
|
151
|
-
|
152
|
-
|
172
|
+
def templates
|
173
|
+
@templates ||=
|
174
|
+
(Dir["#{source_location.sub(/#{File.extname(source_location)}$/, '')}.*{#{ActionView::Template.template_handler_extensions.join(',')}}"] - [source_location]).each_with_object([]) do |path, memo|
|
175
|
+
memo << {
|
176
|
+
path: path,
|
177
|
+
variant: path.split(".").second.split("+")[1]&.to_sym,
|
178
|
+
handler: path.split(".").last
|
179
|
+
}
|
180
|
+
end
|
181
|
+
end
|
153
182
|
|
154
|
-
|
183
|
+
def validate_templates
|
184
|
+
if templates.empty?
|
155
185
|
raise NotImplementedError.new("Could not find a template file for #{self}.")
|
156
186
|
end
|
157
187
|
|
158
|
-
|
159
|
-
|
160
|
-
|
188
|
+
if templates.select { |template| template[:variant].nil? }.length > 1
|
189
|
+
raise StandardError.new("More than one template found for #{self}. There can only be one default template file per component.")
|
190
|
+
end
|
161
191
|
|
162
|
-
|
163
|
-
|
192
|
+
variants.each_with_object(Hash.new(0)) { |variant, counts| counts[variant] += 1 }.each do |variant, count|
|
193
|
+
next unless count > 1
|
164
194
|
|
165
|
-
|
166
|
-
|
195
|
+
raise StandardError.new("More than one template found for variant '#{variant}' in #{self}. There can only be one template file per variant.")
|
196
|
+
end
|
167
197
|
end
|
168
198
|
|
169
|
-
def
|
170
|
-
""
|
171
|
-
|
199
|
+
def compiled_template(file_path)
|
200
|
+
handler = ActionView::Template.handler_for_extension(File.extname(file_path).gsub(".", ""))
|
201
|
+
template = File.read(file_path)
|
172
202
|
|
173
|
-
|
174
|
-
|
175
|
-
|
203
|
+
if handler.method(:call).parameters.length > 1
|
204
|
+
handler.call(self, template)
|
205
|
+
else # remove before upstreaming into Rails
|
206
|
+
handler.call(OpenStruct.new(source: template, identifier: identifier, type: type))
|
207
|
+
end
|
176
208
|
end
|
177
209
|
end
|
178
210
|
|
179
|
-
|
180
|
-
|
181
|
-
def request
|
182
|
-
@request ||= controller.request
|
183
|
-
end
|
184
|
-
|
185
|
-
attr_reader :content, :view_context
|
211
|
+
ActiveSupport.run_load_hooks(:action_view_component, self)
|
186
212
|
end
|
187
213
|
end
|
188
214
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Monkey patch ActionView::Base#render to support ActionView::Component
|
4
|
+
#
|
5
|
+
# A version of this monkey patch was upstreamed in https://github.com/rails/rails/pull/36388
|
6
|
+
# We'll need to upstream an updated version of this eventually.
|
7
|
+
class ActionView::Base
|
8
|
+
module RenderMonkeyPatch
|
9
|
+
def render(options = {}, args = {}, &block)
|
10
|
+
if options.respond_to?(:render_in)
|
11
|
+
ActiveSupport::Deprecation.warn(
|
12
|
+
"passing component instances (`render MyComponent.new(foo: :bar)`) has been deprecated and will be removed in v2.0.0. Use `render MyComponent, foo: :bar` instead."
|
13
|
+
)
|
14
|
+
|
15
|
+
options.render_in(self, &block)
|
16
|
+
elsif options.is_a?(Class) && options < ActionView::Component::Base
|
17
|
+
options.new(args).render_in(self, &block)
|
18
|
+
elsif options.is_a?(Hash) && options.has_key?(:component)
|
19
|
+
options[:component].new(options[:locals]).render_in(self, &block)
|
20
|
+
else
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
prepend RenderMonkeyPatch
|
27
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concern"
|
4
|
+
require "active_support/descendants_tracker"
|
5
|
+
require_relative "test_helpers"
|
6
|
+
|
7
|
+
module ActionView
|
8
|
+
module Component #:nodoc:
|
9
|
+
module Previews
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
# Set the location of component previews through app configuration:
|
14
|
+
#
|
15
|
+
# config.action_view_component.preview_path = "#{Rails.root}/lib/component_previews"
|
16
|
+
#
|
17
|
+
mattr_accessor :preview_path, instance_writer: false
|
18
|
+
|
19
|
+
# Enable or disable component previews through app configuration:
|
20
|
+
#
|
21
|
+
# config.action_view_component.show_previews = true
|
22
|
+
#
|
23
|
+
# Defaults to +true+ for development environment
|
24
|
+
#
|
25
|
+
mattr_accessor :show_previews, instance_writer: false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Preview
|
30
|
+
extend ActiveSupport::DescendantsTracker
|
31
|
+
include ActionView::Component::TestHelpers
|
32
|
+
|
33
|
+
def render(component, *locals)
|
34
|
+
render_inline(component, *locals)
|
35
|
+
end
|
36
|
+
|
37
|
+
class << self
|
38
|
+
# Returns all component preview classes.
|
39
|
+
def all
|
40
|
+
load_previews if descendants.empty?
|
41
|
+
descendants
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns the html of the component in its layout
|
45
|
+
def call(example)
|
46
|
+
example_html = new.public_send(example)
|
47
|
+
|
48
|
+
Rails::ComponentExamplesController.render(template: "examples/show",
|
49
|
+
layout: @layout || "layouts/application",
|
50
|
+
assigns: { example: example_html })
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns the component object class associated to the preview.
|
54
|
+
def component
|
55
|
+
self.name.sub(%r{Preview$}, "").constantize
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns all of the available examples for the component preview.
|
59
|
+
def examples
|
60
|
+
public_instance_methods(false).map(&:to_s).sort
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns +true+ if the example of the component preview exists.
|
64
|
+
def example_exists?(example)
|
65
|
+
examples.include?(example)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Returns +true+ if the preview exists.
|
69
|
+
def exists?(preview)
|
70
|
+
all.any? { |p| p.preview_name == preview }
|
71
|
+
end
|
72
|
+
|
73
|
+
# Find a component preview by its underscored class name.
|
74
|
+
def find(preview)
|
75
|
+
all.find { |p| p.preview_name == preview }
|
76
|
+
end
|
77
|
+
|
78
|
+
# Returns the underscored name of the component preview without the suffix.
|
79
|
+
def preview_name
|
80
|
+
name.sub(/Preview$/, "").underscore
|
81
|
+
end
|
82
|
+
|
83
|
+
# Setter for layout name.
|
84
|
+
def layout(layout_name)
|
85
|
+
@layout = layout_name
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def load_previews
|
91
|
+
if preview_path
|
92
|
+
Dir["#{preview_path}/**/*_preview.rb"].sort.each { |file| require_dependency file }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def preview_path
|
97
|
+
Base.preview_path
|
98
|
+
end
|
99
|
+
|
100
|
+
def show_previews
|
101
|
+
Base.show_previews
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActionView
|
4
|
+
module Component
|
5
|
+
class Railtie < Rails::Railtie # :nodoc:
|
6
|
+
config.action_view_component = ActiveSupport::OrderedOptions.new
|
7
|
+
|
8
|
+
initializer "action_view_component.set_configs" do |app|
|
9
|
+
options = app.config.action_view_component
|
10
|
+
|
11
|
+
options.show_previews = Rails.env.development? if options.show_previews.nil?
|
12
|
+
|
13
|
+
if options.show_previews
|
14
|
+
options.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/test/components/previews" : nil
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveSupport.on_load(:action_view_component) do
|
18
|
+
options.each { |k, v| send("#{k}=", v) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
initializer "action_view_component.set_autoload_paths" do |app|
|
23
|
+
require "railties/lib/rails/components_controller"
|
24
|
+
require "railties/lib/rails/component_examples_controller"
|
25
|
+
|
26
|
+
options = app.config.action_view_component
|
27
|
+
|
28
|
+
if options.show_previews && options.preview_path
|
29
|
+
ActiveSupport::Dependencies.autoload_paths << options.preview_path
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
initializer "action_view_component.compile_config_methods" do
|
34
|
+
ActiveSupport.on_load(:action_view_component) do
|
35
|
+
config.compile_methods! if config.respond_to?(:compile_methods!)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
config.after_initialize do |app|
|
40
|
+
options = app.config.action_view_component
|
41
|
+
|
42
|
+
if options.show_previews
|
43
|
+
app.routes.prepend do
|
44
|
+
get "/rails/components" => "rails/components#index", :internal => true
|
45
|
+
get "/rails/components/*path" => "rails/components#previews", :internal => true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -4,7 +4,7 @@ module ActionView
|
|
4
4
|
module Component
|
5
5
|
module TestHelpers
|
6
6
|
def render_inline(component, **args, &block)
|
7
|
-
Nokogiri::HTML(controller.view_context.render(component, args, &block))
|
7
|
+
Nokogiri::HTML(controller.view_context.render(component, args, &block)).css("body > *")
|
8
8
|
end
|
9
9
|
|
10
10
|
def controller
|
@@ -22,6 +22,14 @@ module ActionView
|
|
22
22
|
|
23
23
|
render_inline(component, args, &block)
|
24
24
|
end
|
25
|
+
|
26
|
+
def with_variant(variant)
|
27
|
+
old_variants = controller.view_context.lookup_context.variants
|
28
|
+
|
29
|
+
controller.view_context.lookup_context.variants = variant
|
30
|
+
yield
|
31
|
+
controller.view_context.lookup_context.variants = old_variants
|
32
|
+
end
|
25
33
|
end
|
26
34
|
end
|
27
35
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Description:
|
2
|
+
============
|
3
|
+
Creates a new component and test.
|
4
|
+
Pass the component name, either CamelCased or under_scored, and an optional list of attributes as arguments.
|
5
|
+
|
6
|
+
Example:
|
7
|
+
========
|
8
|
+
bin/rails generate component Profile name age
|
9
|
+
|
10
|
+
creates a Profile component and test:
|
11
|
+
Component: app/components/profile_component.rb
|
12
|
+
Template: app/components/profile_component.html.erb
|
13
|
+
Test: test/components/profile_component_test.rb
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Generators
|
5
|
+
class ComponentGenerator < Rails::Generators::NamedBase
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
7
|
+
|
8
|
+
argument :attributes, type: :array, default: [], banner: "attribute"
|
9
|
+
hook_for :test_framework
|
10
|
+
check_class_collision suffix: "Component"
|
11
|
+
|
12
|
+
def create_component_file
|
13
|
+
template "component.rb", File.join("app/components", "#{file_name}_component.rb")
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_template_file
|
17
|
+
template "component.html.erb", File.join("app/components", "#{file_name}_component.html.erb")
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def file_name
|
23
|
+
@_file_name ||= super.sub(/_component\z/i, "")
|
24
|
+
end
|
25
|
+
|
26
|
+
def requires_content?
|
27
|
+
return @requires_content if @asked
|
28
|
+
|
29
|
+
@asked = true
|
30
|
+
@requires_content = ask("Would you like #{class_name} to require content? (Y/n)").downcase == "y"
|
31
|
+
end
|
32
|
+
|
33
|
+
def parent_class
|
34
|
+
defined?(ApplicationComponent) ? "ApplicationComponent" : "ActionView::Component::Base"
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize_signature
|
38
|
+
if attributes.present?
|
39
|
+
attributes.map { |attr| "#{attr.name}:" }.join(", ")
|
40
|
+
else
|
41
|
+
"*"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def initialize_body
|
46
|
+
attributes.map { |attr| "@#{attr.name} = #{attr.name}" }.join("\n ")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rspec
|
4
|
+
module Generators
|
5
|
+
class ComponentGenerator < ::Rails::Generators::NamedBase
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
7
|
+
|
8
|
+
def create_test_file
|
9
|
+
template "component_spec.rb", File.join("spec/components", "#{file_name}_component_spec.rb")
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def file_name
|
15
|
+
@_file_name ||= super.sub(/_component\z/i, "")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "rails_helper"
|
2
|
+
|
3
|
+
RSpec.describe <%= class_name %>Component, type: :component do
|
4
|
+
pending "add some examples to (or delete) #{__FILE__}"
|
5
|
+
|
6
|
+
# it "renders something useful" do
|
7
|
+
# expect(
|
8
|
+
# render_inline(described_class, attr: "value") { "Hello, components!" }.css("p").to_html
|
9
|
+
# ).to include(
|
10
|
+
# "Hello, components!"
|
11
|
+
# )
|
12
|
+
# end
|
13
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TestUnit
|
4
|
+
module Generators
|
5
|
+
class ComponentGenerator < ::Rails::Generators::NamedBase
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
7
|
+
check_class_collision suffix: "ComponentTest"
|
8
|
+
|
9
|
+
def create_test_file
|
10
|
+
template "component_test.rb", File.join("test/components", "#{file_name}_component_test.rb")
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def file_name
|
16
|
+
@_file_name ||= super.sub(/_component\z/i, "")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class <%= class_name %>ComponentTest < ActiveSupport::TestCase
|
4
|
+
include ActionView::Component::TestHelpers
|
5
|
+
|
6
|
+
test "component renders something useful" do
|
7
|
+
# assert_equal(
|
8
|
+
# %(<span title="my title">Hello, components!</span>),
|
9
|
+
# render_inline(<%= class_name %>, attr: "value") { "Hello, components!" }.css("span").to_html
|
10
|
+
# )
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/application_controller"
|
4
|
+
load "config/application.rb" unless Rails.root
|
5
|
+
|
6
|
+
class Rails::ComponentExamplesController < ActionController::Base # :nodoc:
|
7
|
+
prepend_view_path File.expand_path("templates/rails", __dir__)
|
8
|
+
append_view_path Rails.root.join("app/views")
|
9
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/application_controller"
|
4
|
+
|
5
|
+
class Rails::ComponentsController < Rails::ApplicationController # :nodoc:
|
6
|
+
prepend_view_path File.expand_path("templates/rails", __dir__)
|
7
|
+
|
8
|
+
around_action :set_locale, only: :previews
|
9
|
+
before_action :find_preview, only: :previews
|
10
|
+
before_action :require_local!, unless: :show_previews?
|
11
|
+
|
12
|
+
if respond_to?(:content_security_policy)
|
13
|
+
content_security_policy(false)
|
14
|
+
end
|
15
|
+
|
16
|
+
def index
|
17
|
+
@previews = ActionView::Component::Preview.all
|
18
|
+
@page_title = "Component Previews"
|
19
|
+
render template: "components/index"
|
20
|
+
end
|
21
|
+
|
22
|
+
def previews
|
23
|
+
if params[:path] == @preview.preview_name
|
24
|
+
@page_title = "Component Previews for #{@preview.preview_name}"
|
25
|
+
render template: "components/previews"
|
26
|
+
else
|
27
|
+
@example_name = File.basename(params[:path])
|
28
|
+
render template: "components/preview", layout: false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def show_previews? # :doc:
|
35
|
+
ActionView::Component::Base.show_previews
|
36
|
+
end
|
37
|
+
|
38
|
+
def find_preview # :doc:
|
39
|
+
candidates = []
|
40
|
+
params[:path].to_s.scan(%r{/|$}) { candidates << $` }
|
41
|
+
preview = candidates.detect { |candidate| ActionView::Component::Preview.exists?(candidate) }
|
42
|
+
|
43
|
+
if preview
|
44
|
+
@preview = ActionView::Component::Preview.find(preview)
|
45
|
+
else
|
46
|
+
raise AbstractController::ActionNotFound, "Component preview '#{params[:path]}' not found"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_locale
|
51
|
+
I18n.with_locale(params[:locale] || I18n.default_locale) do
|
52
|
+
yield
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<% @previews.each do |preview| %>
|
2
|
+
<h3><%= link_to preview.preview_name.titleize, "/rails/components/#{preview.preview_name}" %></h3>
|
3
|
+
<ul>
|
4
|
+
<% preview.examples.each do |preview_example| %>
|
5
|
+
<li><%= link_to preview_example, "/rails/components/#{preview.preview_name}/#{preview_example}" %></li>
|
6
|
+
<% end %>
|
7
|
+
</ul>
|
8
|
+
<% end %>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= raw @preview.call(@example_name) %>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= raw @example %>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: actionview-component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3
|
4
|
+
version: 1.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub Open Source
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,16 +84,16 @@ dependencies:
|
|
84
84
|
name: rubocop
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - '='
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0.
|
89
|
+
version: '0.74'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - '='
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '0.
|
96
|
+
version: '0.74'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: rubocop-github
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -127,9 +127,28 @@ files:
|
|
127
127
|
- README.md
|
128
128
|
- Rakefile
|
129
129
|
- actionview-component.gemspec
|
130
|
+
- lib/action_view/component.rb
|
130
131
|
- lib/action_view/component/base.rb
|
132
|
+
- lib/action_view/component/monkey_patch.rb
|
133
|
+
- lib/action_view/component/preview.rb
|
134
|
+
- lib/action_view/component/railtie.rb
|
131
135
|
- lib/action_view/component/test_helpers.rb
|
132
136
|
- lib/action_view/component/version.rb
|
137
|
+
- lib/rails/generators/component/USAGE
|
138
|
+
- lib/rails/generators/component/component_generator.rb
|
139
|
+
- lib/rails/generators/component/templates/component.html.erb.tt
|
140
|
+
- lib/rails/generators/component/templates/component.rb.tt
|
141
|
+
- lib/rails/generators/rspec/component_generator.rb
|
142
|
+
- lib/rails/generators/rspec/templates/component_spec.rb.tt
|
143
|
+
- lib/rails/generators/test_unit/component_generator.rb
|
144
|
+
- lib/rails/generators/test_unit/templates/component_test.rb.tt
|
145
|
+
- lib/railties/lib/rails.rb
|
146
|
+
- lib/railties/lib/rails/component_examples_controller.rb
|
147
|
+
- lib/railties/lib/rails/components_controller.rb
|
148
|
+
- lib/railties/lib/rails/templates/rails/components/index.html.erb
|
149
|
+
- lib/railties/lib/rails/templates/rails/components/preview.html.erb
|
150
|
+
- lib/railties/lib/rails/templates/rails/components/previews.html.erb
|
151
|
+
- lib/railties/lib/rails/templates/rails/examples/show.html.erb
|
133
152
|
- script/bootstrap
|
134
153
|
- script/console
|
135
154
|
- script/install
|