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

Allow sidecar CSS files #57

Merged
merged 3 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions lib/action_view/component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ def template_file_path

filename = self.instance_method(:initialize).source_location[0]
filename_without_extension = filename[0..-(File.extname(filename).length + 1)]
sibling_files = Dir["#{filename_without_extension}.*"] - [filename]
sibling_template_files = Dir["#{filename_without_extension}.????.{#{ActionView::Template.template_handler_extensions.join(",")}}"] - [filename]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on using single quotes within the double quotes to make clear that you aren't stopping the outer double quotes?

Also, TIL ????

Suggested change
sibling_template_files = Dir["#{filename_without_extension}.????.{#{ActionView::Template.template_handler_extensions.join(",")}}"] - [filename]
sibling_template_files = Dir["#{filename_without_extension}.????.{#{ActionView::Template.template_handler_extensions.join(',')}}"] - [filename]


if sibling_files.length > 1
if sibling_template_files.length > 1
raise StandardError.new("More than one template found for #{self}. There can only be one sidecar template file per component.")
end

if sibling_files.length == 0
if sibling_template_files.length == 0
raise NotImplementedError.new(
"Could not find a template file for #{self}."
)
end

sibling_files[0]
sibling_template_files[0]
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/action_view/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def test_renders_another_component
assert_equal trim_result(result.css("div").first.to_html), "<div>hello,world!</div>"
end

def test_renders_component_with_css_sidecar
result = render_inline(CssSidecarFileComponent)

assert_equal trim_result(result.css("div").first.to_html), "<div>hello,world!</div>"
end

def test_template_changes_are_not_reflected_in_production
ActionView::Base.cache_template_loading = true

Expand Down
3 changes: 3 additions & 0 deletions test/app/components/css_sidecar_file_component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div {
font-weight: bold;
}
1 change: 1 addition & 0 deletions test/app/components/css_sidecar_file_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>hello, world!</div>
6 changes: 6 additions & 0 deletions test/app/components/css_sidecar_file_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class CssSidecarFileComponent < ActionView::Component::Base
def initialize(*)
end
end