Jasminerice and stylesheets for fixtures - ruby-on-rails-3.1

I have found jasminerice very helpful for getting all my Jasmine tests to run via Rails 3.1's asset pipeline. The only thing I remain stuck with, is that I cannot get my setup to load any stylesheets (that go with my fixtures) and I need them for a couple of dom / element-style specific tests. Does anybody know how to get the stylesheets to be loaded in this setup?

Support for CSS files has been added recently, the README states:
For including stylesheets in your specs, jasminerice uses a spec.css file. Create such a file next to the spec.js.coffee file:
spec/javascripts/spec.css
and in that file, use sprockets directives to include the right css files, e.g.
/*
*= require application
*/
The change is fairly new so you may want to include the gem directly from the latest github version:
# Gemfile
gem 'jasminerice', git: 'git://github.com/bradphelan/jasminerice.git'
You may also want to be precise with your css markup, so as to not mess up Jasmine's spec runner page as the css files (as the js files) are included directly into it.

I have updated the Jasminerice gem and bradphelan (the Jasminerice author) has pulled that change into the source on Github. So in order to use use stylesheets in your Jasmine tests running through Jasminerice, simply refer to the gem on Github in your Gemfile like so: gem "jasminerice", :git => 'git://github.com/bradphelan/jasminerice.git'. The documentation has also been updated on Github.

Related

How to use LiveReload without rails, to serve html in the browser

I am working on the css and html of a simple web-project.
I would like to use livereload to update what gets rendered in the browser on the fly whenever I make a change to any of the files in my project.
From the description on the livereload website:
What does LiveReload do?
LiveReload monitors changes in the file system. As soon as you save a
file, it is preprocessed as needed, and the browser is refreshed.
Even cooler, when you change a CSS file or an image, the browser is
updated instantly without reloading the page.
Since this is not a rails/sinatra project, but just simple html/css/js, what could I use to get live reloading?
E.g. is there some lightweight server in the gem repos that could solve this for me?
You could use the catapult gem to set up a simple sprockets app, without sinatra or rails. It is just a static site that makes it easy to use a sort of asset pipeline. I have used it a lot and I love it.
Then in your Gemfile you can add gem 'guard-livereload' project here.
Just follow the setup instructions, you may also have to check out guard to get everything working nicely together.
I only am suggesting catapult because it makes setting up a simple static site incredibly easy with the added benefit of sprockets, coffeescript and your flavor of sass.
To do this without catapult, just run bundle init (assuming you have the bundler gem installed) in your root directory which will create a Gemfile. Then all you need to do is add the guard-livereload gem like I wrote above.
enjoy

SASS and Jekyll integration

I'm just wondering if there is a way to integrate SASS into Jekyll. All that I need is an automatic .scss compilation into .css when I launch Jekyll.
I don't see anything wrong with compiling assets when you launch (which I take to mean run) jekyll. The whole point of running jekyll is to pre-compile your site, which is good for performance.
As for asset conversion-- there are many plugins available that focus on this. I like the Jekyll Asset Pipeline gem, which supports any language (e.g. Scss, Less, CoffeeScript, Erb, etc.) and has a bunch of features (e.g. asset tagging, compression, gzipping, etc.) that set it apart. It also seems to be the fastest growing Jekyll-related gem these days, which I take to mean that it is gaining traction in the community.
If you want to keep it as simple as compass watch you can use the Guard gem along with guard-jekyll and guard-compass (and if you like style injection, guard-livereload).
Guard bundles multiple 'watch' actions under a single terminal window, and is much easier to set up than a robust asset pipeline. Install the gems, configure the .guardfile according to the guard-compass and guard-jekyll instructions, cd to your directory and type guard. Any time a relevant file changes your sass files and/or jekyll site will be recompiled.
Native Sass, and CoffeeScript, processing debuted in Jekyll v2.0:
http://jekyllrb.com/docs/assets/#sassscss
Full disclosure: I am the lead dev behind this project.
The easiest way I've seen to setup Sass with Jekyll is with jekyll-compass. This gem will do exactly as you describe: Any time jekyll builds your website (jekyll build, jekyll serve, etc) your Sass will be compiled into the output folder along with the rest of your website. Check out the readme linked above for full usage details.
There's also some work under way currently by the Jekyll guys to get Sass support into the core of Jekyll so that everyone will have at least basic access to Sass and the wonderful feature-set it provides.

How can I completely disable CoffeeScript in a Rails 3.1 app?

