actionview-component 1.7.0 → 1.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE +5 -8
  3. data/.github/PULL_REQUEST_TEMPLATE +2 -4
  4. data/.github/workflows/ruby_on_rails.yml +6 -2
  5. data/.gitignore +1 -0
  6. data/.rubocop.yml +4 -0
  7. data/CHANGELOG.md +150 -0
  8. data/CONTRIBUTING.md +10 -19
  9. data/Gemfile.lock +20 -5
  10. data/README.md +204 -248
  11. data/actionview-component.gemspec +10 -8
  12. data/{lib/railties/lib → app/controllers}/rails/components_controller.rb +18 -8
  13. data/docs/case-studies/jellyswitch.md +76 -0
  14. data/lib/action_view/component.rb +1 -20
  15. data/lib/action_view/component/base.rb +2 -246
  16. data/lib/action_view/component/preview.rb +1 -80
  17. data/lib/action_view/component/railtie.rb +1 -64
  18. data/lib/action_view/component/test_case.rb +1 -3
  19. data/lib/action_view/component/test_helpers.rb +1 -19
  20. data/lib/rails/generators/component/component_generator.rb +6 -12
  21. data/lib/rails/generators/component/templates/component.rb.tt +0 -4
  22. data/lib/rails/generators/erb/component_generator.rb +21 -0
  23. data/lib/rails/generators/erb/templates/component.html.erb.tt +1 -0
  24. data/lib/rails/generators/haml/component_generator.rb +21 -0
  25. data/lib/rails/generators/haml/templates/component.html.haml.tt +1 -0
  26. data/lib/rails/generators/rspec/component_generator.rb +1 -1
  27. data/lib/rails/generators/rspec/templates/component_spec.rb.tt +1 -1
  28. data/lib/rails/generators/slim/component_generator.rb +21 -0
  29. data/lib/rails/generators/slim/templates/component.html.slim.tt +1 -0
  30. data/lib/rails/generators/test_unit/component_generator.rb +1 -1
  31. data/lib/rails/generators/test_unit/templates/component_test.rb.tt +3 -3
  32. data/lib/railties/lib/rails.rb +0 -1
  33. data/lib/railties/lib/rails/templates/rails/components/preview.html.erb +1 -1
  34. data/lib/view_component.rb +30 -0
  35. data/lib/view_component/base.rb +279 -0
  36. data/lib/view_component/conversion.rb +9 -0
  37. data/lib/view_component/engine.rb +65 -0
  38. data/lib/view_component/preview.rb +78 -0
  39. data/lib/view_component/previewable.rb +25 -0
  40. data/lib/view_component/render_monkey_patch.rb +31 -0
  41. data/lib/view_component/rendering_monkey_patch.rb +13 -0
  42. data/lib/view_component/template_error.rb +9 -0
  43. data/lib/view_component/test_case.rb +9 -0
  44. data/lib/view_component/test_helpers.rb +39 -0
  45. data/lib/view_component/version.rb +11 -0
  46. data/script/console +1 -1
  47. metadata +48 -21
  48. data/lib/action_view/component/conversion.rb +0 -11
  49. data/lib/action_view/component/previewable.rb +0 -27
  50. data/lib/action_view/component/render_monkey_patch.rb +0 -29
  51. data/lib/action_view/component/template_error.rb +0 -11
  52. data/lib/action_view/component/version.rb +0 -13
  53. data/lib/rails/generators/component/templates/component.html.erb.tt +0 -5
  54. data/lib/railties/lib/rails/component_examples_controller.rb +0 -9
  55. data/lib/railties/lib/rails/templates/rails/examples/show.html.erb +0 -1
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent # :nodoc:
4
+ module Conversion
5
+ def to_component_class
6
+ "#{self.class.name}Component".safe_constantize
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+ require "view_component"
5
+
6
+ module ViewComponent
7
+ class Engine < Rails::Engine # :nodoc:
8
+ config.action_view_component = ActiveSupport::OrderedOptions.new
9
+
10
+ initializer "action_view_component.set_configs" do |app|
11
+ options = app.config.action_view_component
12
+
13
+ options.show_previews = Rails.env.development? if options.show_previews.nil?
14
+
15
+ if options.show_previews
16
+ options.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/test/components/previews" : nil
17
+ end
18
+
19
+ ActiveSupport.on_load(:action_view_component) do
20
+ options.each { |k, v| send("#{k}=", v) }
21
+ end
22
+ end
23
+
24
+ initializer "action_view_component.set_autoload_paths" do |app|
25
+ options = app.config.action_view_component
26
+
27
+ if options.show_previews && options.preview_path
28
+ ActiveSupport::Dependencies.autoload_paths << options.preview_path
29
+ end
30
+ end
31
+
32
+ initializer "action_view_component.eager_load_actions" do
33
+ ActiveSupport.on_load(:after_initialize) do
34
+ ViewComponent::Base.descendants.each(&:compile)
35
+ end
36
+ end
37
+
38
+ initializer "action_view_component.compile_config_methods" do
39
+ ActiveSupport.on_load(:action_view_component) do
40
+ config.compile_methods! if config.respond_to?(:compile_methods!)
41
+ end
42
+ end
43
+
44
+ initializer "action_view_component.monkey_patch_render" do
45
+ ActiveSupport.on_load(:action_view) do
46
+ ActionView::Base.prepend ViewComponent::RenderMonkeyPatch
47
+ end
48
+
49
+ ActiveSupport.on_load(:action_controller) do
50
+ ActionController::Base.prepend ViewComponent::RenderingMonkeyPatch
51
+ end
52
+ end
53
+
54
+ config.after_initialize do |app|
55
+ options = app.config.action_view_component
56
+
57
+ if options.show_previews
58
+ app.routes.prepend do
59
+ get "/rails/components" => "rails/components#index", :internal => true
60
+ get "/rails/components/*path" => "rails/components#previews", :internal => true
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/descendants_tracker"
4
+
5
+ module ViewComponent # :nodoc:
6
+ class Preview
7
+ include ActionView::Helpers::TagHelper
8
+ extend ActiveSupport::DescendantsTracker
9
+
10
+ def render(component, **args, &block)
11
+ { component: component, args: args, block: block }
12
+ end
13
+
14
+ class << self
15
+ # Returns all component preview classes.
16
+ def all
17
+ load_previews if descendants.empty?
18
+ descendants
19
+ end
20
+
21
+ # Returns the arguments for rendering of the component in its layout
22
+ def render_args(example)
23
+ new.public_send(example).merge(layout: @layout)
24
+ end
25
+
26
+ # Returns the component object class associated to the preview.
27
+ def component
28
+ name.chomp("Preview").constantize
29
+ end
30
+
31
+ # Returns all of the available examples for the component preview.
32
+ def examples
33
+ public_instance_methods(false).map(&:to_s).sort
34
+ end
35
+
36
+ # Returns +true+ if the example of the component preview exists.
37
+ def example_exists?(example)
38
+ examples.include?(example)
39
+ end
40
+
41
+ # Returns +true+ if the preview exists.
42
+ def exists?(preview)
43
+ all.any? { |p| p.preview_name == preview }
44
+ end
45
+
46
+ # Find a component preview by its underscored class name.
47
+ def find(preview)
48
+ all.find { |p| p.preview_name == preview }
49
+ end
50
+
51
+ # Returns the underscored name of the component preview without the suffix.
52
+ def preview_name
53
+ name.chomp("Preview").underscore
54
+ end
55
+
56
+ # Setter for layout name.
57
+ def layout(layout_name)
58
+ @layout = layout_name
59
+ end
60
+
61
+ private
62
+
63
+ def load_previews
64
+ if preview_path
65
+ Dir["#{preview_path}/**/*_preview.rb"].sort.each { |file| require_dependency file }
66
+ end
67
+ end
68
+
69
+ def preview_path
70
+ Base.preview_path
71
+ end
72
+
73
+ def show_previews
74
+ Base.show_previews
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module ViewComponent # :nodoc:
6
+ module Previewable
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ # Set the location of component previews through app configuration:
11
+ #
12
+ # config.action_view_component.preview_path = "#{Rails.root}/lib/component_previews"
13
+ #
14
+ mattr_accessor :preview_path, instance_writer: false
15
+
16
+ # Enable or disable component previews through app configuration:
17
+ #
18
+ # config.action_view_component.show_previews = true
19
+ #
20
+ # Defaults to +true+ for development environment
21
+ #
22
+ mattr_accessor :show_previews, instance_writer: false
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent
4
+ module RenderMonkeyPatch # :nodoc:
5
+ def render(options = {}, args = {}, &block)
6
+ if options.respond_to?(:render_in) && Rails.version.to_f < 6.1
7
+ options.render_in(self, &block)
8
+ elsif options.is_a?(Class) && options < ActionView::Component::Base
9
+ ActiveSupport::Deprecation.warn(
10
+ "`render MyComponent, foo: :bar` has been deprecated and will be removed in v2.0.0. Use `render MyComponent.new(foo: :bar)` instead."
11
+ )
12
+
13
+ options.new(args).render_in(self, &block)
14
+ elsif options.is_a?(Hash) && options.has_key?(:component) && options[:component] < ActionView::Component::Base
15
+ ActiveSupport::Deprecation.warn(
16
+ "`render component: MyComponent, locals: { foo: :bar }` has been deprecated and will be removed in v2.0.0. Use `render MyComponent.new(foo: :bar)` instead."
17
+ )
18
+
19
+ options[:component].new(options[:locals]).render_in(self, &block)
20
+ elsif options.respond_to?(:to_component_class) && !options.to_component_class.nil? && options.to_component_class < ActionView::Component::Base
21
+ ActiveSupport::Deprecation.warn(
22
+ "rendering objects that respond_to `to_component_class` has been deprecated and will be removed in v2.0.0. Use `render MyComponent.new(foo: :bar)` instead."
23
+ )
24
+
25
+ options.to_component_class.new(options).render_in(self, &block)
26
+ else
27
+ super
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent
4
+ module RenderingMonkeyPatch # :nodoc:
5
+ def render(options = {}, args = {})
6
+ if options.respond_to?(:render_in) && Rails.version.to_f < 6.1
7
+ self.response_body = options.render_in(self.view_context)
8
+ else
9
+ super
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent
4
+ class TemplateError < StandardError
5
+ def initialize(errors)
6
+ super(errors.join(", "))
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/test_case"
4
+
5
+ module ViewComponent
6
+ class TestCase < ActiveSupport::TestCase
7
+ include ViewComponent::TestHelpers
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "capybara/minitest"
4
+
5
+ module ViewComponent
6
+ module TestHelpers
7
+ include Capybara::Minitest::Assertions
8
+
9
+ def page
10
+ Capybara::Node::Simple.new(@raw)
11
+ end
12
+
13
+ def refute_component_rendered
14
+ assert_no_selector("body")
15
+ end
16
+
17
+ def render_inline(component, **args, &block)
18
+ @raw = controller.view_context.render(component, args, &block)
19
+
20
+ Nokogiri::HTML.fragment(@raw)
21
+ end
22
+
23
+ def controller
24
+ @controller ||= Base.test_controller.constantize.new.tap { |c| c.request = request }.extend(Rails.application.routes.url_helpers)
25
+ end
26
+
27
+ def request
28
+ @request ||= ActionDispatch::TestRequest.create
29
+ end
30
+
31
+ def with_variant(variant)
32
+ old_variants = controller.view_context.lookup_context.variants
33
+
34
+ controller.view_context.lookup_context.variants = variant
35
+ yield
36
+ controller.view_context.lookup_context.variants = old_variants
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent
4
+ module VERSION
5
+ MAJOR = 1
6
+ MINOR = 17
7
+ PATCH = 0
8
+
9
+ STRING = [MAJOR, MINOR, PATCH].join(".")
10
+ end
11
+ end
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "bundler/setup"
5
- require "action_view/component"
5
+ require "view_component"
6
6
 
