view_component 2.24.0 → 2.25.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of view_component might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28b088083cd9a83d90264715da7105bbc83e4c37e81785db5ca11c8192c6ccf6
4
- data.tar.gz: d6ebc8a6a830e7bcf2490457d471017b9d123705e55ec9f6e0b1d2745e6a99c4
3
+ metadata.gz: ed1ee878b4b1519adcb75b06689a3341367cca5aa8ae4dd087cb2fba0c06a355
4
+ data.tar.gz: 70459f53b555b8edf6675346a42c1be87bb36267bce23ffb9cc4a639731c1b77
5
5
  SHA512:
6
- metadata.gz: 9061290b38e66429e6f66327082d17f2fb8d26ef1022557a049a642335d4af79e6dcf05797a6af9ba95eb074315d3d1272c4c6902e87aa87728fc01fd694fe62
7
- data.tar.gz: 5bcf720d202c0fbbeea76d9dbd044239dfb777be51ba47d39965aef104f84af5372aecf3d99bf203f973773bce3b311e41716b3e7715e8bd4b9175b3c305740a
6
+ metadata.gz: 2ac2189345c278608e8dc96ec1c7f90472991aefc86da43968489f3ea3cff4c9306ef8280553b2dad6a7a35b24e74eabd2269b3efcba4026b00b642dcac0dcf4
7
+ data.tar.gz: 6a393762db37291a6f6eaa2e19575aa035f103cc6c0fe71c96648fb827ed0e140c0e9d9f297901f699f7abd3e4946ca8e048bf53b1dbe62e47663e2bb591918e
@@ -2,8 +2,22 @@
2
2
 
3
3
  ## main
4
4
 
5
+ ## 2.25.0
6
+
7
+ * Add `--preview` generator option to create an associated preview file.
8
+
9
+ *Bob Maerten*
10
+
11
+ * Add argument validation to avoid `content` override.
12
+
13
+ *Manuel Puyol*
14
+
5
15
  ## 2.24.0
6
16
 
17
+ * Add `--inline` option to the erb generator. Prevents default erb template from being created and creates a component with a call method.
18
+
19
+ *Nachiket Pusalkar*
20
+
7
21
  * Add test case for checking presence of `content` in `#render?`.
8
22
 
9
23
  *Joel Hawksley*
@@ -7,6 +7,7 @@ module Rails
7
7
 
8
8
  argument :attributes, type: :array, default: [], banner: "attribute"
9
9
  check_class_collision suffix: "Component"
10
+ class_option :inline, type: :boolean, default: false
10
11
 
11
12
  def create_component_file
12
13
  template "component.rb", File.join("app/components", class_path, "#{file_name}_component.rb")
@@ -14,6 +15,8 @@ module Rails
14
15
 
15
16
  hook_for :test_framework
16
17
 
18
+ hook_for :preview, type: :boolean
19
+
17
20
  hook_for :template_engine do |instance, template_engine|
18
21
  instance.invoke template_engine, [instance.name]
19
22
  end
@@ -37,6 +40,10 @@ module Rails
37
40
  def initialize_body
38
41
  attributes.map { |attr| "@#{attr.name} = #{attr.name}" }.join("\n ")
39
42
  end
43
+
44
+ def initialize_call_method_for_inline?
45
+ options["inline"]
46
+ end
40
47
  end
41
48
  end
42
49
  end
@@ -6,4 +6,10 @@ class <%= class_name %>Component < <%= parent_class %>
6
6
  <%= initialize_body %>
7
7
  end
8
8
  <%- end -%>
9
+ <%- if initialize_call_method_for_inline? -%>
10
+ def call
11
+ content_tag :h1, "Hello world!"
12
+ end
13
+ <%- end -%>
14
+
9
15
  end
@@ -7,6 +7,7 @@ module Erb
7
7
  class ComponentGenerator < Base
8
8
  source_root File.expand_path("templates", __dir__)
9
9
  class_option :sidecar, type: :boolean, default: false
10
+ class_option :inline, type: :boolean, default: false
10
11
 
11
12
  def copy_view_file
12
13
  template "component.html.erb", destination
@@ -15,10 +16,12 @@ module Erb
15
16
  private
16
17
 
17
18
  def destination
18
- if options["sidecar"]
19
- File.join("app/components", class_path, "#{file_name}_component", "#{file_name}_component.html.erb")
20
- else
21
- File.join("app/components", class_path, "#{file_name}_component.html.erb")
19
+ if !options["inline"]
20
+ if options["sidecar"]
21
+ File.join("app/components", class_path, "#{file_name}_component", "#{file_name}_component.html.erb")
22
+ else
23
+ File.join("app/components", class_path, "#{file_name}_component.html.erb")
24
+ end
22
25
  end
23
26
  end
