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.

Rails snippet: Get a local copy of rails

Working with rails a lot it is nice to have the codebase itself at hand and
runable to check out how things actually work. Getting setup is really easy
compared to a lot of other projects, actually there are just a couple of steps
to get up and running. First you need a machine with all the necessary stuff to
run everything included in rails, which is a lot so I recommend the rails dev
box
for this. This setup installs and
boots a virtual machine using vagrant and
virtualbox, so you need to install
both first. Afterwards clone the rails-dev-box repository

$ git clone https://github.com/rails/rails-dev-box.git

And setup the box itself, which will take a couple of minutes, depending on your
internet connection.

$ cd rails-dev-box
$ vagrant up

Next up its time to clone rails itself inside the rails-dev-box folder, so it is
available inside the VM. Now to dive into i.e. 3.2 checkout the right branch.

$ git clone https://github.com/rails/rails.git
$ git checkout -t origin/3-2-stable

Now you can SSH in and switch to the rails folder to continue

$ vagrant ssh
$ cd /vagrant/rails

Finally just like with every other modern rails app, just run

$ bundle install

To get every gem needed installed, and run

$ rake test

The tests will take a while (like with every modern rails app ;) ) and you can
be sure to have a valid rails installation.

Next up more on running tests.

Enjoy

Rails snippet: scripts

To keep my development workflow consistent I rely on scripts being present in my
projects to handle running my specs as well as starting my test server without
having to think about which project I am in. Since those scripts are ment to be
consistent across projects it only made sense to move them to a repository. So
for anybody interested they are available on github

To use simple clone to whichever folder inside your project, or add a submodule

$ git submodule add
git://github.com/sideshowcoder/rails_scripts.git
script/rails_scripts

And run via
$ scripts/rails_scripts/test
$ scripts/rails_scripts/start_dev_server

Personally I mapped a vim key to do so.

Enjoy

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

giant robots smashing into other giant robots: Process Jobs Inline when Running Acceptance Tests

Link: giant robots smashing into other giant robots: Process Jobs Inline when Running Acceptance Tests

thoughtbot:

Web apps often move long-running processes (such as delivering email) out of the request/response cycle into a queue of background jobs. A worker process then picks up those jobs from the queue and runs them in the background.

In acceptance tests, it’s desirable to run those background jobs…

Make your testing live simpler by not delaying execution of async tasks during test.

Thoughtbot provides a nice resource with their blog for sure :)