rails-assets icon indicating copy to clipboard operation
rails-assets copied to clipboard

Cannot remove asset dependency path in Rails 5

Open smarquez1 opened this issue 8 years ago • 2 comments

Hi, I created this issue since https://github.com/rails-assets/rails-assets/issues/132 is locked. I have the exact same issue of the link above but the mentioned workaround does not appear to be working on Rails 5 (beta 3). The assets path is empty while trying to access from an initializer. Do you know any workaround for this?

Rails.configuration.assets.paths.reject! do |path|
  path.include?('rails-assets-bootstrap')
end

smarquez1 avatar Mar 05 '16 16:03 smarquez1

For anyone searching here's the thing:

In your Rails 5 initializer you don't have all the paths populated yet, so we want to do this rejection after the initialization is complete.

Rails.application.config.after_initialize do
  Rails.application.config.assets.paths.reject! do |path|
      path.include? 'rails-assets-bootstrap'
  end
end

However this is a little simplistic, as noted in another issue (#282) this will reject rails-assets-bootstrap and rails-assets-bootstrap-datepicker etc.

Here's an example that lets you set an array of gem names to reject, it includes the gem version in the final check to safely reject specific gems only:

Rails.application.config.after_initialize do
  # add the gem names you wish to reject to the below array
  excluded_gem_names = ['rails-assets-bootstrap']

  excluded_gem_full_names = Gem::Specification.select { |g| excluded_gem_names.include? g.name }.flat_map { |a| a.full_name }
  Rails.application.config.assets.paths.reject! do |path|
    excluded_gem_full_names.any? { |gem_name| path.include? gem_name }
  end
end

rjocoleman avatar Apr 14 '17 09:04 rjocoleman

I couldn't get the above to work in Rails 5.2, but using the to_prepare hook instead of after_initialize fixed it for me:

Rails.application.config.to_prepare do
  Rails.configuration.assets.paths.reject! do |path|
    path =~ /rails-assets-bootstrap-[0-9]/
  end
end

fbacall avatar Jul 05 '19 13:07 fbacall