Asset pipeline theme support

I have the following project structure:

/app /config /db ... /themes /default /assets /images /stylesheets /... /views /... /theme1 /assets /... /views /... 

'default' theme should be used by default :). 'theme1' - you should redefine any, for example, logo.png or view (application.erb).

It should work as follows: try to get the asset from "theme1", if it is missing, use "default".

Pretty simple with views:

 self.prepend_view_path ::ActionView::FileSystemResolver.new(theme_view_path_for(name)) self.prepend_view_path ::ActionView::FileSystemResolver.new(default_theme_path) 

But I cannot resolve this with assets.

 config.assets.paths << "#{Rails.root}/themes/default/assets/stylesheets" config.assets.paths << "#{Rails.root}/themes/default/assets/images" config.assets.paths << "#{Rails.root}/themes/default/assets/javascript" 

This adds assets as usual, for example, access to the resource can be obtained using the url 'assets / logo.png', but I need one prefix - "assets / theme1 / logo.png". Also, if "theme1" does not redefine logo.png with "assets / theme1 / logo.png", then the URL should be returned. (Similarly above with views).

I tried theme_for_rails - but it does not work, because they redefine the entire structure of the assets (own controller for maintenance, etc.).

In addition, it would be great to be able to connect to assets, when accessing logo1.png it should be possible to execute it with my controller and, for example, return it from the database.

Thanks for any advice, I will share the results at the end.

Kirill Salykin

+4
source share
1 answer

By default, folders on the first level will be ignored by an asterisk in the rails. You can check this in the default application, where you see that the compiled files skip the initial javascripts / images / or stylesheets / folders.

The best structure for you:

 /themes /stylesheets /default /theme1 /images /default /theme1 

Then add the directory like this:

 config.assets.paths << "#{Rails.root}/stylesheets" config.assets.paths << "#{Rails.root}/images" 

Want to link to files in a topic?

 = image_tag("#{theme_name}/background.png") = stylesheet_tag("#{theme_name}/application.css") 

That should work.

0
source

Source: https://habr.com/ru/post/1443602/


All Articles