Skip to content

Commit

Permalink
Add an option to only pick assets from app/assets/stylesheets using s…
Browse files Browse the repository at this point in the history
…tylesheet_link_tag :app
  • Loading branch information
brunoprietog committed May 19, 2024
1 parent 59406ab commit 8e6190a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
38 changes: 28 additions & 10 deletions lib/propshaft/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,40 @@ 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.
# Add an option to call `stylesheet_link_tag` with
# `:all` to include every css file found on the load path or
# `:app` to include every css file found on `app/assets/stylesheets`.
def stylesheet_link_tag(*sources, **options)
if sources.first == :all
case sources.first
when :all
super(*all_stylesheets_paths, **options)
when :app
super(*app_stylesheets_paths, **options)
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
private
def all_stylesheets_paths
stylesheets_paths_for(Rails.application.assets.load_path)
end

def app_stylesheets_paths
stylesheets_paths_for(
Rails.application.assets.load_path.dup.tap do |load_path|
load_path.paths = [ Rails.root.join("app/assets/stylesheets") ]
end
)
end

# Returns a sorted and unique array of logical paths for a stylesheets load path.
def stylesheets_paths_for(load_path)
load_path
.assets(content_types: [ Mime::EXTENSION_LOOKUP["css"] ])
.collect { |css| css.logical_path.to_s }
.sort
.uniq
end
end
end
5 changes: 5 additions & 0 deletions lib/propshaft/load_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def initialize(paths = [], compilers:, version: nil)
@paths, @compilers, @version = dedup(paths), compilers, version
end

def paths=(paths)
@paths = dedup(paths)
clear_cache
end

def find(asset_name)
assets_by_path[asset_name]
end
Expand Down
5 changes: 5 additions & 0 deletions test/dummy/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ApplicationHelper
def app_stylesheets?
params[:stylesheets] == "app"
end
end
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 :all, data: { custom_attribute: true } %>
<%= stylesheet_link_tag(app_stylesheets? ? :app : :all, data: { custom_attribute: true }) %>
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ class Application < Rails::Application
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
config.assets.paths << File.join(Rails.root, "vendor", "stylesheets")
end
end
Empty file.
10 changes: 10 additions & 0 deletions test/propshaft_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ class PropshaftIntegrationTest < ActionDispatch::IntegrationTest

assert_select 'link[href="/assets/hello_world-4137140a.css"][data-custom-attribute="true"]'
assert_select 'link[href="/assets/goodbye-b1dc9940.css"][data-custom-attribute="true"]'
assert_select 'link[href="/assets/library-356a192b.css"][data-custom-attribute="true"]'

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

test "using stylesheet_link_tag :app option should only resolve assets contained in app/assets/stylesheets" do
get sample_load_real_assets_url(stylesheets: :app)

assert_response :success
assert_select 'link[href="/assets/hello_world-4137140a.css"][data-custom-attribute="true"]'
assert_select 'link[href="/assets/goodbye-b1dc9940.css"][data-custom-attribute="true"]'
assert_select 'link[href="/assets/library-356a192b.css"][data-custom-attribute="true"]', 0
end

test "should raise an exception when resolving nonexistent assets" do
exception = assert_raises ActionView::Template::Error do
get sample_load_nonexistent_assets_url
Expand Down

0 comments on commit 8e6190a

Please sign in to comment.