7
7
  require "irb"
8
8
  IRB.start(__FILE__)
metadata CHANGED
@@ -1,27 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionview-component
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.17.0
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: 2020-01-09 00:00:00.000000000 Z
11
+ date: 2020-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capybara
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - ">="
31
+ - - "~>"
18
32
  - !ruby/object:Gem::Version
19
33
  version: '1.14'
20
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
40
  version: '1.14'
27
41
  - !ruby/object:Gem::Dependency
@@ -30,14 +44,14 @@ dependencies:
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '10.0'
47
+ version: '13.0'
34
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: '10.0'
54
+ version: '13.0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: minitest
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -122,9 +136,9 @@ dependencies:
122
136
  - - "~>"
123
137
  - !ruby/object:Gem::Version
124
138
  version: 0.13.0
125
- description: View components for Rails, intended for upstreaming in Rails 6.1
139
+ description:
126
140
  email:
127
- - opensource+actionview-component@github.com
141
+ - opensource+view_component@github.com
128
142
  executables: []
129
143
  extensions: []
130
144
  extra_rdoc_files: []
@@ -143,43 +157,56 @@ files:
143
157
  - README.md
144
158
  - Rakefile
145
159
  - actionview-component.gemspec
160
+ - app/controllers/rails/components_controller.rb
161
+ - docs/case-studies/jellyswitch.md
146
162
  - lib/action_view/component.rb
