actionview-component 1.13.0 → 1.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/Gemfile.lock +1 -1
  4. data/README.md +43 -55
  5. data/actionview-component.gemspec +2 -2
  6. data/app/controllers/rails/components_controller.rb +4 -4
  7. data/lib/action_view/component.rb +1 -21
  8. data/lib/action_view/component/base.rb +1 -262
  9. data/lib/action_view/component/preview.rb +1 -72
  10. data/lib/action_view/component/railtie.rb +1 -1
  11. data/lib/action_view/component/test_case.rb +1 -4
  12. data/lib/rails/generators/component/component_generator.rb +1 -1
  13. data/lib/rails/generators/test_unit/templates/component_test.rb.tt +1 -1
  14. data/lib/view_component.rb +29 -0
  15. data/lib/view_component/base.rb +273 -0
  16. data/lib/view_component/conversion.rb +9 -0
  17. data/lib/view_component/engine.rb +65 -0
  18. data/lib/view_component/preview.rb +77 -0
  19. data/lib/view_component/previewable.rb +25 -0
  20. data/lib/view_component/render_monkey_patch.rb +31 -0
  21. data/lib/view_component/rendering_monkey_patch.rb +13 -0
  22. data/lib/view_component/template_error.rb +9 -0
  23. data/lib/view_component/test_case.rb +9 -0
  24. data/lib/view_component/test_helpers.rb +43 -0
  25. data/lib/view_component/version.rb +11 -0
  26. data/script/console +1 -1
  27. metadata +14 -10
  28. data/lib/action_view/component/conversion.rb +0 -11
  29. data/lib/action_view/component/engine.rb +0 -67
  30. data/lib/action_view/component/previewable.rb +0 -27
  31. data/lib/action_view/component/render_monkey_patch.rb +0 -34
  32. data/lib/action_view/component/rendering_monkey_patch.rb +0 -15
  33. data/lib/action_view/component/template_error.rb +0 -11
  34. data/lib/action_view/component/test_helpers.rb +0 -45
  35. data/lib/action_view/component/version.rb +0 -13
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/descendants_tracker"
4
+
5
+ module ViewComponent # :nodoc:
6
+ class Preview
7
+ extend ActiveSupport::DescendantsTracker
8
+
9
+ def render(component, **args, &block)
10
+ { component: component, args: args, block: block }
11
+ end
12
+
13
+ class << self
14
+ # Returns all component preview classes.
15
+ def all
16
+ load_previews if descendants.empty?
17
+ descendants
18
+ end
19
+
20
+ # Returns the arguments for rendering of the component in its layout
21
+ def render_args(example)
22
+ new.public_send(example).merge(layout: @layout)
23
+ end
24
+
25
+ # Returns the component object class associated to the preview.
26
+ def component
27
+ name.chomp("Preview").constantize
28
+ end
29
+
30
+ # Returns all of the available examples for the component preview.
31
+ def examples
32
+ public_instance_methods(false).map(&:to_s).sort
33
+ end
34
+
35
+ # Returns +true+ if the example of the component preview exists.
36
+ def example_exists?(example)
37
+ examples.include?(example)
38
+ end
39
+
40
+ # Returns +true+ if the preview exists.
41
+ def exists?(preview)
42
+ all.any? { |p| p.preview_name == preview }
43
+ end
44
+
45
+ # Find a component preview by its underscored class name.
46
+ def find(preview)
47
+ all.find { |p| p.preview_name == preview }
48
+ end
49
+
50
+ # Returns the underscored name of the component preview without the suffix.
51
+ def preview_name
52
+ name.chomp("Preview").underscore
53
+ end
54
+
55
+ # Setter for layout name.
56
+ def layout(layout_name)
57
+ @layout = layout_name
58
+ end
59
+
60
+ private
61
+
62
+ def load_previews
63
+ if preview_path
64
+ Dir["#{preview_path}/**/*_preview.rb"].sort.each { |file| require_dependency file }
65
+ end
66
+ end
67
+
68
+ def preview_path
69
+ Base.preview_path
70
+ end
71
+
72
+ def show_previews
73
+ Base.show_previews
74
+ end
75
+ end
76
+ end
77
+ 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)
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)
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,43 @@
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 render_inline(component, **args, &block)
14
+ @raw = controller.view_context.render(component, args, &block)
15
+
16
+ Nokogiri::HTML.fragment(@raw)
17
+ end
18
+
19
+ def controller
20
+ @controller ||= Base.test_controller.constantize.new.tap { |c| c.request = request }.extend(Rails.application.routes.url_helpers)
21
+ end
22
+
23
+ def request
24
+ @request ||= ActionDispatch::TestRequest.create
25
+ end
26
+
27
+ def render_component(component, **args, &block)
28
+ ActiveSupport::Deprecation.warn(
29
+ "`render_component` has been deprecated in favor of `render_inline`, and will be removed in v2.0.0."
30
+ )
31
+
32
+ render_inline(component, args, &block)
33
+ end
34
+
35
+ def with_variant(variant)
36
+ old_variants = controller.view_context.lookup_context.variants
37
+
38
+ controller.view_context.lookup_context.variants = variant
39
+ yield
40
+ controller.view_context.lookup_context.variants = old_variants
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent
4
+ module VERSION
5
+ MAJOR = 1
6
+ MINOR = 14
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionview-component
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.13.0
4
+ version: 1.14.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-03-02 00:00:00.000000000 Z
11
+ date: 2020-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara
@@ -160,17 +160,9 @@ files:
160
160
  - app/controllers/rails/components_controller.rb
