Skip to content

Commit 85d15a4

Browse files
authored
Merge pull request #2354 from ViewComponent/v4-var-rename
Rename more methods and variables to avoid collisions with consumers
2 parents 7dc2b01 + 935989a commit 85d15a4

8 files changed

Lines changed: 83 additions & 84 deletions

File tree

lib/view_component.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ module ViewComponent
77
extend ActiveSupport::Autoload
88

99
autoload :Base
10-
autoload :CaptureCompatibility
1110
autoload :Compiler
1211
autoload :CompileCache
1312
autoload :Config
1413
autoload :Deprecation
1514
autoload :InlineTemplate
1615
autoload :Instrumentation
1716
autoload :Preview
18-
autoload :TestHelpers
19-
autoload :SystemTestHelpers
20-
autoload :TestCase
21-
autoload :SystemTestCase
2217
autoload :Translatable
18+
19+
if Rails.env.test?
20+
autoload :TestHelpers
21+
autoload :SystemTestHelpers
22+
autoload :TestCase
23+
autoload :SystemTestCase
24+
end
2325
end
2426

2527
require "view_component/engine" if defined?(Rails::Engine)

lib/view_component/base.rb

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def render_in(view_context, &block)
146146
value = if output_preamble.blank? && output_postamble.blank?
147147
rendered_template
148148
else
149-
safe_output_preamble + rendered_template + safe_output_postamble
149+
__vc_safe_output_preamble + rendered_template + __vc_safe_output_postamble
150150
end
151151
end
152152

@@ -355,11 +355,7 @@ def __vc_content_set_by_with_content_defined?
355355
defined?(@__vc_content_set_by_with_content)
356356
end
357357

358-
def content_evaluated?
359-
defined?(@__vc_content_evaluated) && @__vc_content_evaluated
360-
end
361-
362-
def maybe_escape_html(text)
358+
def __vc_maybe_escape_html(text)
363359
return text if @current_template && !@current_template.html?
364360
return text if text.blank?
365361

@@ -371,14 +367,14 @@ def maybe_escape_html(text)
371367
end
372368
end
373369

374-
def safe_output_preamble
375-
maybe_escape_html(output_preamble) do
370+
def __vc_safe_output_preamble
371+
__vc_maybe_escape_html(output_preamble) do
376372
Kernel.warn("WARNING: The #{self.class} component was provided an HTML-unsafe preamble. The preamble will be automatically escaped, but you may want to investigate.")
377373
end
378374
end
379375

380-
def safe_output_postamble
381-
maybe_escape_html(output_postamble) do
376+
def __vc_safe_output_postamble
377+
__vc_maybe_escape_html(output_postamble) do
382378
Kernel.warn("WARNING: The #{self.class} component was provided an HTML-unsafe postamble. The postamble will be automatically escaped, but you may want to investigate.")
383379
end
384380
end
@@ -556,7 +552,7 @@ def render_template_for(requested_details)
556552
child.virtual_path = child.name&.underscore
557553

558554
# Set collection parameter to the extended component
559-
child.with_collection_parameter provided_collection_parameter
555+
child.with_collection_parameter(__vc_provided_collection_parameter)
560556

561557
if instance_methods(false).include?(:render_template_for)
562558
vc_ancestor_calls = defined?(@__vc_ancestor_calls) ? @__vc_ancestor_calls.dup : []
@@ -591,8 +587,8 @@ def __vc_compiler
591587
#
592588
# @param parameter [Symbol] The parameter name used when rendering elements of a collection.
593589
def with_collection_parameter(parameter)
594-
@provided_collection_parameter = parameter
595-
@initialize_parameters = nil
590+
@__vc_provided_collection_parameter = parameter
591+
@__vc_initialize_parameters = nil
596592
end
597593

598594
# Strips trailing whitespace from templates before compiling them.
@@ -622,10 +618,10 @@ def strip_trailing_whitespace?
622618
# rendering is optional.
623619
# @private
624620
def __vc_validate_collection_parameter!(validate_default: false)
625-
parameter = validate_default ? __vc_collection_parameter : provided_collection_parameter
621+
parameter = validate_default ? __vc_collection_parameter : __vc_provided_collection_parameter
626622

627623
return unless parameter
628-
return if __vc_initialize_parameter_names.include?(parameter) || splatted_keyword_argument_present?
624+
return if __vc_initialize_parameter_names.include?(parameter) || __vc_splatted_keyword_argument_present?
629625

630626
raise MissingCollectionArgumentError.new(name, parameter)
631627
end
@@ -642,7 +638,7 @@ def __vc_validate_initialization_parameters!
642638

643639
# @private
644640
def __vc_collection_parameter
645-
@provided_collection_parameter ||= name && name.demodulize.underscore.chomp("_component").to_sym
641+
@__vc_provided_collection_parameter ||= name && name.demodulize.underscore.chomp("_component").to_sym
646642
end
647643

648644
# @private
@@ -667,25 +663,25 @@ def __vc_iteration_argument_present?
667663

668664
private
669665

670-
def splatted_keyword_argument_present?
671-
initialize_parameters.flatten.include?(:keyrest)
666+
def __vc_splatted_keyword_argument_present?
667+
__vc_initialize_parameters.flatten.include?(:keyrest)
672668
end
673669

674670
def __vc_initialize_parameter_names
675671
@__vc_initialize_parameter_names ||=
676672
if respond_to?(:attribute_names)
677673
attribute_names.map(&:to_sym)
678674
else
679-
initialize_parameters.map(&:last)
675+
__vc_initialize_parameters.map(&:last)
680676
end
681677
end
682678

683-
def initialize_parameters
684-
@initialize_parameters ||= instance_method(:initialize).parameters
679+
def __vc_initialize_parameters
680+
@__vc_initialize_parameters ||= instance_method(:initialize).parameters
685681
end
686682

687-
def provided_collection_parameter
688-
@provided_collection_parameter ||= nil
683+
def __vc_provided_collection_parameter
684+
@__vc_provided_collection_parameter ||= nil
689685
end
690686
end
691687

lib/view_component/compiler.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def compile(raise_errors: false, force: false)
5757
unique_formats = templates.map(&:format).uniq
5858
@component.__vc_response_format = unique_formats.last if unique_formats.one?
5959

60-
@component.register_default_slots
60+
@component.__vc_register_default_slots
6161
@component.__vc_build_i18n_backend
6262

6363
CompileCache.register(@component)
@@ -177,10 +177,10 @@ def template_errors
177177

178178
def gather_templates
179179
@templates ||=
180-
if @component.inline_template.present?
180+
if @component.__vc_inline_template.present?
181181
[Template::Inline.new(
182182
component: @component,
183-
inline_template: @component.inline_template
183+
inline_template: @component.__vc_inline_template
184184
)]
185185
else
186186
path_parser = ActionView::Resolver::PathParser.new

lib/view_component/inline_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def respond_to_missing?(method, include_all = false)
3737
method.end_with?("_template") || super
3838
end
3939

40-
def inline_template
40+
def __vc_inline_template
4141
@__vc_inline_template if defined?(@__vc_inline_template)
4242
end
4343

0 commit comments

Comments
 (0)