Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Underscore internal methods that are public #2100

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def set_original_view_context(view_context)
#
# @return [String]
def render_in(view_context, &block)
self.class.compile(raise_errors: true)
self.class.__vc_compile(raise_errors: true)

@view_context = view_context
self.__vc_original_view_context ||= view_context
Expand Down Expand Up @@ -109,7 +109,7 @@ def render_in(view_context, &block)

if render?
rendered_template =
if compiler.renders_template_for?(@__vc_variant, request&.format&.to_sym)
if __vc_compiler.renders_template_for?(@__vc_variant, request&.format&.to_sym)
render_template_for(@__vc_variant, request&.format&.to_sym)
else
maybe_escape_html(render_template_for(@__vc_variant, request&.format&.to_sym)) do
Expand Down Expand Up @@ -351,8 +351,8 @@ def safe_output_postamble
end
end

def compiler
@compiler ||= self.class.compiler
def __vc_compiler
@compiler ||= self.class.__vc_compiler
end

# Set the controller used for testing components:
Expand Down Expand Up @@ -503,18 +503,18 @@ def with_collection(collection, **args)
def inherited(child)
# Compile so child will inherit compiled `call_*` template methods that
# `compile` defines
compile
__vc_compile

# Give the child its own personal #render_template_for to protect against the case when
# eager loading is disabled and the parent component is rendered before the child. In
# such a scenario, the parent will override ViewComponent::Base#render_template_for,
# meaning it will not be called for any children and thus not compile their templates.
if !child.instance_methods(false).include?(:render_template_for) && !child.compiled?
if !child.instance_methods(false).include?(:render_template_for) && !child.__vc_compiled?
child.class_eval <<~RUBY, __FILE__, __LINE__ + 1
def render_template_for(variant = nil, format = nil)
# Force compilation here so the compiler always redefines render_template_for.
# This is mostly a safeguard to prevent infinite recursion.
self.class.compile(raise_errors: true, force: true)
self.class.__vc_compile(raise_errors: true, force: true)
# .compile replaces this method; call the new one
render_template_for(variant, format)
end
Expand Down Expand Up @@ -555,22 +555,22 @@ def render_template_for(variant = nil, format = nil)
end

# @private
def compiled?
compiler.compiled?
def __vc_compiled?
__vc_compiler.compiled?
end

# @private
def ensure_compiled
compile unless compiled?
def __vc_ensure_compiled
__vc_compile unless __vc_compiled?
end

# @private
def compile(raise_errors: false, force: false)
compiler.compile(raise_errors: raise_errors, force: force)
def __vc_compile(raise_errors: false, force: false)
__vc_compiler.compile(raise_errors: raise_errors, force: force)
end

# @private
def compiler
def __vc_compiler
@__vc_compiler ||= Compiler.new(self)
end

Expand Down Expand Up @@ -612,8 +612,8 @@ def strip_trailing_whitespace?
# is accepted, as support for collection
# rendering is optional.
# @private TODO: add documentation
def validate_collection_parameter!(validate_default: false)
parameter = validate_default ? collection_parameter : provided_collection_parameter
def __vc_validate_collection_parameter!(validate_default: false)
parameter = validate_default ? __vc_collection_parameter : provided_collection_parameter

return unless parameter
return if initialize_parameter_names.include?(parameter) || splatted_keyword_argument_present?
Expand All @@ -632,35 +632,35 @@ def validate_collection_parameter!(validate_default: false)
# invalid parameters that could override the framework's
# methods.
# @private TODO: add documentation
def validate_initialization_parameters!
def __vc_validate_initialization_parameters!
return unless initialize_parameter_names.include?(RESERVED_PARAMETER)

raise ReservedParameterError.new(name, RESERVED_PARAMETER)
end

# @private
def collection_parameter
def __vc_collection_parameter
provided_collection_parameter || name && name.demodulize.underscore.chomp("_component").to_sym
end

# @private
def collection_counter_parameter
:"#{collection_parameter}_counter"
def __vc_collection_counter_parameter
:"#{__vc_collection_parameter}_counter"
end

# @private
def counter_argument_present?
initialize_parameter_names.include?(collection_counter_parameter)
def __vc_counter_argument_present?
initialize_parameter_names.include?(__vc_collection_counter_parameter)
end

# @private
def collection_iteration_parameter
:"#{collection_parameter}_iteration"
def __vc_collection_iteration_parameter
:"#{__vc_collection_parameter}_iteration"
end

# @private
def iteration_argument_present?
initialize_parameter_names.include?(collection_iteration_parameter)
def __vc_iteration_argument_present?
initialize_parameter_names.include?(__vc_collection_iteration_parameter)
end

private
Expand Down
8 changes: 4 additions & 4 deletions lib/view_component/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def components

iterator = ActionView::PartialIteration.new(@collection.size)

component.validate_collection_parameter!(validate_default: true)
component.__vc_validate_collection_parameter!(validate_default: true)