161
161
  - lib/action_view/component.rb
162
162
  - lib/action_view/component/base.rb
163
- - lib/action_view/component/conversion.rb
164
- - lib/action_view/component/engine.rb
165
163
  - lib/action_view/component/preview.rb
166
- - lib/action_view/component/previewable.rb
167
164
  - lib/action_view/component/railtie.rb
168
- - lib/action_view/component/render_monkey_patch.rb
169
- - lib/action_view/component/rendering_monkey_patch.rb
170
- - lib/action_view/component/template_error.rb
171
165
  - lib/action_view/component/test_case.rb
172
- - lib/action_view/component/test_helpers.rb
173
- - lib/action_view/component/version.rb
174
166
  - lib/rails/generators/component/USAGE
175
167
  - lib/rails/generators/component/component_generator.rb
176
168
  - lib/rails/generators/component/templates/component.rb.tt
@@ -188,6 +180,18 @@ files:
188
180
  - lib/railties/lib/rails/templates/rails/components/index.html.erb
189
181
  - lib/railties/lib/rails/templates/rails/components/preview.html.erb
190
182
  - lib/railties/lib/rails/templates/rails/components/previews.html.erb
183
+ - lib/view_component.rb
184
+ - lib/view_component/base.rb
185
+ - lib/view_component/conversion.rb
186
+ - lib/view_component/engine.rb
187
+ - lib/view_component/preview.rb
188
+ - lib/view_component/previewable.rb
189
+ - lib/view_component/render_monkey_patch.rb
190
+ - lib/view_component/rendering_monkey_patch.rb
191
+ - lib/view_component/template_error.rb
192
+ - lib/view_component/test_case.rb
193
+ - lib/view_component/test_helpers.rb
194
+ - lib/view_component/version.rb
191
195
  - script/bootstrap
192
196
  - script/console
