Skip to content

Commit

Permalink
Add helper to include all stylesheets in the load path
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Sep 24, 2023
1 parent 96ddc84 commit fe07120
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
18 changes: 18 additions & 0 deletions lib/propshaft/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,23 @@ module Helper
def compute_asset_path(path, options = {})
Rails.application.assets.resolver.resolve(path) || raise(MissingAssetError.new(path))
end

# Add an option to call `stylesheet_link_tag` with `:all` to include every css file found on the load path.
def stylesheet_link_tag(*sources)
if sources.first == :all
super *all_stylesheets_paths
else
super
end
end

# Returns a sorted and unique array of logical paths for all stylesheets in the load path.
def all_stylesheets_paths
Rails.application.assets.load_path
.assets(content_types: [ Mime::EXTENSION_LOOKUP["css"] ])
.collect { |css| css.logical_path.to_s }
.sort
.uniq
end
end
end
15 changes: 0 additions & 15 deletions test/dummy/app/assets/stylesheets/application.css

This file was deleted.

3 changes: 3 additions & 0 deletions test/dummy/app/assets/stylesheets/goodbye.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h2:after {
content: "Goodbye!";
}
2 changes: 1 addition & 1 deletion test/dummy/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application" %>
<%= stylesheet_link_tag :all %>
</head>

<body>
Expand Down
4 changes: 4 additions & 0 deletions test/propshaft_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
class PropshaftIntegrationTest < ActionDispatch::IntegrationTest
test "should be able to resolve real assets" do
get sample_load_real_assets_url

assert_response :success

assert_select 'link[href="/assets/hello_world-4137140a1298c3924d5f7135617c23e23fb167a8.css"]'
assert_select 'link[href="/assets/goodbye-b1dc9940e9800d8bc96f7434617c043e58277419.css"]'

assert_select 'script[src="/assets/hello_world-888761f849ba63a95a56f6ef898a9eb70ca4c46e.js"]'
end

Expand Down

0 comments on commit fe07120

Please sign in to comment.