view_component 2.46.0 → 2.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

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: b9b1ca38d53b64e55ff434c43f11839cf122d82d17768c2f064e2622ef88dc88
4
- data.tar.gz: 7bc10806de04abbe6c7458305592d09ea6f4dfe9d86aff5d0cf0b9b9cfa9a620
3
+ metadata.gz: 2c94e6fdeccccb7180494d100eef8c78a8dc8e3e9bc30e5647017b440788c6eb
4
+ data.tar.gz: c3836df9c15e8039ba673895ad5a9544aebe55325f9fbd418b96f4bb4238b8d6
5
5
  SHA512:
6
- metadata.gz: 5c100fe77483f4b5f7227a47188f63428cbde65ebddf75cd0accf881d2140536150ea1d52f5c68b66126dd5087d9ac7fd7725f4c829a82d58c5cf86ef8abff2e
7
- data.tar.gz: 19e3230c2b894651462dd6dbae44b4579267cc662c962e2bf9208e28cea299e01286427ae3c949d5622e8302cad4ba2b8f5e58a6aa6dabd3e872de367affb8bb
6
+ metadata.gz: 644f964985c8418371fdcac45e1e7b1771d89ee65ed8a692ec51c50cbed8a578937ba1bb7b9bf1d3b3e3729bd418df5fb2e9d1aee3bbb8215bfbed4504457074
7
+ data.tar.gz: c4df9e14be4dc20e529beb794c96baf424087f7e36423b301402325e1b1ce0ae8ab575fe1b69334b3e27b5fa9e2fee227e9bd20e0e99fe5f384f2f535b43f80c
@@ -29,12 +29,11 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
29
29
  @example_name = File.basename(params[:path])
30
30
  @render_args = @preview.render_args(@example_name, params: params.permit!)
31
31
  layout = determine_layout(@render_args[:layout], prepend_views: false)[:layout]
32
- template = @render_args[:template]
33
32
  locals = @render_args[:locals]
34
33
  opts = {}
35
34
  opts[:layout] = layout if layout.present? || layout == false
36
35
  opts[:locals] = locals if locals.present?
37
- render template, opts # rubocop:disable GitHub/RailsControllerRenderLiteral
36
+ render "view_components/preview", opts # rubocop:disable GitHub/RailsControllerRenderLiteral
38
37
  end
39
38
  end
40
39
 
@@ -4,16 +4,64 @@ module PreviewHelper
4
4
  AVAILABLE_PRISM_LANGUAGES = ["ruby", "erb", "haml"]
5
5
  FALLBACK_LANGUAGE = "ruby"
6
6
 
7
- def prism_language_name(template:)
7
+ def preview_source
8
+ return if @render_args.nil?
9
+
10
+ render "preview_source" # rubocop:disable GitHub/RailsViewRenderPathsExist
11
+ end
12
+
13
+ def find_template_data(lookup_context:, template_identifier:)
14
+ template = lookup_context.find_template(template_identifier)
15
+
16
+ if Rails.version.to_f >= 6.1 || template.source.present?
17
+ return {
18
+ source: template.source,
19
+ prism_language_name: prism_language_name_by_template(template: template)
20
+ }
21
+ else
22
+ # Fetch template source via finding it through preview paths
23
+ # to accomodate source view when exclusively using templates
24
+ # for previews for Rails < 6.1.
25
+ all_template_paths = ViewComponent::Base.preview_paths.map do |preview_path|
26
+ Dir.glob("#{preview_path}/**/*")
27
+ end.flatten
28
+
29
+ # Search for templates the contain `html`.
30
+ matching_templates = all_template_paths.find_all do |template|
31
+ template =~ /#{template_identifier}*.(html)/
32
+ end
33
+
34
+ # In-case of a conflict due to multiple template files with
35
+ # the same name
36
+ raise "found 0 matches for templates for #{template_identifier}." if matching_templates.empty?
37
+ raise "found multiple templates for #{template_identifier}." if matching_templates.size > 1
38
+
39
+ template_file_path = matching_templates.first
40
+ template_source = File.read(template_file_path)
41
+ prism_language_name = prism_language_name_by_template_path(template_file_path: template_file_path)
42
+
43
+ return {
44
+ source: template_source,
45
+ prism_language_name: prism_language_name
46
+ }
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def prism_language_name_by_template(template:)
8
53
  language = template.identifier.split(".").last
54
+
9
55
  return FALLBACK_LANGUAGE unless AVAILABLE_PRISM_LANGUAGES.include? language
10
56
 
11
57
  language
12
58
  end
13
59
 
14
- def preview_source
15
- return if @render_args.nil?
60
+ def prism_language_name_by_template_path(template_file_path:)
61
+ language = template_file_path.gsub(".html", "").split(".").last
16
62
 
17
- render "preview_source" # rubocop:disable GitHub/RailsViewRenderPathsExist
63
+ return FALLBACK_LANGUAGE unless AVAILABLE_PRISM_LANGUAGES.include? language
64
+
65
+ language
18
66
  end
19
67
  end
@@ -7,9 +7,9 @@
7
7
  <%= h @preview.preview_source(@example_name) %>
