Archive for the ‘Ruby on Rails’ Category

Calling Oracle stored procedure from rails

Sometimes it is necessary to reuse existing logic from stored procedures. This line of code shows how to call a stored procedure in Oracle.

ActiveRecord::Base.connection.execute('BEGIN do_the_calculation; END;')

RoR in enterprise – lessons learned

After a while my first enterprise prototype is finished and I have to summarize what was right and what was wrong during the period of prototyping.

Really nice surprise for me was the way of communication. The requirements were formulated more precisely then any requirement before, but not from the beginning. When I did start, the requirements were very vague, but after first screens and first few features the communication was excellent and clear. This was the phase of fundamental principles and relations creation.

After some time, the flow of requirements was stronger and stronger. It was necessary to start requirements management – yes, it is true. You cannot get rid of it.

In the middle of development users started to use it and a new set of “handy little” features was requested.

Later the system became very important and the users started to solve real production issues using the system.

Now, the system is almost finished. I mean – the necessary features are there, but sometimes it is not consistent. Especially the stuff that is used rarely. The enterprise is pushing me to pass it into production regime.
And what are my lessons learned?

  • The start will be painful. Be prepared to completely redesign the code and model.
  • Scaffolding is for some time more than enough.
  • It is not necessary to focus on good graphical desing from the beginning. Focusing on features is more important. In my case I did implement a nice design after 80-90% of features was implemented. And in fact, I did it because the environment was really ugly, not because customer did ask me to do it.
  • Make sure you find the right group of people to prototype with. It is impossible to create a prototype without business experts.
  • It is not important how much you boost your performance using good tools. You will always have more requirements than could be implemented. Requirements management is a must.
  • Communicate clearly that you are working on a prototype. Otherwise you will be forced to make it a real application. Here I started to think about using grails for prototyping. Nevertheless, I have no experience to decide if it was a good idea or not.
  • Be prepared for success! The application that will be created is in line with the requirements and heals the biggest pains of the business users. It is highly probable that the users will love it.
  • Do not stick with one technology. Anything that makes the process faster is valuable. Eg. I did use Sybase PowerDesigner to generate “history keeping” triggers automatically. Adding a new table to my model was just few clicks and assigning the right trigger template.
  • And last, but not least. Listen! Listen more! And make sure you understand that you are not the one who knows the business. You came there to help them to communicate their needs, not to show them how to do their business.

Now I must say, that Ruby on Rails is a great tool for project communication. It is able to communicate user requirements very efficiently and precisely.

From pictures to picturables

This article tries to explain how to convert existing class in Ruby on Rails to a class that can be used in polymorphic association class. As the beginning, I do recommend to read article about polymorphic associations. It explains what they are and how they work.

The whole process will be described on classes Picture (represents picture) and Item (an item that needs a picture).

1. First of all, it is necessary to create columns for the pictures table. The best way is to use migrations. So, create file e.g. 013_convert_pictures_to_polymorph.rb in your db/migrate directory. It should contain something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ConvertPicturesToPolymorph < ActiveRecord::Migration
 
  def self.up
    # Change old item_id identifier to new picturable_id
    rename_column :pictures, :item_id, :picturable_id
    # Add type of association
    add_column :pictures, :picturable_type, :string, :length => 50
    # Fill in the type of association
    execute "update pictures set picturable_type = \"Item\""
  end
 
 
  def self.down
    # Bring it back
    rename_column :pictures, :picturable_id, :item_id
    remove_column :pictures, :picturable_type
  end
end

now run:

1
rake migrate

to create the columns.

2. Once the appropriate columns were created, it is necessary to change the Item class. Change the

1
  has_many  :pictures

to

1
  has_many  :pictures, :as => :picturable

3. Now change the Picture class. Change

1
  belongs_to :item

to

1
  belongs_to :picturable, :polymorphic => true

Make sure, there is no “item” left in the Picture class. If so, change it to picturable and make sure all picturables have the method you are using.

4. Creating a new class enriched by pictures is simple. Just add

1
  has_many  :pictures, :as => :picturable

to your class.

5. Last but not least, change your fixtures. The easiest way is to export the current data from your database. For this purpose I do use manage_fixtures plugin.

