A Rails plugin that allows you to transform your ActiveRecord model's "legacy" attribute methods into attribute aliases that can be used in dynamic finders and attribute hashes.
Switch branches/tags
Nothing to show
Latest commit e880fbd Dec 25, 2009 sshaw sshaw README changes
Failed to load latest commit information.
lib first commit Dec 25, 2009
test first commit Dec 25, 2009
README README changes Dec 25, 2009
Rakefile first commit Dec 25, 2009
init.rb first commit Dec 25, 2009



A Rails plugin that allows you to transform your ActiveRecord model's "legacy" column names 
into conventional attribute aliases and query methods that can then be used in dynamic 
finders and attribute hashes. 

Usefull when you want to follow Rails' naming conventions but your database doesn't.

Your table definitions:

     create table Person (
       FirstName varchar(32),
       LastName varchar(32),
       Birthday date

    create table bills (
      bill_from varchar(32),
      bill_amount decimal(2,2),
      bill_late tinyint

Your model:

    class Person < ActiveRecord::Base
      set_table_name 'Person'
      transform_legacy_attribute_methods #use default = TransformLegacyAttributeMethods.transformer
    class Bill< ActiveRecord::Base
      transform_legacy_attribute_methods(:skip => 'bill_late')  { |name| name.sub /^bill_/, '' } 
Your code:

     person = Person.new( :first_name => 'Mac', :last_name => 'Dre' )
     bill = person.bills.build( :from => 'Coco County', :amount => 35_000.45 ) 

     person = Person.find_by_first_name_and_last_name('Viviane',  'Castro')

     bill.attributes = {
       :from => 'Whole Foods',
       :amount => 300,
       :bill_late => true       

     s = bill.amount_before_type_cast
     bill.pay! if bill.amount?


class YourModel < ActiveRecord::Base
  transform_legacy_attribute_methods(*args, &block)

transform_legacy_attribute_methods requires a transformer to convert your column names. 
A transformer is either a method of the String class -given via a Symbol or String, or a Proc. 

Transformation gives you the following accessors: 

- column= 
- column
- column?
- column_before_type_cast

You can also use column in in dynamic finders and attribute hashes.

A transformer can be specified as the 1st argument or via a block:

transform_legacy_attribute_methods( :titleize ) 
transform_legacy_attribute_methods { |column_name| column_name.underscore }
transform_legacy_attribute_methods( 'downcase' )
transform_legacy_attribute_methods( lambda { |column_name| column_name.chop } )

If no transformer is provided TransformLegacyAttributeMethods.transformer will be used.
By default, this is set to :underscore.

All other arguments are options. Currently the only option is :skip, which specifies 
column(s) that should not be transformed. :skip can be a String, Symbol, or an Array:

transform_legacy_attribute_methods( :skip => %w|columnA columnB columnC| )
transform_legacy_attribute_methods( :titleize, :skip => :some_column )