147
163
  - lib/action_view/component/base.rb
148
- - lib/action_view/component/conversion.rb
149
164
  - lib/action_view/component/preview.rb
150
- - lib/action_view/component/previewable.rb
151
165
  - lib/action_view/component/railtie.rb
152
- - lib/action_view/component/render_monkey_patch.rb
153
- - lib/action_view/component/template_error.rb
154
166
  - lib/action_view/component/test_case.rb
155
167
  - lib/action_view/component/test_helpers.rb
156
- - lib/action_view/component/version.rb
157
168
  - lib/rails/generators/component/USAGE
158
169
  - lib/rails/generators/component/component_generator.rb
159
- - lib/rails/generators/component/templates/component.html.erb.tt
160
170
  - lib/rails/generators/component/templates/component.rb.tt
171
+ - lib/rails/generators/erb/component_generator.rb
172
+ - lib/rails/generators/erb/templates/component.html.erb.tt
173
+ - lib/rails/generators/haml/component_generator.rb
174
+ - lib/rails/generators/haml/templates/component.html.haml.tt
161
175
  - lib/rails/generators/rspec/component_generator.rb
162
176
  - lib/rails/generators/rspec/templates/component_spec.rb.tt
177
+ - lib/rails/generators/slim/component_generator.rb
178
+ - lib/rails/generators/slim/templates/component.html.slim.tt
163
179
  - lib/rails/generators/test_unit/component_generator.rb
