Le Coin Wiki
d'Olivier Dalle
$WikiTagline
 

This page is a kind of Open NoteBook about tips, tricks and any useful information I happen to collect about the Ruby-on-Rails framework and the Hobo extension.

DISCLAIMER: The information on this page may be outdated.
Ruby-on-Rails/Hobo is a very complex (and powerful) software stack. Everything changes all the time. The only guarantee given here is that, at some point, what is reported here was accurate/working. Also, I am not really interested in maintaining an up-to-date version of this content, but everybody is welcome to share their tips and tricks with me!

1.  Useful Links

Ruby on Rails Guides
http://guides.rubyonrails.org/
Hobo Central (Hobo main site)
http://hobocentral.net
This link lands directly on the latest version (Currently Hobo 2.0/Rails 3.2).
Michael Haartl’s great RoR tutorial
http://ruby.railstutorial.org/
Rspec testing framework
http://rspec.info/
RSpec is an excellent framework but quite tricky to set up with RoR/Hobo

2.  My current configuration

It took me a long time to reach an ‘almost stable configuration. I work on my MAC, but in a linux vm because I had too much trouble mixing several ruby versions.

Linux Version
Xubuntu 13.04
Ruby manager
RBenv

2.1  Installation Procedure

This section describes the steps followed after installation of my vm with a pristine XUbuntu-13.04.

       * ok
  • Set version 2.0.0-p247 as default
    • rbenv global 2.0.0-p247
  • hobo (RoR extension) needs nodejs:
    • $ sudo apt-get install nodejs-dev
  • Installing rails:
    • $ gem install rails
      • installs rails 4.0.0 (unsure if it works with hobo…)
    • $ gem install -v 3.2.14 rails
      (force install of version 3.2.14)
  • Mess after installing both rails 4.0 and rails 3.2, solved by:
    • $ gem uninstall rails railties
      (all versions)
    • $ gem install -v 3.2.14 rails
  • installing hobo
    • $ gem install hobo —pre
      • installs hobo -v 2.0.1
  • installing bundler
    • $ gem install bundler

2.2  Installing Java

All you need to know about installing java seems to be available on Ubuntu’s doc site: http://doc.ubuntu-fr.org/java

  • So, it is possible to install Oracle’s java using apt, as follows:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
  • Then you may want to install the firefox plugin:
mkdir -v ~/.mozilla/plugins
sudo apt-get remove icedtea-6-plugin && sudo apt-get remove icedtea-7-plugin
rm -v ~/.mozilla/plugins/libnpjp2.so
ln -s /usr/lib/jvm/java-7-oracle/jre/lib/i386/libnpjp2.so ~/.mozilla/plugins/

2.3  Creating a new app

  • go into dev directory
    • $ mkdir ~/dev ; cd dev
  • Run hobo new project command
    • $ hobo new proj_name
  • Say yes to run wizard
  • Choose all the defaults (always press enter)
    • I experienced several issues when trying something else than default, eg. trying to set up non default testing environment. Never mind, default is safe to start with.
  • Last wizard questions
    • answer ‘m’ (generate and migrate)
    • select default language only (‘en’)
      • other languages are not well supported, only the machinery seems to be there.
    • don’t initialize git yet
  • Edit .gitignore
    • Add temp files, eg. emacs backup: *~
  • Set up git
    • % $ git init && git add . && git commit -m “Initial import”
  • check that everything works fine
    • console:

$ rails c
Loading development environment (Rails 3.2.14)
irb(main):001:0> User.all
User Load (0.3ms) SELECT “users”.* FROM “users”
=> []
irb(main):002:0> quit

  * web server (on local port 3000):

$ rails s
=> Booting WEBrick
=> Rails 3.2.14 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013–11–12 07:27:35] INFO WEBrick 1.3.1
[2013–11–12 07:27:35] INFO ruby 2.0.0 (2013–06–27) [i686-linux]
[2013–11–12 07:27:35] INFO WEBrick::HTTPServer#start: pid=6775 port=3000

  * simply visit this URL to see result: http://localhost:3000

3.  Testing

Setting up a sophisticated environment fo testing is a bit tricky. I managed once to setup Rspec, but it’s quite some work to keep the tests up to date with hobo. It takes seconds to make LOTS of change, and then it takes days to cacth up implementing tests. I did once implement RSpec tests for the user login and creation, following M. Haartl’s tutorial.

The only very basic feature I use for now is the yaml fixtures to reset and re-populate the database. However, even this is quite tricky to start with. Indeed, by default, a newly initialized hobo starts with an empty users table. We want this table to be pre-loaded with at least an admin user and a regular user.

3.1  A user fixture

Here is an example to fill-in the user table with an admin user (dump this in file test/fixtures/users.yaml) :

admin:
  id:             1
  name:	          "Olivier Admin"
  email_address:  olivier@example.com
  administrator:  true
  state:	  "active"
  crypted_password: <%= User.encrypt('password', 'salty') %>
  salt:		  'salty'

Notice some important details:

  1. state is an attribute automatically added by the hobo_user life-cycle. If you forget it, you won’t be able to login.
  2. the new user form requests a passowrd/password_confirmation, but what is actually saved is a crypted_password. To make it less easy to crack, an additional salt value is added to the actual password.
  3. No need to set the timestamp values, they are automatically added

Then you can check the result as follows:

$ rake db:fixtures:load models=users

$ echo “User.all” | rails c
Loading development environment (Rails 3.2.14)
Switch to inspect mode.
User.all
User Load (0.2ms) SELECT “users”.* FROM “users” [#<User id: 1, crypted_password: “cc9a35990355e2e1db6311e9a85acdd843ef595f”, salt: “salty”, remember_token: nil, remember_token_expires_at: nil, name: “Olivier Admin”, email_address: “olivier@example.com”, administrator: true, created_at: “2013–11–12 23:26:03″, updated_at: “2013–11–12 23:26:03″, state: “active”, key_timestamp: nil>]

4.  Tips and Tricks

4.1  Linked list of records

Ever thought of building linked list of records? For example a product could be part of a series of product with some kind of meaningful sequence (eg version 1, version 2, …). When programming in-memory data-strictures, linked lists may be good candidate for implementing this, but when it comes to data-base, simply forget about linked lists. There are three good reasons:

  1. it is awfully inefficient, because to have to query the database many times to reach one element and good (efficient) DB practice is to try to do it in a minimal amount of queries
  2. it is a programming nightmare. Just try it for the fun, if you are more successful than I was, I am curious of the solution. For example, a simple belongs_to/has_one is not symetric and if the has_one is not bound, well you have to deal with the case. If you want to make it symetric, then you need a join table the same way as for a many-many relation with the added constraint that the join table joins the same table on both ends, which requires to rename the associations to non-default names which is hard to solve with ActiveRecords.
  3. There exists a better and simpler solution!! … See acts_as_list below.

5.  Some useful hobo plugins

5.1  acts_as_list: build ordered list that can be reordered

Acts as list lets you build ordered series of records that can be reordered at will. This plugin is fully and nicely integrated in Hobo/Rails. Once its set up for a table, it becomes as easy as drag and drop to change the position of a record in a list.

5.2  paperclip: upload files to server

There are two designs for uploading files, a good one and a bad one. The bad one consists in saving the raw data in the data-base. See Nick’s comment on this page for the whys: Hobo Tutorial: Uploading for Hobo app

The good one consists to save the file on the file system and for this purpose hobo has also a well supported plugin: paperclip. See here: Hobo paperclip

6.  Polymorphic associations

Coming soon…