@components = @collection.map do |item|
component.new(**component_options(item, iterator)).tap do |component|
Expand Down Expand Up @@ -63,9 +63,9 @@ def collection_variable(object)
end

def component_options(item, iterator)
item_options = {component.collection_parameter => item}
item_options[component.collection_counter_parameter] = iterator.index if component.counter_argument_present?
item_options[component.collection_iteration_parameter] = iterator.dup if component.iteration_argument_present?
item_options = {component.__vc_collection_parameter => item}
item_options[component.__vc_collection_counter_parameter] = iterator.index if component.__vc_counter_argument_present?
item_options[component.__vc_collection_iteration_parameter] = iterator.dup if component.__vc_iteration_argument_present?

@options.merge(item_options)
end
Expand Down
10 changes: 5 additions & 5 deletions lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ def compile(raise_errors: false, force: false)
gather_templates

if self.class.development_mode && @templates.any?(&:requires_compiled_superclass?)
@component.superclass.compile(raise_errors: raise_errors)
@component.superclass.__vc_compile(raise_errors: raise_errors)
end

return if gather_template_errors(raise_errors).any?

if raise_errors
@component.validate_initialization_parameters!
@component.validate_collection_parameter!
@component.__vc_validate_initialization_parameters!
@component.__vc_validate_collection_parameter!
end

define_render_template_for

@component.register_default_slots
@component.build_i18n_backend
@component.__vc_register_default_slots
@component.__vc_build_i18n_backend

CompileCache.register(@component)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Engine < Rails::Engine # :nodoc:

initializer "view_component.eager_load_actions" do
ActiveSupport.on_load(:after_initialize) do
ViewComponent::Base.descendants.each(&:compile) if Rails.application.config.eager_load
ViewComponent::Base.descendants.each(&:__vc_compile) if Rails.application.config.eager_load
end
end

Expand Down
34 changes: 17 additions & 17 deletions lib/view_component/slotable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module Slotable
# Setup component slot state
included do
# Hash of registered Slots
class_attribute :registered_slots
self.registered_slots = {}
class_attribute :__vc_registered_slots
self.__vc_registered_slots = {}
end

class_methods do
Expand Down Expand Up @@ -78,7 +78,7 @@ def renders_one(slot_name, callable = nil)
validate_singular_slot_name(slot_name)

if callable.is_a?(Hash) && callable.key?(:types)
register_polymorphic_slot(slot_name, callable[:types], collection: false)
__vc_register_polymorphic_slot(slot_name, callable[:types], collection: false)
else
validate_plural_slot_name(ActiveSupport::Inflector.pluralize(slot_name).to_sym)

Expand Down Expand Up @@ -148,7 +148,7 @@ def renders_many(slot_name, callable = nil)
validate_plural_slot_name(slot_name)

if callable.is_a?(Hash) && callable.key?(:types)
register_polymorphic_slot(slot_name, callable[:types], collection: true)
__vc_register_polymorphic_slot(slot_name, callable[:types], collection: true)
else
singular_name = ActiveSupport::Inflector.singularize(slot_name)
validate_singular_slot_name(ActiveSupport::Inflector.singularize(slot_name).to_sym)
Expand Down Expand Up @@ -189,20 +189,20 @@ def renders_many(slot_name, callable = nil)
end

def slot_type(slot_name)
registered_slot = registered_slots[slot_name]
registered_slot = __vc_registered_slots[slot_name]
if registered_slot
registered_slot[:collection] ? :collection : :single
else
plural_slot_name = ActiveSupport::Inflector.pluralize(slot_name).to_sym
plural_registered_slot = registered_slots[plural_slot_name]
plural_registered_slot = __vc_registered_slots[plural_slot_name]
plural_registered_slot&.fetch(:collection) ? :collection_item : nil
end
end

def inherited(child)
# Clone slot configuration into child class
# see #test_slots_pollution
child.registered_slots = registered_slots.clone
child.__vc_registered_slots = __vc_registered_slots.clone

# Add a module for slot methods, allowing them to be overriden by the component class
# see #test_slot_name_can_be_overriden
Expand All @@ -215,7 +215,7 @@ def inherited(child)
super
end

def register_polymorphic_slot(slot_name, types, collection:)
def __vc_register_polymorphic_slot(slot_name, types, collection:)
self::GeneratedSlotMethods.define_method(slot_name) do
get_slot(slot_name)
end
Expand Down Expand Up @@ -262,25 +262,25 @@ def register_polymorphic_slot(slot_name, types, collection:)
end
end

