Enforcing one-to-one matching of sass and css files - ruby-on-rails

I'm using Jammit to package assets and Sass to generate stylesheets in a Rails 3 app. There are around 35 stylesheets for different site components and all are individually listed in our Jammit config. I would like to get to something based more on convention, ie:
stylesheets:
common:
- public/stylesheets/application.css
- public/stylesheets/components/*.css
- public/stylesheets/pages/*.css
The only hurdle I am running into is that I can't find a way to enforce a one-to-one mapping between sass files and css files. The above approach would remove the need to mess with the Jammit config and would automatically add new css files to the site, but if I were to remove a sass file the CSS file would still exist and would still be in our common package. CSS files are ignored in the working tree. Any way to tell Sass to remove CSS files with no template or do I have to add our CSS files to the repo? Is there another option?

Since the CSS files are automatically generated, you can safely just delete the whole directory if you ever want to clean it, and then have Sass regenerate them.

Related

How to compile your CSS/JS and keep sprockets from concatenating the files?

So we have an app made with rails that we are we are deploying to Heroku. We have been having very odd CSS, JS issues on the heroku build of our app. The CSS and JS are being put into one file when compiled. Is there any way to keep sprockets from concatenating my CSS and maybe even JS files, but still compile? Thanks
Sprockets concatenates all JavaScript files into one master .js file
and all CSS files into one master .css file.
Yes, you can individually compile your assets by
1) Adding each individual file to you the Rails.application.config.assets.precompile array
2) Referencing each JS/CSS file with a javascript_include_tag or stylesheet_link_tag
3) Removing the sprocket references in you application.js & application.css files
Ultimately, you'll be going against best practice of providing a single, larger, JS/CSS files (slower load time), and this will be more difficult to maintain.
Alternatively, I'd suggest you sort out the issues you mentioned by correcting your load order within your application.js/css files such that they can be concatenated correctly without errors.

What is the best way of organizing and implementing page specific CSS and JavaScript in a Ruby on Rails application?

Question 1:
I've heard of creating div class' to cater to certain areas of the HTML but is there another way out there? Here's a link that I found related to what I mean.
http://brandonhilkert.com/blog/page-specific-javascript-in-rails/
Question 2:
Is there a way in order for me to organize my CSS and JavaScripts in my asset folder in the rails app IN SPECIFIC FOLDERS while accessing the asset pipeline benefits?
Eg.
Normal way:
assets
images(under assets)
stylesheets(under assets)
mycss1 CSS scss
mycss2 CSS scss
mycss3 CSS scss
javascripts(under assets)
myjava1 js
myjava2 js
myjava custom js
The Idea:
assets
images(under assets)
stylesheets(under assets)
myview(this is a subfolder of the stylesheets folder)
mycss1 CSS scss
mycss2 CSS scss
homepage(subfolder)
mycss3 CSS scss
javascripts(under assets)
myview(subfolder of javascripts)
myjava1 js
homepage(subfolder)
myjava2 js
myjava2 custom js
As far as I understand, you want to add your custom folders under the assets directory, put some files with specific CSS and JS and use them whenever you need it.
To achieve this you should include those files somewhere. By default, you have an appplication layout which includes application.js and application.css files which includes the rest of the related files. Those files known as manifest files and they are using sprockets gem to handle dependencies, preprocessing, compressing and other stuff (you can read more about it here). You can change the assets folder using config.assets.prefix or add new paths to look up for sprockets using config.assets.paths (This might be what you are looking for. Further reading here)

Install custom theme with twitter-bootstrap-rails

How do I install a custom theme for twitter-bootstrap with the rails asset pipeline?
Should I create a new folder under assets and dump in all of the css, js, image and font files for the theme?
Update:
I put all files in my downloaded theme (except the html example files) into a folder in app/assets and added this folder to my asset paths in application.rb:
config.assets.paths << Rails.root.join("app", "assets", "bootstrap_theme")
I added some markup using css classes from the theme but it's not using the theme..
Update: I see that the boostrap theme has the compiled core bootstrap files included inside it. Should I take this to mean it's not intended to be used with Less?
Also, should I precompile every asset file individually for production (in application.rb) and in each view include the specific ones needed (and include them after the core bootstrap files)? Then I guess to make any overrides to the theme they'd need to be made directly in the theme files?
Update: I think the problem was that I was requiring the theme css files in my manifest after the bootstrap_and_overrides, assuming the idea of the theme was to change the bootstrap defaults. But requiring some of the theme css before the standard bootstrap_and_defaults works better. It means needing to manually pick and choose which css to load before and after bootstrap.
I never proved the twitter bootstrap in a rails application but I searched before how can I do it.
If you install the gem less-rails-bootstrap or bootstrap-sass, you can follow the instructions to modify the theme here.
I hope it helps.

Sass - can it be compiled at runtime?

I've seen that certain Rails CMSes (like Radiant) have plugins that essentially compile Sass when a page is accessed. Is there a way to do this in a regular rails app? Is doing so performant? Basically, I'm looking at a way to remove the extra step of running Compass to compile my stylesheets.
I've not used compass specifically but there looks like there's a production flag so files are compiled - I couldn't imagine they'd build it to recompile per request in production, Radiant compiles it's css on Application startup and if you then commit those generated CSS files it doesn't try to generate them again AFAIK.
http://compass-style.org/docs/tutorials/production-css/
Sass and Compass automatically integrate with Rails. If you're using Rails 3, all you have to do is add gem "haml" to your Gemfile and all .sass and .scss files in public/stylesheets/sass will get compiled to .css files in public/stylesheets.
Compile per request? I think it could be a hit for performance. You should definitely use a caching strategy in that case. So that it compiles the stylesheet only if it is not in the cache.
You could create a helper method setup_stylesheet that will take care of setting up the css stylesheet. You call this method on the application layout.
setup_stylesheet will check if the css stylesheet is on the cache, and if it is there then use it. If it is not, then compile it.
Another approach:
You could set up an initialiser that will call Compass to compile your SASS stylesheets when the App is launched.
Is doing so performant?
There will be a massive performance hit when compiling at run-time.
As Nex3 (author of Sass gem) pointed out on another forum, there's no need any need to run compass watch.
I strongly advise putting the following into production.rb: Sass::Plugin.options[:never_update] = true - this is especially important if you're on Heroku. (you could also do this in your rack file, where you can also specify other options
Hmm, good luck

What is the best method for storing SASS generated CSS in your application and source control?

If you are using HAML and SASS in your Rails application, then any templates you define in public/stylesheet/*.sass will be compiled into *.css stylesheets. From your code, you use stylesheet_link_tag to pull in the asset by name without having to worry about the extension.
Many people dislike storing generated code or compiled code in version control, and it also stands to reason that the public/ directory shouldn't contain elements that you don't send to the browser.
What is the best pattern to follow when laying out SASS resources in your Rails project?
The compass framework recommends putting your sass stylesheets under app/stylesheets and your compiled css in public/stylesheets/compiled.
You can configure this by adding the following code to your environment.rb:
Sass::Plugin.options[:template_location] = {
"#{RAILS_ROOT}/app/stylesheets" => "#{RAILS_ROOT}/public/stylesheets/compiled"
}
If you use the compass framework, it sets up this configuration for you when you install it.
I always version all stylesheets in "public/stylesheets/sass/*.sass" and set up an exclude filter for compiled ones:
/public/stylesheets/*.css
Honestly, I like having my compiled SASS stylesheets in version control. They're small, only change when your .sass files change, and having them deploy with the rest of your app means the SASS compiler doesn't ever need to fire in production.
The other advantage (albeit a small one) is that if you're not using page caching, your rails process doesn't need to have write access to your public_html directory. So there's one fewer way an exploit of your server can be evil.
Somewhat related, but it's a good idea to regenerate your CSS during your capistrano deployments. This callback hook does just that:
after "deploy:update_code" do
rails_env = fetch(:rails_env, "production")
run "#{release_path}/script/runner -e #{rails_env} 'Sass::Plugin.update_stylesheets'"
end
Update: This should no longer be necessary with modern versions of Haml/Sass.
If I can manage it, I like to store all of my styles in SASS templates when I choose HAML/SASS for a project, and I'll remove application.css and scaffold.css. Then I will put SASS in public/stylesheets/sass, and add /public/stylesheets/*.css to .gitignore.
If I have to work with a combination of SASS and CSS based assets, it's a little more complicated. The simplest way of handling this is to have an output subdirectory for generated CSS within the stylesheets directory, then exclude that subdirectory in .gitignore. Then, in your views you have to know which styling type you're using (SASS or CSS) by virtue of having to select the public/stylesheets/foo stylesheet or the public/stylesheets/sass-out/foo stylesheet.
If you have to go the second route, build a helper to abstract away the sass-out subdirectory.

Resources