Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating list or paths of stylesheets and javascript files in Rails 4

I'm writing a Rails4 app that uses a custom cache manifest file which needs to include references to all the required Javascript and CSS files. Due to the nature of the application, the Rack Offline gem can't be used.

The stylesheet_link_tag and javascript_include_tag calls produce the correct list of files (as generated by the asset pipeline) but embed them in HTML tags.

Is there a way to get the paths to all the compiled javascript and stylesheet files in the controller?

eg.

/assets/custom.css?body=1
/assets/incidents.css?body=1
/assets/users.css?body=1
/assets/application.css?body=
/assets/jquery.js?body=1
/assets/bootstrap/affix.js?body=1
...
like image 246
MZB Avatar asked Aug 26 '14 20:08

MZB


1 Answers

That one was fun! Had to go into the Sprockets source to figure it out.

asset_list = Rails.application.assets.each_logical_path(*Rails.application.config.assets).to_a

You can then go in a grep through the asset list, something like:

asset_list.grep(/\.(js|css)/)

EDIT:

If you want the hex digests, you could do something like:

environment = Rails.application.assets
asset_list = environment.each_logical_path(*Rails.application.config.assets).to_a
asset_list.map! { |asset| environment.find_asset(asset).digest_path rescue nil }.compact
like image 186
kddeisz Avatar answered Nov 06 '22 03:11

kddeisz