Working with timeranges

Posted by Andre Foeken Fri, 31 Aug 2007 19:23:31 GMT

Everything in moves 'acts_as_placed_on_timeline'. This entails that every object has a valid_from and valid_to field, which specify when they are active.

A simple example is an address, it becomes active (valid_from) when a person starts living there and is deactivated (valid_to) when the person moves to another address.

Looking at the example above it is hard to see why this would ever pose a problem with code readability. Hang in there a moment longer.

Read more...

Posted in ,  | Tags , , , ,  | 1 comment

Gettext Generators 1.2.3 Released

Posted by Bart ten Brinke Tue, 28 Aug 2007 18:43:07 GMT

The longawaited update for the Gettext Generator.

  • First release as plugin
  • Added svn:merge raketask
  • Updated generated resource to mimmick scaffold_generator packaged with rails 1.2.3 tag

So go to RubyForge or take a look here.

I'm currently getting the plugin uploaded to rubyforge. As soon as I have that figured out how that works, I'll post the link here.

ruby script/plugin install svn://rubyforge.org/var/svn/gettextgnrtrs/tags\ /Gettext-Generators-1.2.3/

Posted in , ,  | Tags , ,  | no comments

Solving some gem install problems

Posted by Andre Foeken Sun, 29 Jul 2007 16:08:14 GMT

Lately we have been having some issues installing gem that have to compile native extensions like libxml-ruby and termios.

The error we got from libxml was: "extconf failure: need libm". Strangely this was all caused by incorrect compile flags in the rbconfig.rb file.

To fix this (and other issues) you can customize the rbconfig.rb file to your specific computer.

Mine was located in:

 /usr/lib/ruby/1.8/universal-darwin8.0/rbconfig.rb

On leopard this is universal-darwin9.0. The file contains a lot of config definitions. I removed all traces of any other architecture then i386 (like -arch x86_64, etc). I also removed the 64 flag that was included.

After altering this in the entire file (quite a few replaces), everything compiled happily.

update

I was also having some issues with the SQLSessionStore plugin. Seems recent changes to ruby have made it a no-no to alter constants (no surprise there). So now in stead of doing

ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS. update(:database_manager => SqlSessionStore)

You have to do:

dso = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS dso[:database_manager] = SqlSessionStore ActionController::CgiRequest.send(:remove_const, "DEFAULT_SESSION_OPTIONS") ActionController::CgiRequest.const_set("DEFAULT_SESSION_OPTIONS", dso)

Which is uglier, and still should not be possible :)

To make it work I also had to remove the require 'mysql' line from the plugins MySqlSession.rb file. Else it would crash rails with the following error:

warning: already initialized constant OPTIONS

Posted in ,  | Tags , , , , , , , , , , ,  | no comments

Sort, sort_by and group_by

Posted by Andre Foeken Sun, 29 Jul 2007 14:57:24 GMT

I wanted to put the spotlight on some small but very useful ruby on rails functions: sort, sort_by and group_by.

Let's start with sort and sort_by. Its basic function should not come as a surprise: it sorts a list.

[1,3,2,4].sort => [1,2,3,4]

These functions really shine when you use objects inside the list.

a = { :key => 0, :value => 'a', :extra => 0 }
b = { :key => 1, :value => 'b', :extra => 0 }
c = { :key => 2, :value => 'c', :extra => 1 }
d = { :key => 3, :value => 'd', :extra => 1 }

[a,c,d,b].sort_by{ |i| i[:key] } => [a,b,c,d]

The previous example already contains the correct objects to demonstrate the use of group_by so we just give that example now.

[a,b,c,d].group_by{ |i| i[:extra] } => [ 0=>[a,b], 1=>[c,d] ]

As you can see this gives us the power of the SQL group_by statements right inside ruby. Sure, this cannot beat the speed of the SQL equivalent. But sometimes when speed is not really an issue or when you just can't query the right results, group_by can be a real life-saver.

As a bonus, when you want to group_by or sort_by functions on objects, you can use this pretty shorthand:

[a,b,c,d].group_by(&:length) => [ 3 => [a,b,c,d] ]

If you are wondering why the above works, it is just basic ruby. Any function that is defined to receive a &block can be called by either giving it a block of code in parenthesis or by passing it a proc object. The &:length is just shorthand for &proc{ |i| i.length }. Too bad you can't pass it arguments using the shorthand, then you'd be able to write:

[a,b,c,d].group_by(&:fetch, :key)

Hope you find this useful, if anyone knows if the above code is indeed possible in another form, please comment!

Update: I recently found out you can also use arrays in combination with these functions. So you can sort by multiple values. If you pass array [a,b] to a sort function it will sort by a and then sort by b. Very handy!

Posted in ,  | Tags , , ,  | 5 comments

RailsConf Europe 2007

Posted by Andre Foeken Thu, 19 Jul 2007 21:10:53 GMT

Just wanted to share we will be attending RailsConf 2007, so if anyone wants to say hello that's their chance!

Currently we are swamped with work, as our version 1.0 is coming up. But don't worry out multi-page plugin hasn't been forgotten, it'll get there!

Posted in ,  | 3 comments

Speeding up Active Resource

Posted by Bart ten Brinke Thu, 19 Jul 2007 08:20:47 GMT

