Skip to content

Commit

Permalink
Expose .identifier (#2153)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhawksley authored Nov 1, 2024
1 parent beb25df commit 1da654c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Expose `.identifier` method as part of public API.

*Joel Hawksley*

## 3.20.0

* Allow rendering `with_collection` to accept an optional `spacer_component` to be rendered between each item.
Expand Down
31 changes: 15 additions & 16 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,16 @@ def compiler
# Defaults to `false`.

class << self
# The file path of the component Ruby file.
#
# @return [String]
attr_reader :identifier

# @private
attr_writer :identifier

# @private
attr_accessor :source_location, :virtual_path
attr_accessor :virtual_path

# Find sidecar files for the given extensions.
#
Expand All @@ -468,13 +476,13 @@ class << self
# For example, one might collect sidecar CSS files that need to be compiled.
# @param extensions [Array<String>] Extensions of which to return matching sidecar files.
def sidecar_files(extensions)
return [] unless source_location
return [] unless identifier

extensions = extensions.join(",")

# view files in a directory named like the component
directory = File.dirname(source_location)
filename = File.basename(source_location, ".rb")
directory = File.dirname(identifier)
filename = File.basename(identifier, ".rb")
component_name = name.demodulize.underscore

# Add support for nested components defined in the same file.
Expand All @@ -499,7 +507,7 @@ def sidecar_files(extensions)

sidecar_directory_files = Dir["#{directory}/#{component_name}/#{filename}.*{#{extensions}}"]

(sidecar_files - [source_location] + sidecar_directory_files + nested_component_files).uniq
(sidecar_files - [identifier] + sidecar_directory_files + nested_component_files).uniq
end

# Render a component for each element in a collection ([documentation](/guide/collections)):
Expand Down Expand Up @@ -548,11 +556,11 @@ def render_template_for(variant = nil, format = nil)
# has been re-defined by the consuming application, likely in ApplicationComponent.
# We use `base_label` method here instead of `label` to avoid cases where the method
# owner is included in a prefix like `ApplicationComponent.inherited`.
child.source_location = caller_locations(1, 10).reject { |l| l.base_label == "inherited" }[0].path
child.identifier = caller_locations(1, 10).reject { |l| l.base_label == "inherited" }[0].path

# If Rails application is loaded, removes the first part of the path and the extension.
if defined?(Rails) && Rails.application
child.virtual_path = child.source_location.gsub(
child.virtual_path = child.identifier.gsub(
/(.*#{Regexp.quote(ViewComponent::Base.config.view_component_path)})|(\.rb)/, ""
)
end
Expand Down Expand Up @@ -590,15 +598,6 @@ def compiler
@__vc_compiler ||= Compiler.new(self)
end

# @private
def identifier
# :nocov:
Kernel.warn("WARNING: The #{self.class}.identifier is undocumented and was meant for internal framework usage only. As it is no longer used by the framework it will be removed in a coming non-breaking ViewComponent release.")

source_location
# :nocov:
end

# Set the parameter name used when rendering elements of a collection ([documentation](/guide/collections)):
#
# ```ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def render_in(view_context, &block)
notification_name,
{
name: self.class.name,
identifier: self.class.source_location
identifier: self.class.identifier
}
) do
super
Expand Down
4 changes: 4 additions & 0 deletions test/sandbox/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
require "test_helper"

class ViewComponent::Base::UnitTest < Minitest::Test
def test_identifier
assert(MyComponent.identifier.include?("test/sandbox/app/components/my_component.rb"))
end

def skip_templates_parses_all_types_of_paths
file_path = [
"/Users/fake.user/path/to.templates/component/test_component.html+phone.erb",
Expand Down

0 comments on commit 1da654c

Please sign in to comment.