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

Add helper to include all stylesheets in the load path #154

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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