193
197
  - script/install
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActionView
4
- module Component # :nodoc:
5
- module Conversion
6
- def to_component_class
7
- "#{self.class.name}Component".safe_constantize
8
- end
9
- end
10
- end
11
- end
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails"
4
- require "action_view/component"
5
-
6
- module ActionView
7
- module Component
8
- class Engine < Rails::Engine # :nodoc:
9
- config.action_view_component = ActiveSupport::OrderedOptions.new
10
-
11
- initializer "action_view_component.set_configs" do |app|
12
- options = app.config.action_view_component
13
-
14
- options.show_previews = Rails.env.development? if options.show_previews.nil?
15
-
16
- if options.show_previews
17
- options.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/test/components/previews" : nil
18
- end
19
-
20
- ActiveSupport.on_load(:action_view_component) do
21
- options.each { |k, v| send("#{k}=", v) }
22
- end
23
- end
24
-
25
- initializer "action_view_component.set_autoload_paths" do |app|
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.eager_load_actions" do
34
- ActiveSupport.on_load(:after_initialize) do
35
- ActionView::Component::Base.descendants.each(&:compile)
36
- end
37
- end
38
-
39
- initializer "action_view_component.compile_config_methods" do
40
- ActiveSupport.on_load(:action_view_component) do
41
- config.compile_methods! if config.respond_to?(:compile_methods!)
42
- end
43
- end
44
-
45
- initializer "action_view_component.monkey_patch_render" do
46
- ActiveSupport.on_load(:action_view) do
47
- ActionView::Base.prepend ActionView::Component::RenderMonkeyPatch
48
- end
49
-
50
- ActiveSupport.on_load(:action_controller) do
51
- ActionController::Base.prepend ActionView::Component::RenderingMonkeyPatch
52
- end
53
- end
54
-
55
- config.after_initialize do |app|
56
- options = app.config.action_view_component
57
-
58
- if options.show_previews
59
- app.routes.prepend do
60
- get "/rails/components" => "rails/components#index", :internal => true
61
- get "/rails/components/*path" => "rails/components#previews", :internal => true
62
- end
63
- end
64
- end
65
- end
66
- end
67
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_support/concern"
4
-
5
- module ActionView
6
- module Component # :nodoc:
7
- module Previewable
8
- extend ActiveSupport::Concern
9
-
10
- included do
11
- # Set the location of component previews through app configuration:
12
- #
13
- # config.action_view_component.preview_path = "#{Rails.root}/lib/component_previews"
14
- #
15
- mattr_accessor :preview_path, instance_writer: false
16
-
17
- # Enable or disable component previews through app configuration:
18
- #
19
- # config.action_view_component.show_previews = true
20
- #
21
- # Defaults to +true+ for development environment
22
- #
23
- mattr_accessor :show_previews, instance_writer: false
24
- end
25
- end
26
- end
27
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Monkey patch ActionView::Base#render to support ActionView::Component
4
- module ActionView
5
- module Component
6
- module RenderMonkeyPatch # :nodoc:
7
- def render(options = {}, args = {}, &block)
8
- if options.respond_to?(:render_in)
9
- options.render_in(self, &block)
10
- elsif options.is_a?(Class) && options < ActionView::Component::Base
11
- ActiveSupport::Deprecation.warn(
12
- "`render MyComponent, foo: :bar` has been deprecated and will be removed in v2.0.0. Use `render MyComponent.new(foo: :bar)` instead."
13
- )
14
-
15
- options.new(args).render_in(self, &block)
16
- elsif options.is_a?(Hash) && options.has_key?(:component)
17
- ActiveSupport::Deprecation.warn(
18
- "`render component: MyComponent, locals: { foo: :bar }` has been deprecated and will be removed in v2.0.0. Use `render MyComponent.new(foo: :bar)` instead."
19
- )
20
-
21
- options[:component].new(options[:locals]).render_in(self, &block)
22
- elsif options.respond_to?(:to_component_class) && !options.to_component_class.nil?
23
- ActiveSupport::Deprecation.warn(
24
- "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."
25
- )
26
-
27
- options.to_component_class.new(options).render_in(self, &block)
28
- else
29
- super
30
- end
31
- end
32
- end
33
- end
34
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActionView
4
- module Component
5
- module RenderingMonkeyPatch # :nodoc:
6
- def render(options = {}, args = {})
7
- if options.respond_to?(:render_in)
8
- self.response_body = options.render_in(self.view_context)
9
- else
10
- super
11
- end
12
- end
13
- end
14
- end
15
- end