Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how best to organize assets in a rails app

As I continue to make progress on my app my stylesheets folder has quickly become overrun. I've read the rails guide on the asset pipeline, and scoured the internet for a reputable source and am still at a loss on the "rails way" to keep everything tidy.

Before I go and try and develop a system by myself I was hoping to get some suggestions from you guys.

Is there a well known "best practice" when it comes to asset organization? If not, what has worked well for you?

As of now my setup is this:

app/assets/stylesheets >

application.css, application-RESPONSIVE.css

any code specific to the body, containers, navbar, footer

controller.scss, controller-RESPONSIVE

*any code specific to that controller

lib/assets/stylesheets >

reset.css

in application.css i use

*= require reset
*= require_tree .
*= require_self

To get all the styles however I've also been wondering if its better to set self before tree as a means of overriding the main styles if need be?

like image 628
thesowismine Avatar asked Oct 19 '22 07:10

thesowismine


1 Answers

Is there a well known "best practice" when it comes to asset organization?

Organization of assets:

In Rails, pipeline assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.

1) app/assets is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.

2) lib/assets is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.

3) vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.

Is it better to set self before tree as a means of overriding the main styles if need be?

application.css is what is called a manifest file. Now, it wouldn't particularly make any difference by switching self with tree, but it would change it's position in the loading sequence. Consider this...

Once you’ve placed your assets in their logical locations, you can use manifest files to tell Rails (via the Sprockets gem) how to combine them to form single files. These manifest files contain directives - instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file.

Additional info:

One other thing that Rails does, is generate stylesheets for you when you run the rails generate command in the terminal. On creation of a new controller, you'll see a stylesheet with the controller's name in your assets. It'll look something like app/assets/stylesheets/newcontroller.css.scss

Also, what steve klein said.

Hope this helps you out.

UPDATE:

I've tried looking for examples on github but all of the apps I have found have been too simple and havent shown any advanced organization for a large amount of files.

I encourage you to check out Open Source Rails website. I have looked at some projects there and they seem complex enough for you to be interested enough. Especially, look at the Discourse app. Discourse was co-founded by Jeff Atwood, co-founder of Stack Overflow. That I suppose speaks for itself ;)

like image 89
Timur Mamedov Avatar answered Oct 22 '22 02:10

Timur Mamedov