Can't find Javascript assets in Jasmine specs in Middleman - jasmine

We're wanting to use middleman-jasmine to run some tests on the generated sites: e.g.
does the popup contact form appear
injecting a GeoIP location does it show the nearest office
We've managed to set up Jasmine and it runs a basic trivial test, but I can't see how to require our source JS libs.
we have this structure:
source
js
thing.js
spec
js
spec.js
thing_spec.js
spec.js looks like this:
//= require_tree .
//= require thing
But it can't find thing. I've tried every combination of:
//= require ../../source/js/thing
//= require ../../build/js/thing
//= require js/thing
//= require js/thing.js
None of which work.

Okay, discovered the problem. It turns out we had the wrong path in config.rb:
set :js_dir, 'js'
We incorrectly had 'javascripts' instead of 'js'.
Strangely, it didn't affect anything in the rest of the middleman build, but did stop sprockets from finding the asset paths for jasmine.

Related

livereload only responding to css changes, not html

I have installed livereload gem, which I'm using with a ruby middleman application. However, it's only autoloading changes to the css. If I make changes to the html files, I still have to restart the server to view the changes. Is there somehow I can make it autoload html changes?
It seems that this feature doesn't work on all versions of livereload and also depends on the browser and it's extensions. Try to also change a css file when you change the html so that the refresh occurs. Also, have you looked at https://github.com/guard/guard-livereload ? That has an example watching the change of html.
guard 'livereload' do
watch(%r{app/views/.+\.(erb|haml|slim)})
watch(%r{app/helpers/.+\.rb})
watch(%r{public/.+\.(css|js|html)})
watch(%r{config/locales/.+\.yml})
# Rails Assets Pipeline
watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html))).*}) { |m| "/assets/#{m[3]}" }
end

Rails 3.1 assets pipeline, how does it handle files with the same name? priority?

This is problem, I'm using Typus, which includes a fancybox jquery plugin under /vendor/assets. That fancybox version is too old (1.3.4) and it does not work with rails 1.9.x so I need to replace that with fancybox 2.x that works with that version of jquery because the rest of my site uses jquery 1.9.
Typus's application.js manifest loads fancybox this way
//= require jquery.fancybox-1.3.4.pack
so I thought I could just add fancybox 2 with that file name and the asset pipeline would use that version instead but it's not, it's loading the file inside /vendor/assets.
So, my question is, how does rails handle that case when there's more than one file with the same name? is there any priority? and most important, can I change that so the files under /app/assets have more priority?
I've read it uses the first file it finds with the name but I don't know how to tell rails to look in some folder first.
I've tried
config.assets.paths = [Rails.root.join('app','assets')]
or
config.assets.paths = [Rails.root.join('app','assets'),Rails.root.join('vendor','assets')]
on my config/application.rb but it keeps using the file under /vendor/assets
I don't want to fork>modify Typus, I want to tell rails to use my jquery.fancybox-1.3.4.pack.js version of the file
I've seen on github that the last commit on Typus downgrades jquery to 1.8.x but I can't do that, I need jquery 1.9.
I'm also trying to override Typus's application.js inside my project but with no luck.

Serving up pages using Rack (PHP style)

How would I go about building a (small?) Rack app that serves Slim templates from say a "views" directory and static files from "static".
I've tried several frameworks that seem to do this, including -- Serve, Brochure, RackServerPages but they all seem to have their own issues.
Serve won't let you choose a layout
Brochure bombs on a "yield" in the template
RackServerPages won't let me pass locals while rendering a partial
Or is there a gem out there that does it already?
Thanks!
Try Stasis - It has layouts, controllers and local variables.

asset pipeline javascript structure files

At the moment my application.js file looks like this:
//= require_tree ./vendor
//= require_directory ./lib
I also have individual JavaScript files for individual views, e.g. home.js, user.js etc.
Do people put these files in the application.js? If so how do they activate them? Most of my JavaScript is executed from the jQuery ready event handler $(function (){ });
Obviously this does not work if they are all in the same file. How could I minify these files and access the correct ready event if they are all in the same file?
Any help or tips greatly appreciated.
It's fine to have multiple jQuery document ready handlers as you shouldn't care about the order between them. You'll need to check that the elements they work on exist before trying to attach any handlers, though.
For example, in home.js, you might have something like
if ($("#home_div").length) { // do some stuff on the home page }
Doing this is better than splitting up your JS and only serving some pieces on some pages, since with the compiled approach your users only have to download JS once. I think the default rails 3.1 config should minify and concatenate all of your JS in production but not in dev. If you want to enable it manually,
config.assets.compress = true # compress js into one file
config.assets.js_compressor = :uglifier # minify the JS (need the uglifier gem)
config.assets.digest = true # append hashes to filenames so you can set far-expiry headers for caching
some more info, check out the "customizing the pipeline section" of the docs http://guides.rubyonrails.org/asset_pipeline.html#customizing-the-pipeline
and this related question
How to properly work with jQuery in Rails 3.1 asset pipeline?

Sinatra, where to place the require statments

I'm currently developing a Sinatra/Rack app, and I've run into a design problem. I was looking around, and I'm not quite sure where to place the bulk of the require statements.
I figure they go in one of two places, either the main.rb after requiring Sinatra itself, or they go in the config.ru so they are all loaded at the start of the application.
I'm currently leaning towards the main.rb as that is what's loaded by all of the testing applications.
Thank you for your help.
I recommend:
Require your main app file only from your config.ru.
Require Sinatra and views gems in your main app
Create individual init.rb files for each of your helpers, models, and routes, and require those in your main app.
Require DB-related gems in models/init.rb
Here's an example of the layout I use:
Using Sinatra for larger projects via multiple files
Note that by loading DB-related gems and setting up your DB in your models/init.rb you can (from IRB) load just that file and have your full model stack available for poking at.
Take a look at this blog post by Engine Yard. It does a fairly good job of explaining what you want to know: https://www.engineyard.com/blog/using-the-rubygems-bundler-for-your-app
Take a look at my source code.
https://github.com/sirfilip/sinatrablog
:)
Just realized i have to remove all of the require statements in my models since they are not needed anyway.
The most interesting file in there is the bootloader.rb. If you want to follow the request path start from the config ru which acts as a front controller for the app.

Resources