Skip to content

Commit d629a83

Browse files
tomascjoelhawksley
andauthored
Refine config method in ViewComponent::Base (#2278)
* Let's be a bit gentler with trying the module parents on config. Also, avoid naming variables inside after the method name itself. Refactor config method in ViewComponent to improve readability and safety. Use explicit checks for module parent configuration instead of chaining try methods. * fix allocations * more allocations --------- Co-authored-by: Joel Hawksley <[email protected]> Co-authored-by: Joel Hawksley <[email protected]>
1 parent 1b3052f commit d629a83

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

lib/view_component/base.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ class << self
2626
#
2727
# @return [ActiveSupport::OrderedOptions]
2828
def config
29-
module_parents.each do |m|
30-
config = m.try(:config).try(:view_component)
31-
return config if config
29+
module_parents.each do |module_parent|
30+
next unless module_parent.respond_to?(:config)
31+
module_parent_config = module_parent.config.try(:view_component)
32+
return module_parent_config if module_parent_config
3233
end
3334
ViewComponent::Config.current
3435
end

test/sandbox/test/rendering_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ def test_render_inline_allocations
1717

1818
allocations =
1919
if Rails.version.to_f < 8.0
20-
{"3.3.8" => 128, "3.3.0" => 140, "3.2.8" => 126, "3.1.7" => 126, "3.0.7" => 135}
20+
{"3.3.8" => 124, "3.3.0" => 140, "3.2.8" => 122, "3.1.7" => 122, "3.0.7" => 131}
2121
elsif Rails.version.split(".").first(2).map(&:to_i) == [8, 0]
22-
{"3.5.0" => 121, "3.4.3" => 125, "3.3.8" => 137}
22+
{"3.5.0" => 117, "3.4.4" => 121, "3.3.8" => 133}
2323
else
24-
{"3.4.3" => 123}
24+
{"3.4.4" => 119}
2525
end
2626

2727
assert_allocations(**allocations) do

0 commit comments

Comments
 (0)