Text 27 Mar Array Intersect and difference

Ran into something pretty slick today with Ruby arrays. The & operator and the - operator.

So if you do something along the lines of [1,2,3,4,5] & [4,5,6,7] you’ll get the intersection of the two arrays [4,5].

And if you do something like [1,2,3,4,5] - [3,4,5] you’ll get the difference of the two arrays [1,2].

Just a little ruby elegance!

Text 8 Feb Rails _method

Quick little tip on method and instance names in the Rails source.

I noticed a few methods with the underscore character prepended to them and some instance variables with the same convention. After doing some digging over the internets it appears the rails source uses this naming convention as a call out that these are internal and should not be called by public consumers.

In one particular place is the OrderedOptions class in ActiveSupport. If you checkout the _get method you’ll see what I mean.

Text 27 Jan Vim Tips (Multi-line Comment)

I ran across this hidden gem today in Vim.

Enter visual mode by pressing Ctrl-v.
Next select the lines you want then press Shift-i.
After that insert the comment character(s). I use Ruby so I entered ‘#’.
Finally press escape and you will have a nice block of comments inserted.

Happy Vim’er!

Text 1 Jan Asset pipeline path

I just ran into a little issue while fooling around with rails and backbone.js. I wanted to start using haml_coffee_assets for templates and added the directory app/assets/templates. When refreshing the page to try and get ahold of the template I was getting an error Sprockets::FileOutsidePaths. Turns out I needed to restart WEBrick in order for the assets path to be “reloaded”

Just a quick gotcha!

Text 15 Dec Test a javascript/json api

Headers, Headers Headers!! I have tested controller calls numerous times when trying to verify that the json response is coming back correct. However I always forget what to set the headers to. I always know its something to do with the request object, but never remember what to enter to make the format.js cal to fire correctly.

request.env[‘HTTP_ACCEPT’] = ‘application/json, text/javascript, *’

This tells rails that, “hey I am expecting you to return json or javascript”.

request.env[‘CONTENT_TYPE’] = ‘application/json’

So my code ended up looking like so:

in the spec helper

Text 19 Jul Launchy 2.0 with Capybara 1.0
Another issue arose tonight where launchy was screaming at me about Capybara save_and_open_page method using an old API.
WARNING: You made a call to a deprecated Launchy API. This call should be changed to 'Launchy.open( uri )'
To fix this I simply updated my gem file to include what version of launchy I was using like so:

gem ‘launchy’, ‘0.4.0’

Next update bundler from the command prompt

bundle update launchy

Bingo, I’m seeing pages again…
Text 19 Jul Passing params to before_filter

Ran across something I never did today. I needed to pass in a param which changed based on whether the route was nested or not. So I could have something like /foo/1 and /foo/1/bar/2.

I am really after what the first number after foo is so I can verify something in my before filter. In the first example /foo/1 the param being passed to the controller is :id. In the second example /foo/1/bar/2 the param is :foo_id.

After some digging I found the info I was looking for on the Rails Guides and came up up with this:

All my cucumber and rspec tests are green and I feel good!

Text 14 Jul easy singleton (eigen) class

I was perusing thru the Rails source code in the ActiveSupport component and ran across this gem

The interesting part for me was the singleton_class method. What a nice handy tool to put in the tool box.

Text 14 Jul 48 notes Devise, authenticate_user! and redirect

While working with a new rails project for my current employer I hooked up Devise for authentication. I wanted all redirects to for authenticate_user! to be sent to /login. Getting this to work was relatively easy and here’s the files I had to edit.

This was all I need to do to get the redirects working.

Text 14 Jul 4 notes Spork and factory_girl - Reloaded

Today I ran across an issue where when running Spork and autotest the factories that I added were not being reloaded. After some Googling I found a simple solution which requires just two lines in the Spork.each_run block. It looks like so

reload_routes! and Factory.factories.clear do exactly what they say. The Dir.glob reads all my factories out of the factories folder and loads each on every run. Simple and to the point!


Design crafted by Prashanth Kamalakanthan. Powered by Tumblr.