164
180
  - lib/rails/generators/test_unit/templates/component_test.rb.tt
165
181
  - lib/railties/lib/rails.rb
166
- - lib/railties/lib/rails/component_examples_controller.rb
167
- - lib/railties/lib/rails/components_controller.rb
168
182
  - lib/railties/lib/rails/templates/rails/components/index.html.erb
169
183
  - lib/railties/lib/rails/templates/rails/components/preview.html.erb
170
184
  - lib/railties/lib/rails/templates/rails/components/previews.html.erb
171
- - lib/railties/lib/rails/templates/rails/examples/show.html.erb
185
+ - lib/view_component.rb
186
+ - lib/view_component/base.rb
187
+ - lib/view_component/conversion.rb
188
+ - lib/view_component/engine.rb
189
+ - lib/view_component/preview.rb
190
+ - lib/view_component/previewable.rb
191
+ - lib/view_component/render_monkey_patch.rb
192
+ - lib/view_component/rendering_monkey_patch.rb
193
+ - lib/view_component/template_error.rb
194
+ - lib/view_component/test_case.rb
195
+ - lib/view_component/test_helpers.rb
196
+ - lib/view_component/version.rb
172
197
  - script/bootstrap
173
198
  - script/console
174
199
  - script/install
175
200
  - script/release
176
201
  - script/test
177
- homepage: https://github.com/github/actionview-component
202
+ homepage: https://github.com/github/view_component
178
203
  licenses:
179
204
  - MIT
180
205
  metadata:
181
206
  allowed_push_host: https://rubygems.org
182
- post_install_message:
207
+ post_install_message: 'WARNING: actionview-component has been renamed to view_component,
208
+ and will no longer be published in this namespace. Please update your Gemfile to
209
+ use view_component.'
183
210
  rdoc_options: []
184
211
  require_paths:
185
212
  - lib
@@ -194,8 +221,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
221
  - !ruby/object:Gem::Version
195
222
  version: '0'
196
223
  requirements: []
197
- rubygems_version: 3.0.3
224
+ rubygems_version: 3.1.2
198
225
  signing_key:
199
226
  specification_version: 4
200
- summary: View components for Rails
227
+ summary: MOVED to view_component.
201
228
  test_files: []