Sunday, May 18, 2008

"Skinny Controller, Fat Model" best practice

My first impression about reading about it ("Skinny Controller, Fat Model") was that it is a basic of Object-Oriented Programming. If you are familiar with Object-Oriented Programming practices such as small method, refactoring, Design Patterns, your code naturally becomes what this article is proposing. Especially, my impression is that if you are doing Test/Behaviour-Driven Development, it is even difficult NOT to become as proposed.

What I liked about "Skinny Controller, Fat Model" is that it is explaining the best practice from the different perspective and with the simpler expression. You can reach the same code either following the basic Object-Oriented Programming practices or following "Skinny Controller, Fat Model".

Ultrasphinx setup part2

Regarding what I wrote about test database in section "4. Build index" in my previous post "Ultrasphinx setup", RSpec code would become like below.

I put this under spec/models accessing database. But for controller spec under spec/controllers, I mocked Ultrasphinx::Search so that the controller spec is not accessing database.

(Note: The line with "system(..)" is one line.
It is shown to be multiple lines because of the space.)

require File.dirname(__FILE__) + '/../spec_helper'

describe "Ultrasphinx sample" do
fixtures :overviews

before(:each) do
system("cd #{RAILS_ROOT};
rake ultrasphinx:index RAILS_ENV=\"test\"")
system("cd #{RAILS_ROOT};
rake ultrasphinx:daemon:start RAILS_ENV=\"test\"
&> /dev/null")
end

after(:each) do
system("cd #{RAILS_ROOT};
rake ultrasphinx:daemon:stop RAILS_ENV=\"test\"
&> /dev/null")
end

it "should find text with 'test'" do

@search = Ultrasphinx::Search.new(:query => "test")
@search.run

@search.results.size.should == 2

end

end

Thursday, May 15, 2008

Ultrasphinx setup

It was more straightforward to figure it out than other things I have done in the past. But I just write the whole thing I did to set up Ultrasphinx.

1. Sphinx installation

$ wget http://www.sphinxsearch.com/downloads/sphinx-0.9.8-rc2.tar.gz

$ tar xvfzp sphinx-0.9.8-rc2.tar.gz

$ cd sphinx-0.9.8-rc2/

$ ./configure --prefix=/usr/local

$ make

$ sudo make install

2. Install Chronic
(Chronic is a natural language date/time parser written
in pure Ruby.)

$ sudo gem install chronic

3. Install Ultrasphinx plugin

$ cd RAILS_ROOT

$ svn export svn://rubyforge.org/var/svn/fauna/ultrasphinx
/trunk/vendor/plugins/ultrasphinx
(if GIT or other version control system is used.)

(Or if Subversion is used,
$ script/plugin install -x svn://rubyforge.org/var/svn/fauna/ultrasphinx
/trunk/vendor/plugins/ultrasphinx)

$ cp RAILS_ROOT/examples/default.base RAILS_ROOT/config/ultrasphinx/

Add is_indexed to the model: e.g.

class Overview
is_indexed :fields => [ 'title', 'description' ]
end
4. Build index

$ rake ultrasphinx:configure <= Generates development.conf

($ rake ultrasphinx:configure RAILS_ENV="test" <= Generates test.conf)

$ sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql
(Because "rake ultrasphinx:index" has an incorrect path "/usr/local/mysql/lib/mysql".
Otherwise, "rake ultrasphinx:index" fails.)

$ rake ultrasphinx:index <= For development database

($ rake ultrasphinx:index RAILS_ENV="test" <= For test database)
(Of course, in the Test::Unit or RSpec, test database is cleaned up every time so this has to be put inside Test or Spec.
i.e.
system("cd #{RAILS_ROOT}; rake ultrasphinx:index RAILS_ENV=\"test\"") )

5. Start/Stop daemon

$ rake ultrasphinx:daemon:start <= For development database

($ rake ultrasphinx:daemon:start RAILS_ENV="test" <= For test database)

- How to stop Ultrasphinx

$ rake ultrasphinx:daemon:stop

Saturday, May 03, 2008

GIT

I have started using GIT recently. I like it very much.
In fact, within a day of starting using it, I switched all the current projects to it.