8
8
  </code>
9
9
  <% else %>
10
- <% template = @view_renderer.lookup_context.find_template(@render_args[:template]) %>
11
- <code class="language-<%= prism_language_name(template: template) %>">
12
- <%= h template.source %>
10
+ <% template_data = find_template_data(lookup_context: @view_renderer.lookup_context, template_identifier: @render_args[:template]) %>
11
+ <code class="language-<%= template_data[:prism_language_name] %>">
12
+ <%= h template_data[:source] %>
13
13
  </code>
14
14
  <% end %>
15
15
  </pre>
@@ -1,7 +1,11 @@
1
- <% if ViewComponent::Base.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
2
- <%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
1
+ <% if @render_args[:component] %>
2
+ <% if ViewComponent::Base.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
3
+ <%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
4
+ <% else %>
5
+ <%= render_component(@render_args[:component], &@render_args[:block]) %>
6
+ <% end %>
3
7
  <% else %>
4
- <%= render_component(@render_args[:component], &@render_args[:block]) %>
8
+ <%= render template: @render_args[:template], locals: @render_args[:locals] || {} %>
5
9
  <% end %>
6
10
 
7
11
  <% if ViewComponent::Base.show_previews_source %>
data/docs/CHANGELOG.md CHANGED
@@ -7,6 +7,78 @@ title: Changelog
7
7
 
8
8
  ## main
9
9
 
10
+ ## 2.47.0
11
+
12
+ * Display preview source on previews that exclusively use templates.
13
+
14
+ *Edwin Mak*
15
+
16
+ * Add a test to ensure trailing newlines are stripped when rendering with `#render_in`.
17
+
18
+ *Simon Fish*
19
+
20
+ * Add WEBrick as a depenency to the docs application.
21
+
22
+ *Connor McQuillan*
23
+
24
+ * Update Ruby version in `.tool-versions`.
25
+
26
+ *Connor McQuillan*
27
+
28
+ * Add a test to ensure blocks can be passed into lambda slots without the need for any other arguments.
29
+
30
+ *Simon Fish*
31
+
32
+ * Add linters for file consistency.
33
+
34
+ *Simon Fish*
35
+
36
+ * Add @boardfish to docs/index.md and sort contributors.
37
+
38
+ *Simon Fish*
39
+
40
+ * Set up Codespaces for bug replication.
41
+
42
+ *Simon Fish*
43
+
44
+ * Add instructions for replicating bugs and failures.
45
+
46
+ *Simon Fish*
47
+
48
+ * Make @boardfish a committer.
49
+
50
+ *Joel Hawksley*
51
+
52
+ * Validate collection parameter with Active Model attribute names.
53
+
54
+ *Simon Fish*
55
+
56
+ * Fix `helpers` not working with component slots when rendered more than 2 component levels deep.
57
+
58
+ *Blake Williams*
59
+
60
+ * Update ruby to the latest versions
61
+
62
+ *Pedro Paiva*
63
+
64
+ * Fix `vale` linter config options.
65
+
66
+ *Hans Lemuet*
67
+
68
+ * Improve Contributing docs to include how to run tests for a specific version on Rails.
69
+
70
+ *Hans Lemuet*
71
+
72
+ * Add failing test for default form builder and documentation around known issue.
73
+
74
+ *Simon Fish*
75
+
76
+ * Add `--locale` flag to the component generator. Generates as many locale files as defined in `I18n.available_locales`, alongside the component.
77
+ * Add config option `config.view_component.generate_locale` to enable project-wide locale generation.
78
+ * Add config option `config.view_component.generate_distinct_locale_files` to enable project-wide per-locale translations file generation.
79
+
80
+ *Bob Maerten*
81
+
10
82
  ## 2.46.0
11
83
 
12
84
  * Add thread safety to the compiler.
@@ -25,6 +97,17 @@ title: Changelog
25
97
 
26
98
  *Simon Fish*
27
99
 
