Regdel on Rails

I’m pleased to report that my work with Regdel on Rails is coming along steadily. While I can’t say I was an absolute fan of ActiveRecord in the past, I am really digging it now.

Here’s a sneak peak at the account model:

  1 class Account < ActiveRecord::Base
  2   include AccountMethods
  3 
  4   ACCOUNT_TYPES = ["Asset", "Liability", "Equity", "Revenue", "Expense"]
  5 
  6   serialize :attrs
  7 
  8   validates :name,
  9             :presence => true,
 10             :uniqueness => true
 11 
 12   validates :type,
 13             :presence => true
 14 
 15   has_many :entries, :through => :entry_amounts
 16   has_many :entry_amounts
 17 
 18   acts_as_nested_set
 19   state_machine :initial => :active do
 20   end
 21   
 22   def destroy
 23     raise ActiveRecord::IndestructibleRecord
 24   end
 25   
 26   def as_base
 27     self.becomes(Account)
 28   end
 29   
 30   def balance
 31     entry_amounts.sum(:amount_in_cents)
 32   end
 33 end

I’d rather not depend on Rails too much though, instead I plan to abstract out some of the functionality I’m aiming for into gems or raw code. For instance, instead of leveraging the Rails callback API, I plan to instead use the state_machine gem as much as possible.

Take an invoice, for example. It can have the following states:

  • new
  • issued
  • mailed
  • paid
  • partially paid
  • overdue

The transitioning of the invoice from one state to another to trigger events, just like Rails callback events. Why? I think its a cleaner and more precise API, and hopefully the gem maintainer would manage the interface to Rails as Ruby on Rails version 3 evolves - so I don’t have to!

Not that gems are completely stable, though, or without bugs / unexpected behavior…

UPDATE: I have merged the rails3 branch into the master branch, after tagging the sinatra spot!

By Albert on July 12, 2011 6:35 PM

Categories:

Categories