Rails snippet: Setting up TravisCI

Travis CI is great way to get continuous integration into a project.
So just to get started here is a quick walkthrough, through the setup for a
project of mine. Basically it’s a 3 step process:

Setup rake to run the tests

This is easy, and probably already happens anyway! Just to check

$ rake

Setup .travis.yml

My own .travis.yml for
ActiverecordTranslatable looks like this:

http://gist-it.appspot.com/github/sideshowcoder/activerecord_translatable/blob/master/.travis.yml
So line by line

http://gist-it.appspot.com/github/sideshowcoder/activerecord_translatable/blob/master/.travis.yml?slice=0:2
Set the ruby version to 1.9.3

http://gist-it.appspot.com/github/sideshowcoder/activerecord_translatable/blob/master/.travis.yml?slice=3:5
Setup the database for the tests

Setup a github hook to notify travis about commits

Now to complete the setup setup the hook for travis under the Settings → Service Hooks on Github.

So now quick, learn more checkout TravisCI docs it’s
awesome.

Enjoy

Rails snippet: Rendering a view from model!

Sometimes it comes in handy to render a template from another place than a
controller in Rails. For example using
WickedPdf to generated some pdfs
inside a model comes in quite handy. Normally you need to setup a lot of stuff
for WickedPdf but since everything is already done when calling the build in
render, we can just reuse this.

https://gist.github.com/sideshowcoder/5009089.js

So to call render from somewhere besides a controller, well we need a
controller and we can just call render from there, what pops out is the
actually rendered template as a string (that’s why we call render_to_string of
course), which we can now ship of to wherever we need it.

Enjoy

UPDATE: As Fladam Orin points out in the comments this falls flat if you use helpers like url_for since the controller lacks a request object. This is a problem which can be solved by using an ActionView and loading the helpers.

Rails snippet: running a single test for rails

Getting started to debug a problem within the rails lib itself it is nice to be
able to just run a single test. Doing this is pretty straight forward, still it
took me some minutes to get the command right since I found a lot of non working
ways online first. So here it is (as an example I run the test_label test in
actionpack):

$ cd actionpack
$ bundle exec ruby -I"lib:test"  
test/template/form_helper_test.rb -n test_label

Enjoy

Look at this post as
well for further details about test running in rails.

Convert a check box value to a boolean

Most of the time when I create a model with a boolean attribute I don’t actually
use a boolean in the database. A popular example is published on a blog post,
being implemented via setting the publish_date.

This means that in Rails to use form_for you just need to define the accessors
on the model and everything should work, except it doesn’t because the form
returns “0” for false and “1” for true, as strings. A real ActiveRecord model
gets around this by converting the values to boolean before setting. Since it is
tedious to implement time and time again it is easy to just reuse the Rails
internal function.

ActiveRecord::ConnectionAdapters::Column.value_to_boolean("0")

=> false

ActiveRecord::ConnectionAdapters::Column.value_to_boolean(“1”)

=> true

Quick and easy!

Enjoy.

Checking for the css class of the found element in capybara

Having an element like

My Text

and trying to check if the class is set to “.some-class”

page.find("#some-id").has_css?(".some-class")

does not work in capybara. This is due to the fact that #has_css? checks for
the css being available inside the element not on the element itself. But is
it still easy to check it by accessing the attributes hash.

page.find("#some-id")[:class].include?("some-class")

Works just fine.

Rails snippet: Making a ActiveRecord model save fail

This is the start of bunch of (hopefully) regular small posts with snippets for ruby and
rails. It’s supposed to be about those little things I keep looking up again and
again.

so back to topic: Making a rails model save fail!

  1. Add something to the errors in the validation

https://gist.github.com/4574361.js

  1. Make a before_filter fail (eg. before_create, …)
    Return false in a before_filter, will cause the model save to fail

https://gist.github.com/4574343.js