registered_slots[slot_name] = {
__vc_registered_slots[slot_name] = {
collection: collection,
renderable_hash: renderable_hash
}
end

# Called by the compiler, as instance methods are not defined when slots are first registered
def register_default_slots
registered_slots.each do |slot_name, config|
def __vc_register_default_slots
__vc_registered_slots.each do |slot_name, config|
config[:default_method] = instance_methods.find { |method_name| method_name == :"default_#{slot_name}" }

registered_slots[slot_name] = config
__vc_registered_slots[slot_name] = config
end
end

private

def register_slot(slot_name, **kwargs)
registered_slots[slot_name] = define_slot(slot_name, **kwargs)
__vc_registered_slots[slot_name] = define_slot(slot_name, **kwargs)
end

def define_slot(slot_name, collection:, callable:)
Expand Down Expand Up @@ -332,7 +332,7 @@ def validate_singular_slot_name(slot_name)
end

def raise_if_slot_registered(slot_name)
if registered_slots.key?(slot_name)
if __vc_registered_slots.key?(slot_name)
# TODO remove? This breaks overriding slots when slots are inherited
raise RedefinedSlotError.new(name, slot_name)
end
Expand All @@ -359,7 +359,7 @@ def raise_if_slot_name_uncountable(slot_name)
def get_slot(slot_name)
content unless content_evaluated? # ensure content is loaded so slots will be defined

slot = self.class.registered_slots[slot_name]
slot = self.class.__vc_registered_slots[slot_name]
@__vc_set_slots ||= {}

if @__vc_set_slots[slot_name]
Expand All @@ -372,7 +372,7 @@ def get_slot(slot_name)
end

def set_slot(slot_name, slot_definition = nil, *args, &block)
slot_definition ||= self.class.registered_slots[slot_name]
slot_definition ||= self.class.__vc_registered_slots[slot_name]
slot = Slot.new(self)

# Passing the block to the sub-component wrapper like this has two
Expand Down Expand Up @@ -430,7 +430,7 @@ def set_slot(slot_name, slot_definition = nil, *args, &block)
ruby2_keywords(:set_slot) if respond_to?(:ruby2_keywords, true)

def set_polymorphic_slot(slot_name, poly_type = nil, *args, &block)
slot_definition = self.class.registered_slots[slot_name]
slot_definition = self.class.__vc_registered_slots[slot_name]

if !slot_definition[:collection] && (defined?(@__vc_set_slots) && @__vc_set_slots[slot_name])
raise ContentAlreadySetForPolymorphicSlotError.new(slot_name)
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/slotable_default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module SlotableDefault
def get_slot(slot_name)
@__vc_set_slots ||= {}

return super unless !@__vc_set_slots[slot_name] && (default_method = registered_slots[slot_name][:default_method])
return super unless !@__vc_set_slots[slot_name] && (default_method = __vc_registered_slots[slot_name][:default_method])

renderable_value = send(default_method)
slot = Slot.new(self)
Expand Down
16 changes: 8 additions & 8 deletions lib/view_component/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ module Translatable
TRANSLATION_EXTENSIONS = %w[yml yaml].freeze

included do
class_attribute :i18n_backend, instance_writer: false, instance_predicate: false
class_attribute :__vc_i18n_backend, instance_writer: false, instance_predicate: false
end

class_methods do
def i18n_scope
@i18n_scope ||= virtual_path.sub(%r{^/}, "").gsub(%r{/_?}, ".")
end

def build_i18n_backend
return if compiled?
def __vc_build_i18n_backend
return if __vc_compiled?

# We need to load the translations files from the ancestors so a component
# can inherit translations from its parent and is able to overwrite them.
Expand All @@ -33,7 +33,7 @@ def build_i18n_backend
end

# In development it will become nil if the translations file is removed
self.i18n_backend = if translation_files.any?
self.__vc_i18n_backend = if translation_files.any?
I18nBackend.new(
i18n_scope: i18n_scope,
load_paths: translation_files
Expand All @@ -52,12 +52,12 @@ def i18n_key(key, scope = nil)
def translate(key = nil, **options)
return key.map { |k| translate(k, **options) } if key.is_a?(Array)

ensure_compiled
__vc_ensure_compiled

locale = options.delete(:locale) || ::I18n.locale
key = i18n_key(key, options.delete(:scope))

i18n_backend.translate(locale, key, options)
__vc_i18n_backend.translate(locale, key, options)
end

alias_method :t, :translate
Expand Down Expand Up @@ -91,7 +91,7 @@ def store_translations(locale, data, options = EMPTY_HASH)
def translate(key = nil, **options)
raise ViewComponent::TranslateCalledBeforeRenderError if view_context.nil?

return super unless i18n_backend
return super unless __vc_i18n_backend
return key.map { |k| translate(k, **options) } if key.is_a?(Array)

locale = options.delete(:locale) || ::I18n.locale
Expand All @@ -103,7 +103,7 @@ def translate(key = nil, **options)
if key.start_with?(i18n_scope + ".")
translated =
catch(:exception) do
i18n_backend.translate(locale, key, options)
__vc_i18n_backend.translate(locale, key, options)
end

# Fallback to the global translations
Expand Down
Loading
Loading