24
27
 
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Preview
4
+ module Generators
5
+ class ComponentGenerator < ::Rails::Generators::NamedBase
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ check_class_collision suffix: "ComponentPreview"
9
+
10
+ def create_preview_file
11
+ template "component_preview.rb", File.join("test/components/previews", class_path, "#{file_name}_component_preview.rb")
12
+ end
13
+
14
+ private
15
+
16
+ def file_name
17
+ @_file_name ||= super.sub(/_component\z/i, "")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ class <%= class_name %>ComponentPreview < ViewComponent::Preview
2
+ def default
3
+ render(<%= class_name %>Component.new)
4
+ end
5
+ end
@@ -15,6 +15,8 @@ module ViewComponent
15
15
 
16
16
  ViewContextCalledBeforeRenderError = Class.new(StandardError)
17
17
 
18
+ RESERVED_PARAMETER = :content
19
+
18
20
  # For CSRF authenticity tokens in forms
19
21
  delegate :form_authenticity_token, :protect_against_forgery?, :config, to: :helpers
20
22
 
@@ -264,7 +266,7 @@ module ViewComponent
264
266
  parameter = validate_default ? collection_parameter : provided_collection_parameter
265
267
 
266
268
  return unless parameter
267
- return if initialize_parameters.map(&:last).include?(parameter)
269
+ return if initialize_parameter_names.include?(parameter)
268
270
 
269
271
  # If Ruby cannot parse the component class, then the initalize
270
272
  # parameters will be empty and ViewComponent will not be able to render
@@ -281,8 +283,25 @@ module ViewComponent
281
283
  )
282
284
  end
283
285
 
286
+ # Ensure the component initializer does not define
287
+ # invalid parameters that could override the framework's
288
+ # methods.
289
+ def validate_initialization_parameters!
290
+ return unless initialize_parameter_names.include?(RESERVED_PARAMETER)
291
+
292
+ raise ArgumentError.new(
293
+ "#{self} initializer cannot contain " \
294
+ "`#{RESERVED_PARAMETER}` since it will override a " \
295
+ "public ViewComponent method."
296
+ )
297
+ end
298
+
284
299
  private
285
300
 
301
+ def initialize_parameter_names
302
+ initialize_parameters.map(&:last)
303
+ end
304
+
286
305
  def initialize_parameters
287
306
  instance_method(:initialize).parameters
288
307
  end
@@ -46,7 +46,10 @@ module ViewComponent
46
46
  instance_method(:initialize).parameters.map(&:second).include?(collection_counter_parameter)
47
47
  end
48
48
 
49
- component_class.validate_collection_parameter! if raise_errors
49
+ if raise_errors
50
+ component_class.validate_initialization_parameters!
51
+ component_class.validate_collection_parameter!
52
+ end
50
53
 
51
54
  templates.each do |template|
52
55
  # Remove existing compiled template methods,
@@ -163,9 +166,8 @@ module ViewComponent
163
166
  # end
164
167
  #
165
168
  # Without this, `MyOtherComponent` will not look for `my_component/my_other_component.html.erb`
166
- nested_component_files = if component_class.name.include?("::")
167
- nested_component_path = component_class.name.deconstantize.underscore
168
- Dir["#{directory}/#{nested_component_path}/#{component_name}.*{#{extensions}}"]
169
+ nested_component_files = if component_class.name.include?("::") && component_name != filename
170
+ Dir["#{directory}/#{filename}/#{component_name}.*{#{extensions}}"]
169
171
  else
170
172
  []
171
173
  end
@@ -205,7 +207,6 @@ module ViewComponent
205
207
  end
206
208
  end
207
209
 
208
- # :nocov:
209
210
  def compiled_template(file_path)
210
211
  handler = ActionView::Template.handler_for_extension(File.extname(file_path).gsub(".", ""))
211
212
  template = File.read(file_path)
@@ -216,7 +217,6 @@ module ViewComponent
216
217
  handler.call(OpenStruct.new(source: template, identifier: component_class.identifier, type: component_class.type))
217
218
  end
218
219
  end
219
- # :nocov:
220
220
 
221
221
  def call_method_name(variant)
222
222
  if variant.present? && variants.include?(variant)
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 24
6
+ MINOR = 25
7
7
  PATCH = 0
8
8
 
9
9
  STRING = [MAJOR, MINOR, PATCH].join(".")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.24.0
4
+ version: 2.25.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub Open Source
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-23 00:00:00.000000000 Z
11
+ date: 2021-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -219,6 +219,8 @@ files:
219
219
  - lib/rails/generators/erb/templates/component.html.erb.tt
220
220
  - lib/rails/generators/haml/component_generator.rb
221
221
  - lib/rails/generators/haml/templates/component.html.haml.tt
222
+ - lib/rails/generators/preview/component_generator.rb
223
+ - lib/rails/generators/preview/templates/component_preview.rb.tt
222
224
  - lib/rails/generators/rspec/component_generator.rb
223
225
  - lib/rails/generators/rspec/templates/component_spec.rb.tt
224
226
  - lib/rails/generators/slim/component_generator.rb
@@ -268,7 +270,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
268
270
  - !ruby/object:Gem::Version
269
271
  version: '0'
270
272
  requirements: []
271
- rubygems_version: 3.1.2
273
+ rubygems_version: 3.0.3
272
274
  signing_key:
273
275
  specification_version: 4
274
276
  summary: View components for Rails