Skip to content

Commit

Permalink
Merge pull request #57 from github/55/sidecar-css
Browse files Browse the repository at this point in the history
Allow sidecar CSS files
  • Loading branch information
joelhawksley authored Oct 2, 2019
2 parents b66fa08 + 8990b8d commit 507c800
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
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]

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

0 comments on commit 507c800

Please sign in to comment.