Skip to content

Commit 1da654c

Browse files
authored
Expose .identifier (#2153)
1 parent beb25df commit 1da654c

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

docs/CHANGELOG.md

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

1111
## main
1212

13+
* Expose `.identifier` method as part of public API.
14+
15+
*Joel Hawksley*
16+
1317
## 3.20.0
1418

1519
* Allow rendering `with_collection` to accept an optional `spacer_component` to be rendered between each item.

lib/view_component/base.rb

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,16 @@ def compiler
457457
# Defaults to `false`.
458458

459459
class << self
460+
# The file path of the component Ruby file.
461+
#
462+
# @return [String]
463+
attr_reader :identifier
464+
465+
# @private
466+
attr_writer :identifier
467+
460468
# @private
461-
attr_accessor :source_location, :virtual_path
469+
attr_accessor :virtual_path
462470

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

473481
extensions = extensions.join(",")
474482

475483
# view files in a directory named like the component
476-
directory = File.dirname(source_location)
477-
filename = File.basename(source_location, ".rb")
484+
directory = File.dirname(identifier)
485+
filename = File.basename(identifier, ".rb")
478486
component_name = name.demodulize.underscore
479487

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

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

502-
(sidecar_files - [source_location] + sidecar_directory_files + nested_component_files).uniq
510+
(sidecar_files - [identifier] + sidecar_directory_files + nested_component_files).uniq
503511
end
504512

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

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

593-
# @private
594-
def identifier
595-
# :nocov:
596-
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.")
597-
598-
source_location
599-
# :nocov:
600-
end
601-
602601
# Set the parameter name used when rendering elements of a collection ([documentation](/guide/collections)):
603602
#
604603
# ```ruby

lib/view_component/instrumentation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def render_in(view_context, &block)
1313
notification_name,
1414
{
1515
name: self.class.name,
16-
identifier: self.class.source_location
16+
identifier: self.class.identifier
1717
}
1818
) do
1919
super

test/sandbox/test/base_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
require "test_helper"
44

55
class ViewComponent::Base::UnitTest < Minitest::Test
6+
def test_identifier
7+
assert(MyComponent.identifier.include?("test/sandbox/app/components/my_component.rb"))
8+
end
9+
610
def skip_templates_parses_all_types_of_paths
711
file_path = [
812
"/Users/fake.user/path/to.templates/component/test_component.html+phone.erb",

0 commit comments

Comments
 (0)