forked from sshaw/transform_legacy_attribute_methods
-
Notifications
You must be signed in to change notification settings - Fork 1
ZenMilan/transform_legacy_attribute_methods
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
======================== transform_legacy_attribute_methods ======================== 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. ======================== Examples ======================== 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 end class Bill< ActiveRecord::Base transform_legacy_attribute_methods(:skip => 'bill_late') { |name| name.sub /^bill_/, '' } end 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=Bill.new bill.attributes = { :from => 'Whole Foods', :amount => 300, :bill_late => true } s = bill.amount_before_type_cast bill.pay! if bill.amount? ======================== Usage ======================== class YourModel < ActiveRecord::Base transform_legacy_attribute_methods(*args, &block) end 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 )
About
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.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published
Languages
- Ruby 100.0%