Good luck!

acts_as_ferret tip: uninitialized constant Ferret::Index::FieldInfos

I have upgraded acts_as_ferret plugin to the latest version and my fulltext search stopped searching. It was throwing an error message:

NameError (uninitialized constant Ferret::Index::FieldInfos):
    /opt/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:263:in `load_missing_constant'
    /opt/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:452:in `const_missing'
    .//vendor/plugins/acts_as_ferret/lib/acts_as_ferret.rb:119:in `field_infos'
    .//vendor/plugins/acts_as_ferret/lib/local_index.rb:58:in `rebuild_index'
    .//vendor/plugins/acts_as_ferret/lib/local_index.rb:37:in `ensure_index_exists'
    .//vendor/plugins/acts_as_ferret/lib/local_index.rb:9:in `initialize'
    .//vendor/plugins/acts_as_ferret/lib/class_methods.rb:304:in `new'
    .//vendor/plugins/acts_as_ferret/lib/class_methods.rb:304:in `create_index_instance'
    .//vendor/plugins/acts_as_ferret/lib/class_methods.rb:55:in `aaf_index'
    .//vendor/plugins/acts_as_ferret/lib/class_methods.rb:120:in `find_id_by_contents'
    .//vendor/plugins/acts_as_ferret/lib/class_methods.rb:176:in `ar_find_by_contents'
    .//vendor/plugins/acts_as_ferret/lib/class_methods.rb:170:in `find_records_lazy_or_not'
    .//vendor/plugins/acts_as_ferret/lib/class_methods.rb:86:in `find_by_contents'

If you observe the same error, try to run ruby setup.rb in directory …/ruby/gems/1.8/gems/ferret-0.11.4. It seems that gem install ferret needs a small help.

RSpec for Ruby on Rails

Behaviour driven development is currently in. The best ay how to get in touch with it is to setup your own environment and make few examples.Nevertheless, if you prefer to start with a bit of theory, go to http://behaviour-driven.org/.This article describes simple procedure to setup Rspec to work together with ruby on rails.

Installation of RSpec

The installation procedure described in documentation needed small improvement:

First of all, install the rspec gem

gem install -r rspec #mac users must sudo

Then install following gems:

  • rake # Runs the build script
  • rcov # Verifies that the code is 100% covered by specs
  • webgen # Generates the static HTML website
  • RedCloth # Required by webgen
  • syntax # Required by our own custom webgen extension to highlight ruby code
  • diff-lcs # Required if you use the -diff switch
  • win32console # Required by the -colour switch if you‘re on Windows
  • meta_project # Required in order to make releases at RubyForge
  • heckle # Required if you use the -heckle switch
  • hpricot # Used for parsing HTML from the HTML output formatter in RSpec’s own specs

Then continue with these steps:

svn co svn://rubyforge.org/var/svn/rspec/trunk rspec
cd rspec
rake install_dependencies
cd example_rails_app
export RSPEC_RAILS_VERSION=1.2.3
rake rspec:generate_mysql_config
mysql -u root -p &lt; db/mysql_setup.sql
cd ..change example_rails_app/config/database.yml to correspond to your configurationrake pre_commit

and…Make the first test.

Create new folder in your project called spec. Create a file named e.g. basic_test.rb and fill it with

describe "Sum computation" do  
  it "should return 2" do    
    (1+1).should == 2  
  end
end

run it with

spec spec/basic_test.rb

Your test should finish sucessfuly:

.Finished in 0.006001 seconds1 example, 0 failures

Working with rails

That’s great! Now, let’s make it running with your rails objects. Go to the root of your application and install rspec plugins:

ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec
ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails

bootstrap your application with

ruby script/generate rspec

and start testing with the rails objects. Use rspec generator to create the first test:

ruby script/generate rspec_model user

It generates file spec/models/user_spec.rb file

require File.dirname(__FILE__) + '/../spec_helper'
describe User do  
  before(:each) do    
    @user = User.new  
  end  
 
  it "should be valid" do    
    @user.should be_valid  
  end
end

And now you can just extend the pre-generated file and enjoy it.