Posts Tagged: ruby on rails


27
Feb 10

Hire me!

I recently left my group (Syndeomedia, Exist Global, G2iX) to do freelance software development on the Ruby on Rails framework, and have a bit of time to work on personal projects on the side.

So here you go: I am for hire. If you are interested in working together, please do contact me regarding your project and timeline.


8
Feb 10

Collaboration for Heroku Users

For some reason, there aren’t enough guides for collaboration on Heroku. There is a quickstart guide over at the Heroku docs for cases when both source hosting and deploys happen through a single repository on Heroku. It does not cover other scenarios though.

This is mostly on Git, which Heroku uses for source control and deploy triggers. This also assumes that the heroku gem is all installed and set up.

Separate Source Hosting

Starting off with the most common scenario: Your source is hosted somewhere else (GitHub, for example) and you deploy on Heroku.

If you have not done so yet, clone your application:

$ git clone git@github.com:me/myapp.git myapp

Besides making a copy of the code in the myapp directory, this also takes care of adding the repository as the default remote "origin". This means that, by default, doing:

$ git push

from the directory would already be equivalent to doing:

$ git push origin

which should push your changes up to the source host.

Next let us add the repository on Heroku as a different remote:

$ cd myapp
$ git remote add heroku git@heroku.com:myapp.git
$ git fetch heroku # Download objects and refs

At this point, you should be able to start deploying with:

$ git push heroku

and run heroku gem commands like such:

$ heroku console
$ heroku info

Multiple Environments

Elijah Miller wrote a great guide Deploying Multiple Environments on Heroku. If you are looking into setting up the multiple environments, head to that article instead. The rest of this section is for collaborating on already set up application environments.

So let us say we want to work with the following scenario:

Role Heroku Git Repository
Source - git://github.com/me/myapp.git
Production myapp git://heroku.com/myapp.git
Staging myapp-staging git://heroku.com/myapp-staging.git

The procedure is pretty much the same as above. First, you need to clone your source repository:

$ git clone git@github.com:me/myapp.git myapp

The next step is adding the Heroku repositories as remotes. You will have to come up with remote names better than “heroku” since you will be working with more than one Heroku application this time. The most obvious decent naming scheme is, of course, naming them after the environments:

$ cd myapp
$ git remote add production git://heroku.com/myapp.git
$ git remote add staging git://heroku.com/myapp-staging.git
$ git fetch production # Download objects and refs
$ git fetch staging # Download objects and refs

Since the source repository has been added as the “origin” remote, pushing changes for regular updates even pre-deploy would still be as simple as:

$ git push

Deploying is also straightforward:

$ git push production production-branch-name:master
$ git push staging staging-branch-name:master

Since there will be more than one Heroku repository in your Git configuration, the heroku gem will not be able to automatically determine the application you want to run application commands for. You will need to specify the application name like this:

$ heroku console --app myapp
$ heroku rake db:migrate --app myapp-staging

18
Feb 09

ActiveRecord association :reset

Is it just me, or is the use of reset() on one-to-many and many-to-many ActiveRecord associations quite unpopular? I actually have never used it (maybe never needed to use it) and have never seen it used yet.

I was looking into performance issues in one of our projects, and noticed some bit of association loading within a loop. I realized it is actually quite deceiving if we don’t remember some things.

Consider the following scenario:

persons = Person.find(:all)
persons.each do |person|
  books = persons.books
  # ... And then say a bunch of rendering goes here.
end

Whatever the reason, let us say there are on the average 50 books associated to each person, and we need a list of all books associated to each of 50 persons in a single go. That could grow a pretty large data set.

Sure, the books for each person record are assigned to a variable within the scope of the each block and would normally be garbage collected as soon as the loop ends an iteration, but we might be ignoring that these books have been stored for the :books association of each person record. Each person record then is still referenced in the persons array, so there will be no garbage collection happening there either.

In goes the reset() function. The reset() function on associations simply clears the association data @target and updates the boolean tracker @loaded to put things back how they are before association data is loaded.

persons = Person.find(:all)
persons.each do |person|
  persons.books
  # ... And then say a bunch of rendering goes here.
  person.books.reset
end

This did not help the particular area I was looking at as we are dealing with a harmlessly small collection there. Large index pages and sitemaps should employ memory-efficient tricks too, like combinations of pre-loading record IDs and paginated fetching, so this might not exactly be useful knowledge. It really is something to watch out for, though, and it could contribute much in memory optimization.


9
Aug 08

validates_as_phone on GitHub

A second plugin to GitHub today.

validates_as_phone is a Ruby on Rails plugin that provides strict validation for phone numbers. At the moment, only phone numbers in Australia are supported. This will still be extended to support classifications and areas in the future.

validates_as_phone
==================

Strict validation module for phone numbers that supports classifications and areas.

= General usage

== Installation

You can install the plugin the traditional way. Go to your application root
and do:

  script/plugin install git://github.com/kristinalim/validates_as_phone.git

== Validate your model attributes

Example:

  class Person < ActiveRecord::Base
    validates_as_phone :phone, :allow_blank => true, :set => true
  end

= License

Written by Kristina Lim (http://i-think.com.ph/kristina/)

Copyright (c) 2008 Syndeo Media

http://syndeomedia.com

= Contributing

If you wish to contribute to the project, you may contact the author through:

'kristinasyndeomediacom'.insert(8, '@').insert(20, '.')

= Acknowledgements

This plugin is named after the validates_as_phone plugin of Jerrod Blavos
(http://code.google.com/p/validates-as-phone/). Admittedly, this plugin was
built over the latter, but as the purposes of this plugin is more complex than
that of the minimal one, practically all of the code has been written over by
now.

9
Aug 08

schedule_attribute on GitHub

I pushed schedule_attribute to GitHub early today.

The Ruby on Rails plugin schedule_attribute provides support to ActiveRecord for a Data Definition Language (DDL) for schedules.

schedule_attribute
==================

Provides support to ActiveRecord for a Data Definition Language (DDL) specific to schedules.

= Language

A datetime pair comprised of the start datetime and end datetime is defined with two lines. At the moment, only one option is available.

== Weekly Schedule

  mon 09 00 ==> Monday, 7 AM
  01 17 00  ==> Monday, 5 PM

  1 00 00   ==> Monday, midnight
  2 05 00   ==> Tuesday, 5 PM

  5 09 00   ==> Friday, 9 AM
  0 09 00   ==> Subday, 9 AM

= General Usage

== Installation

You can install the plugin the traditional way. Go to your application root and do:

  script/plugin install git://github.com/kristinalim/schedule_attribute.git

== Specifying the Attributes for Schedule

In your models, you can do:

  class Person < ActiveRecord::Base
    schedule_attribute :o ffice_hours, :type => :weekly
  end

== Checking if a Datetime is Within the Schedule

  person.within_schedule?(:weekly, :o ffice_hours, Time.now)

= License

Written by Kristina Lim (http://i-think.com.ph/kristina/)

Copyright (c) 2008 Syndeo Media

http://syndeomedia.com

= Contributing

If you wish to contribute to the project, you may contact the author through:

'kristinasyndeomediacom'.insert(8, '@').insert(20, '.')