Skip to content

Commit a75cf58

Browse files
Merge branch 'main' into rv_add_component_caching
2 parents 2b50b27 + 212d159 commit a75cf58

File tree

6 files changed

+60
-33
lines changed

6 files changed

+60
-33
lines changed

docs/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ nav_order: 5
1010

1111
## main
1212

13+
* Register ViewComponent tests directory for `rails stats`.
14+
15+
*Javier Aranda*
16+
17+
* Wrap entire compile step in a mutex to make it more resilient to race conditions.
18+
19+
*Blake Williams*
20+
21+
* Add [Niva]([niva.co](https://www.niva.co/)) to companies who use `ViewComponent`.
22+
23+
*Daniel Vu Dao*
24+
1325
* Fix `preview_paths` in docs.
1426

1527
*Javier Aranda*

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ ViewComponent is built by over a hundred members of the community, including:
264264
* [Mission Met Center](https://www.missionmet.com/mission-met-center)
265265
* [Mon Ami](https://www.monami.io)
266266
* [Nikola Motor](https://www.nikolamotor.com/) (50+ components and counting)
267+
* [Niva](https://www.niva.co/)
267268
* [OBLSK](https://oblsk.com/)
268269
* [openSUSE Open Build Service](https://openbuildservice.org/)
269270
* [Ophelos](https://ophelos.com)

lib/view_component/compiler.rb

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Compiler
1212

1313
def initialize(component)
1414
@component = component
15-
@redefinition_lock = Mutex.new
15+
@lock = Mutex.new
1616
@rendered_templates = Set.new
1717
end
1818

@@ -24,30 +24,36 @@ def compile(raise_errors: false, force: false)
2424
return if compiled? && !force
2525
return if @component == ViewComponent::Base
2626

27-
gather_templates
27+
@lock.synchronize do
28+
# this check is duplicated so that concurrent compile calls can still
29+
# early exit
30+
return if compiled? && !force
2831

29-
if self.class.development_mode && @templates.any?(&:requires_compiled_superclass?)
30-
@component.superclass.compile(raise_errors: raise_errors)
31-
end
32+
gather_templates
3233

33-
if template_errors.present?
34-
raise TemplateError.new(template_errors) if raise_errors
34+
if self.class.development_mode && @templates.any?(&:requires_compiled_superclass?)
35+
@component.superclass.compile(raise_errors: raise_errors)
36+
end
3537

36-
# this return is load bearing, and prevents the component from being considered "compiled?"
37-
return false
38-
end
38+
if template_errors.present?
39+
raise TemplateError.new(template_errors) if raise_errors
3940

40-
if raise_errors
41-
@component.validate_initialization_parameters!
42-
@component.validate_collection_parameter!
43-
end
41+
# this return is load bearing, and prevents the component from being considered "compiled?"
42+
return false
43+
end
4444

45-
define_render_template_for
45+
if raise_errors
46+
@component.validate_initialization_parameters!
47+
@component.validate_collection_parameter!
48+
end
49+
50+
define_render_template_for
4651

47-
@component.register_default_slots
48-
@component.build_i18n_backend
52+
@component.register_default_slots
53+
@component.build_i18n_backend
4954

50-
CompileCache.register(@component)
55+
CompileCache.register(@component)
56+
end
5157
end
5258

5359
def renders_template_for?(variant, format)
@@ -60,9 +66,7 @@ def renders_template_for?(variant, format)
6066

6167
def define_render_template_for
6268
@templates.each do |template|
63-
@redefinition_lock.synchronize do
64-
template.compile_to_component
65-
end
69+
template.compile_to_component
6670
end
6771

6872
method_body =
@@ -93,14 +97,12 @@ def define_render_template_for
9397
out << "else\n #{templates.find { _1.variant.nil? && _1.default_format? }.safe_method_name}\nend"
9498
end
9599

96-
@redefinition_lock.synchronize do
97-
@component.silence_redefinition_of_method(:render_template_for)
98-
@component.class_eval <<-RUBY, __FILE__, __LINE__ + 1
99-
def render_template_for(variant = nil, format = nil)
100-
#{method_body}
101-
end
102-
RUBY
100+
@component.silence_redefinition_of_method(:render_template_for)
101+
@component.class_eval <<-RUBY, __FILE__, __LINE__ + 1
102+
def render_template_for(variant = nil, format = nil)
103+
#{method_body}
103104
end
105+
RUBY
104106
end
105107

106108
def template_errors

lib/view_component/engine.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ class Engine < Rails::Engine # :nodoc:
1515
else
1616
initializer "view_component.stats_directories" do |app|
1717
require "rails/code_statistics"
18-
dir = ViewComponent::Base.view_component_path
19-
Rails::CodeStatistics.register_directory("ViewComponents", dir)
18+
19+
if Rails.root.join(ViewComponent::Base.view_component_path).directory?
20+
Rails::CodeStatistics.register_directory("ViewComponents", ViewComponent::Base.view_component_path)
21+
end
22+
23+
if Rails.root.join("test/components").directory?
24+
Rails::CodeStatistics.register_directory("ViewComponent tests", "test/components", test_directory: true)
25+
end
2026
end
2127
end
2228

lib/view_component/rails/tasks/view_component.rake

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ namespace :view_component do
77
# :nocov:
88
require "rails/code_statistics"
99

10-
dir = ViewComponent::Base.view_component_path
11-
::STATS_DIRECTORIES << ["ViewComponents", dir] if File.directory?(Rails.root + dir)
10+
if Rails.root.join(ViewComponent::Base.view_component_path).directory?
11+
::STATS_DIRECTORIES << ["ViewComponents", ViewComponent::Base.view_component_path]
12+
end
13+
14+
if Rails.root.join("test/components").directory?
15+
::STATS_DIRECTORIES << ["ViewComponent tests", "test/components"]
16+
CodeStatistics::TEST_TYPES << "ViewComponent tests"
17+
end
1218
# :nocov:
1319
end
1420
end

test/sandbox/test/rendering_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_render_inline_allocations
1515
ViewComponent::CompileCache.cache.delete(MyComponent)
1616
MyComponent.ensure_compiled
1717

18-
assert_allocations("3.4.0" => 107, "3.3.5" => 116, "3.3.0" => 129, "3.2.5" => 115, "3.1.6" => 115, "3.0.7" => 125) do
18+
assert_allocations("3.4.0" => 110, "3.3.5" => 116, "3.3.0" => 129, "3.2.5" => 115, "3.1.6" => 115, "3.0.7" => 125) do
1919
render_inline(MyComponent.new)
2020
end
2121

0 commit comments

Comments
 (0)