100
+ * Deprecate loading `view_component/engine` directly.
101
+
102
+ **Upgrade notice**: You should update your `Gemfile` like this:
103
+
104
+ ```diff
105
+ - gem "view_component", require: "view_component/engine"`
106
+ + gem "view_component"
107
+ ```
108
+
109
+ *Yoshiyuki Hirano*
110
+
28
111
  ## 2.45.0
29
112
 
30
113
  * Remove internal APIs from API documentation, fix link to license.
@@ -63,10 +146,6 @@ title: Changelog
63
146
 
64
147
  *Yoshiyuki Hirano*
65
148
 
66
- * Deprecate engine loading manually.
67
-
68
- *Yoshiyuki Hirano*
69
-
70
149
  ## 2.44.0
71
150
 
72
151
  * Rename internal accessor to use private naming.
@@ -15,6 +15,7 @@ module Rails
15
15
  class_option :parent, type: :string, desc: "The parent class for the generated component"
16
16
  class_option :stimulus, type: :boolean, default: ViewComponent::Base.generate_stimulus_controller
17
17
  class_option :sidecar, type: :boolean, default: false
18
+ class_option :locale, type: :boolean, default: ViewComponent::Base.generate_locale
18
19
 
19
20
  def create_component_file
20
21
  template "component.rb", File.join(component_path, class_path, "#{file_name}_component.rb")
@@ -26,6 +27,8 @@ module Rails
26
27
 
27
28
  hook_for :stimulus, type: :boolean
28
29
 
30
+ hook_for :locale, type: :boolean
31
+
29
32
  hook_for :template_engine do |instance, template_engine|
30
33
  instance.invoke template_engine, [instance.name]
31
34
  end
@@ -6,7 +6,7 @@ class <%= class_name %>Component < <%= parent_class %>
6
6
  <%= initialize_body %>
7
7
  end
8
8
  <%- end -%>
9
- <%- if initialize_call_method_for_inline? -%>
9
+ <%- if initialize_call_method_for_inline? -%>
10
10
  def call
11
11
  content_tag :h1, "Hello world!"<%= ", data: { controller: \"#{stimulus_controller}\" }" if options["stimulus"] %>
12
12
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/abstract_generator"
4
+
5
+ module Locale
6
+ module Generators
7
+ class ComponentGenerator < ::Rails::Generators::NamedBase
8
+ include ViewComponent::AbstractGenerator
9
+
10
+ source_root File.expand_path("templates", __dir__)
11
+ argument :attributes, type: :array, default: [], banner: "attribute"
12
+ class_option :sidecar, type: :boolean, default: false
13
+
14
+ def create_locale_file
15
+ if ViewComponent::Base.generate_distinct_locale_files
16
+ I18n.available_locales.each do |locale|
17
+ create_file destination(locale), translations_hash([locale]).to_yaml
18
+ end
19
+ else
20
+ create_file destination, translations_hash(I18n.available_locales).to_yaml
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def translations_hash(locales = [:en])
27
+ locales.map { |locale| [locale.to_s, translation_keys] }.to_h
28
+ end
29
+
30
+ def translation_keys
31
+ keys = attributes.map(&:name)
32
+ keys = %w[hello] if keys.empty?
33
+ keys.map { |name| [name, name.capitalize] }.to_h
34
+ end
35
+
36
+ def destination(locale = nil)
37
+ extention = ".#{locale}" if locale
38
+ if options["sidecar"]
39
+ File.join(component_path, class_path, "#{file_name}_component", "#{file_name}_component#{extention}.yml")
40
+ else
41
+ File.join(component_path, class_path, "#{file_name}_component#{extention}.yml")
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -279,6 +279,25 @@ module ViewComponent
279
279
  #
280
280
  mattr_accessor :generate_stimulus_controller, instance_writer: false, default: false
281
281
 
282
+ # Always generate translations file alongside the component:
283
+ #
284
+ # config.view_component.generate_locale = true
285
+ #
286
+ # Defaults to `false`.
287
+ #
288
+ mattr_accessor :generate_locale, instance_writer: false, default: false
289
+
290
+ # Always generate as many translations files as available locales:
291
+ #
292
+ # config.view_component.generate_distinct_locale_files = true
293
+ #
294
+ # Defaults to `false`.
295
+ #
296
+ # One file will be generated for each configured `I18n.available_locales`.
297
+ # Fallback on `[:en]` when no available_locales is defined.
298
+ #
299
+ mattr_accessor :generate_distinct_locale_files, instance_writer: false, default: false
300
+
282
301
  # Path for component files
283
302
  #
284
303
  # config.view_component.view_component_path = "app/my_components"
@@ -507,6 +526,10 @@ module ViewComponent
507
526
  private
508
527
 
509
528
  def initialize_parameter_names
529
+ return attribute_names.map(&:to_sym) if respond_to?(:attribute_names)
530
+
531
+ return attribute_types.keys.map(&:to_sym) if Rails::VERSION::MAJOR <= 5 && respond_to?(:attribute_types)
532
+
510
533
  initialize_parameters.map(&:last)
511
534
  end
512
535
 
@@ -40,6 +40,8 @@ module ViewComponent
40
40
 
41
41
  @content =
42
42
  if defined?(@__vc_component_instance)
43
+ @__vc_component_instance.__vc_original_view_context = @parent.__vc_original_view_context
44
+
43
45
  if defined?(@__vc_content_set_by_with_content)
44
46
  @__vc_component_instance.with_content(@__vc_content_set_by_with_content)
45
47
 
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 46
6
+ MINOR = 47
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.46.0
4
+ version: 2.47.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: 2021-12-03 00:00:00.000000000 Z
11
+ date: 2021-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -295,6 +295,7 @@ files:
295
295
  - lib/rails/generators/erb/templates/component.html.erb.tt
296
296
  - lib/rails/generators/haml/component_generator.rb
297
297
  - lib/rails/generators/haml/templates/component.html.haml.tt
298
+ - lib/rails/generators/locale/component_generator.rb
298
299
  - lib/rails/generators/preview/component_generator.rb
299
300
  - lib/rails/generators/preview/templates/component_preview.rb.tt
300
301
  - lib/rails/generators/rspec/component_generator.rb