Regdel Demo Now Using Rails 3.1

By Albert on September 11, 2011 7:56 AM

I’ve been completely revamping Regdel to use Rails 3.1 for quite some time now, but all the while, the Regdel demonstration was featuring the Sinatra branch.

I’d been holding off until I could find the time to make the switch, and yesterday was that day.

Without further ado: Regdel Demo, the Rails version

While I was deploying the demo, I was also able to remove the XSL dependency. That should definitely help adoption - its been a hurdle in the past.

Currency, Money, and Rails 3.1

By Albert on September 4, 2011 5:11 PM

I recently added a rake task to generate large amounts of random entries so that I could start to test out Regdel in a more real-world scenario.

So far adding a hundred or so entries is fine, but in the process I ran into some minor issues regarding floats. For example, an amount like “$187.67” might display like “187.6700000001”, or something like that.

I had tried to avoid this by storing the entry amount as an integer and overriding the accessors to multiple the amount entered by 100 when writing and dividing by 100 when reading. That worked OK, but when I would sum the amounts, since they were already floats, things would get a bit messed up.

I did some research to find out if there are any tools for Rails 3.1 to handle this type of stuff and thankfully I found the number_to_currency helper. I also discovered that Rails supports decimals as a primitive data type. I decided against that for portability.

Regdel on Ruby on Rails is Coming Right Along

By Albert on August 14, 2011 11:34 AM

I’m really pleased with Ruby on Rails 3.1… its my favorite version of Rails yet! And I’m pleased to report that progress of Regdel upon it is coming along very nicely.

Here are a few parts I’ve been working on lately:

  • A memcached interface for caching certain parts of the logic during development mode. What I’ve done so far won’t be necessary in production as the classes get cached, but I’ll probably add something that will benefit from getting cached in production at some point. (See this interesting post about undefined module / classes when using Memcached).
  • Better user interface! A lot of work was done on the user interface of the Sinatra version of Regdel, and some of that is working now. I’m changing the way entries will get displayed so that they are more contextual, like checks and transfers, and I plan to port the old general journal entry form to the new version, too. I had to make some changes in the regdel.sass file, but nothing major. I actually really like the updated sass syntax.
  • Tests! I actually setup Regdel to be continuously tested with Travis and while I was able to get it to pass last night, I don’t think it will regularly - only because of problems with gems - specifically Rails 3.1 is pulled from github and AFAIK Travis can’t do that yet.
  • Integration with awesome gems! I’m using state_machine, nested_set, and inherited_resources. They are all amazing gems and so far are working quite well with Rails 3.1.
  • Organization of models. Wow - this took a long time, too. Sort of related to the issue I mention with memcached, putting some models in a sub-folder without making them namespaced was a serious PITA. To make it happen, I added this in config/application.rb:
config.autoload_paths += Dir["#{config.root}/app/models/accounts"]

That, plus the loader in the application controller I mention here seem to do the trick.

Remember, at this time, Regdel is seriously unstable. I make changes to existing migrations on the reg, then trash my db, migrate and seed it again, so don’t expect any upgrade compatibility for quite some time.

If you are interested in collaborating, please let me know on gituhub!

Mega Validations, State Machine, and Accounting Tests

By Albert on August 9, 2011 4:34 PM

One of my favorite things about Ruby on Rails is the amazing validation capabilities. So it should come as no surprise that I am leveraging them heavily in Regdel.

For example, I want to make sure that entries balance, but they can’t balance all the time - especially when they are works in progress, i.e. they are not posted. The model has a posted attribute, so I’m doing something like this:

validate :credits_and_debits_must_balance, :if => :posted?

The problem is that the posted attribute must be set before the validation is run. As such, I’m planning on changing the posted attribute to be a part of the state machine setup. I’ll have to create a posted? accessor to check the state, not a problem.

The issue with that is that I’m planning to use the state machine differently for different classes, and the entry model uses single table inheritance (STI). I wonder if its possible to have more than one status column for different states on different classes. Not sure if I want to do that though.

UPDATE: Whoa - looks like state_machine supports namespaces. AWESOME! So for the base Entry class:

  state_machine :initial => :draft do

    event :post do
      transition :draft => :posted
    end 

    event :reconcile do
      transition :posted => :reconciled
    end 

  end 

and for the Invoice class:

  state_machine :initial => :open, :namespace => :invoice_state do

    event :issue do
      transition :matched => :sent
    end 

    event :reconcile do
      transition :posted => :reconciled
    end 
  end 

After reviewing these factors, another great aspect of Rails comes to mind: the testing frameworks!

Regdel on Rails

By Albert on July 12, 2011 6:35 PM

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!

Regdel is Alive!

By Albert on July 2, 2011 11:12 AM

I’m back working on Regdel again - this time on Rails 3!

For the ORM I’m planning on using ActiveRecord, but I’m not crazy about how it implements single table inheritance (STI), but I am very pleased with how it chains scopes together.

Categories