emersonthis
emersonthis

Reputation: 33408

Can you configure Rails to precompile ALL files under a directory

Yes, there are a million questions asking how to register a directory for the assets pipeline. But my question is slightly different...

I'm building a basic theme system and the file structure will look like this:

app/assets/skins/
             some_theme/
                 style.css.scss
                 fonts/
                    font1.woff
                    font2.woff
                 images/
                     whocares.png
             another_theme/
                 ...

As you can see, the theme-specific assets are bundled in their own directories. When I new theme gets added, I don't want it to require any tinkering with the configurations.

Can I configure the asset pipeline to search/precompile ALL the files under app/assets/skins/?

I think I want something like this in application.rb...

config.assets.paths << Rails.root.join('app', 'assets', 'skins', '**')

...but this isn't how it works. How does it work?

Upvotes: 0

Views: 1342

Answers (1)

Mat
Mat

Reputation: 2184

To include all your scripts: add to your app/assets/javascripts/application.js:

//= require_tree ../skins

To include all your styles: add to your app/assets/stylesheets/application.css:

*= require_tree ../skins

Or the equivalent syntax for sass (better than using comments):

@import '../skins/**/*'

To include all other assets add to config/initializers/assets.rb:

Dir.glob( Rails.root.join( 'app', 'assets', 'skins', '**' ) ).each do |path|
  Rails.application.config.assets.paths << path
end

And to access for example app/assets/skins/a_theme/skin.png you can use view helpers like:

<%= image_tag 'skin.png' %>

And in your sass files helpers like:

background-image: image-url('skin.png')

The same for fonts assets.

UPDATE: just to clear a point: with Dir.glob if 2 images have the same name in different paths only the first in paths list will be used

Upvotes: 2

Related Questions