At the moment when I generate a new controller, Rails also generates a .js.coffee file for the controller as well. As I don't use CoffeeScript I want Rails instead generate .js files for me.
Is it enough to comment out the coffee-rails gem to completely disable CofeeScript in a Rails 3.1 app?
Comment out gem "coffee-script" in your Gemfile
Use .js instead of .js.coffee for your javascript files
Not sure if this counts for Rails 3.1 but in 4 you should also set the javascript_engine to :js in application.rb to instruct generators to create .js files instead of .js.coffee.
config.generators do |g|
# .. other configuration ..
g.javascript_engine :js
end
Koen and Gaurav Gupta have good answers!
If you want to make these changes automatically for every new Rails project, you can use a template file.
In ~/rails-template.rb
# Don't install coffeescript
gsub_file 'Gemfile', /^gem \'coffee-rails\'/ do
"\# gem 'coffee-rails'"
end
# Mess with generators to get the behavior we expect around new files
# For these injections, indentation matters!
inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do
<<-'RUBY'
config.generators do |g|
# Always use .js files, never .coffee
g.javascript_engine :js
end
RUBY
end
Then in ~/.railsrc
-m ~/.rails-template.rb
Now whenever you run rails new, the coffeescript gem will be commented out, and new controllers will use .js instead of .coffee.
Tested on Rails 5.0.4, but I believe it should work for earlier versions as well.
As an aside, Rails templates, and generators in general, are super powerful. I'm a teacher and my students will typically create 15 to 20 rails projects through the course, and providing them with a good template file with debugging gems, spec style testing, etc. is a huge timesaver. After they've made the changes once themselves, of course. If you're interested, my personal .rails-template.rb is on GitHub.
Note for Rails 4, or if you're using 'turbolinks', 'uglifier', or any other kind of gem that requires the server to interpret javascript, comment them out as well.
I had this problem, as I am using codekit to compile my coffeescript.
I got around it by renaming my 'assets/coffee' folder to 'assets/cafe', so rail wouldn't find it.
Edit: What does work (and the ONLY thing that works for me, the above answer does not work) is to add a separate folder 'App/Coffee', and setting it to be compiled into the assets/javascript folder. If it's in the assets directory, rails will find it no matter the name.

rails 3.1 some assets not precompiling on production

I was running into issues with Heroku showing my 'places.js' was not precompiled, even though im running on the cedar stack, and during the slug compilation it is running the rake precompile task. so i tried running it locally with rake assets:precompile RAILS_ENV=production and indeed rails was not precompiling my /app/assets/javascripts/places.js.coffee.erb asset.
my production.rb is using default rails 3.1 configuration, and i even tried removing the .erb from the asset, but to no avail.
I also thought since my places.js.coffee.erb asset is NOT included in the sprockets manifest (i include it manually in my app), perhaps it only precompiles assets in the manifest. Requiring it in the manifest didn't work either.
only my application.js.coffee and `application.css are precompiling (with and without a digest).
the only issue i found was possibly poor regex being used to match assets, but the default of (?:\/|\\|\A)application\.(css|js)$ does not match my asset, so it should be included.
i am not sure how to troubleshoot from here. everything is pretty much default. any thoughts on what could be happening here?
Firstly, if you want a file to compile when it is not in a manifest, you need to add it to the precompile config option:
config.assets.precompile += ['places.js']
Secondly, can you edit your question to include your manifest - it may be a syntax issue. I will edit this answer if I can see what the issue might be.
I had the same issue and resolved it like this:
# add new file /app/assets/javascripts/places_manifest.js
//= require places
# add a line to config/application.rb
config.assets.precompile += ['places_manifest.js']
# in your views include places_manifest, not places
javascript_include_tag 'places_manifest'
While the above solutions seem fine, I wondered why do I have to do this?
Like everyone else, I got the error in production stating that my newly added javascript file was not pre-compiled. It was however, I found it's code in the minified application.css file that Rails had generated on my production server.
The problem was that while developing, I thought I would need to add a javascript_include_tag helper to load my new javascript file. Adding that helper was the source of my particular error. I just removed it, and everything worked fine in both development and production environments.
So my suggestion to you is look for signs of your new .js file in your minified application.js and don't modify any other files as the above solutions suggest. Please point out the error in my ways if necessary ;)

How to automatically compile scss into css with assets pipeline in Rails 3.1?

The new rails 3.1 asset pipeline confused me a lot. In rails 3.0.x, with the sass gem, my global css file got updated when I edit .scss files. But in rails 3.1, that doesn't work anymore. It seems like I have to run a rake task to update my css files whenever I modify the .scss files. I feel like I misunderstand something about the new asset pipeline, but I don't know what it is. Can someone provide a solution or explanation to this problem? Thank you.
There are two possible causes of this. I am assuming that you are in development mode, and that all the gems are loaded.
1. Config
In development mode files are compiled on-demand and cached until any changes are made. You have to get the new 3.1 config options set in the right files or this might not work as expected.
Check out the Rails guides section on upgrading.
2. Extensions
The other is getting the extensions in the right order. For scss that would be file.css.scss. This tells Sprockets to parse the files as scss first, and that the have a css extension. If you had .erb on the end - file.css.scss.erb - then erb is processed first, then scss.
Upgrading apps is a bit tricky because so many things have changed. The asset pipeline guide has lots of useful information and advice.
Sass / SCSS has this built in already so you don't have to do ERB pre-processing.
image-url("image.png")
http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets
For me this problem resolved very easy.
I simple deleted all precompiled *.css files from assets/stylesheets and remain all *.scss files. Rails worked fine with *.scss directly, withoutn precompile.

Resources