As I was trying to import 12 MB of XML, it quickly became very clear that the SimpleXML used in Hash.from_xml was not going to cut it. It took nearly four minutes to convert the xml data to a hash!

As Active Resource will probably be used to handle large xml files, I created a patch so that libxml is used to parse the xml. This made fetching the active resource go from 240 seconds to 12 seconds. That 20 times faster!

The net override (also included) makes downloading a factor four faster.

Find the patch here: http://dev.rubyonrails.org/ticket/9017

Posted in  | Tags , , , , ,  | no comments

Fixing ugly view syntax

Posted by Bart ten Brinke Thu, 05 Jul 2007 13:16:10 GMT

I was busy working on some view templates, but there was some syntax in my views that was absolutely annoying me.

<%= submit_tag h(_('Update')) %>

This can be solved in three locations, all of them extremely ugly:

  • Overwrite submit_tag in the form builder to escape every parameter. This will cause all different uses of submit_tag to break.
  • Escape all entries in my gettext po file and drop the h. Which is not very secure.
  • Overwrite _ to escape everything, which will cause all sorts of strange behaviour on other locations.

After looking at all four, I realised I could fix it a fourth way:

module ApplicationHelper
 def h_(string)
    h(_(string))
 end
end

Making my forms now much nicer to look at and very secure:

<%= submit_tag h_('Update') %>

Dr. Nic, eat your heart out :)!

Ps: add this to your gettext rake task:

 GetText::RubyParser::ID = GetText::RubyParser::ID + ['h_']

Posted in  | Tags , , ,  | no comments

Testing a REST full activeresource

Posted by Bart ten Brinke Wed, 04 Jul 2007 13:49:28 GMT

Active Resources are nice, nut when you're trying to test your active resources , you're in for a long night. Luckally someone did a lot of the basic work for us. The HTTPMock class in ActiveResource is a very handy tool for testing your active resources. Unfortunately there is absolutely no documentation about how to use it, and it has some behaviour you might not expect. Therefore I present you with this code example.

The remote models of our REST connection live in Connections::IO::REST. When requesting the Employees via Connections::IO::REST::Employee.find(:all), we are actually requesting /employees.xml from our HTTPMock class. The HTTPMock class then just outputs the XML file of the testset we got from our friend which wrote the REST-connector we are connecting to. In the example, this is: /test/remote_fixtures/connector/employees.xml

 class IORestConnectionTest < Test::Unit::TestCase

 def setup
   @pre      = Connections::IO::REST
   @mock_url = 'http://localhost/'
   @headers  = {"User-Agent"=>"Moves", "Accept-Encoding"=>"deflate", "Content-Type"=>"application/xml"}
   ActiveResource::HttpMock.respond_to do |mock|
     mock.get "/employees.xml", @headers, ioconnect_resources('employees')
     mock.get "/clients.xml"  , @headers, ioconnect_resources('clients')
   end

   # Overwrite all site URLS
   @pre::Resource.site = @mock_url
 end

 def ioconnect_resources(name)
   path = File.join(RAILS_ROOT, "test", "remote_fixtures", "ioconnect", "#{name.to_s}.xml")
   return nil unless File.exists?(path)
   File.read path
 end

 def teardown
     ActiveResource::HttpMock.reset!
 end

 def test_should_find_all_employees_via_rest
   employees = @pre:Employee.find(:all)
   assert_equal employees.count, 20
 end

 end

One last note: HTTPMock was created to be as simple as possible. If you request something with a different header or parameters in the URL, it will return absolutely nothing. So if you get no response from your mock activeresource, be sure to check the headers you sent.

Posted in  | Tags , , , ,  | 1 comment

Rails OSX Problems

Posted by Bart ten Brinke Tue, 03 Jul 2007 08:57:16 GMT

After updating my MacBook yesterday, I found myself with a broken rails. I reinstalled Rubygems, but this gave me some strange problems when installing gems:

   bart$ sudo gem install mongrel
   Building native extensions.  This could take a while...
   Successfully installed mongrel-0.3.3
   Installing ri documentation for mongrel-0.3.3...
   Installing RDoc documentation for mongrel-0.3.3...
   bart$ sudo gem install mongrel_cluster
   Install required dependency mongrel? [Yn]  Y
      ERROR:  While executing gem ... (Gem::GemNotFoundException)
       Could not find mongrel (>= 0.3.13.4) in any repository

But when I looked at the versions, it said 1.0.1 of mongrel was available. After some help of Andre, we found out the problem: Ruby. As the OSX update had trashed my .bashrc path settings, I got the old ruby version packed with darwin (1.8.2) instead of the fink version (1.8.5). This causes the strange problems like the one above, as mongrel requires a newer version of ruby. Making the path look in /usr/local/bin before anything else cleared up everything nicely.

Posted in ,  | Tags , , ,  | no comments

PDF generation in Rails

Posted by Bart ten Brinke Tue, 19 Jun 2007 08:57:42 GMT

I'm curently doing some restfull PDF views via PDF::Writer. This all works pretty well, except for the fact that I found it was impossible to access layout functions in my Action View helper file from my rpdf view. If you want to do this, you have to access them explicitly via the @action_view instance variable:

 @action_view.build_header(margin_left, margin_top, 'Worksheet', pdf)

As this took me some massive google-ing to find this, I thought it was wise to share this solution.

Posted in  | Tags , , ,  | 2 comments

Older posts: 1 2 3 4 5