normalize_it

normalize_it makes it easy to seamlessly manage database tables that have been normalized.

Example:

create_table :customer_statuses, :force => true do |t|
  t.string :customer_status
end

create_table :email_addresses, :force => true do |t|
  t.string :email_address
end

create_table :customers, :force => true do |t|
  t.string  :name
  t.integer :customer_status_id
  t.integer :email_address_id
end

class CustomerStatus < ActiveRecord::Base
  normalizes :customers, :with_field => :customer_status
  validates :customer_status, :presence => true
  validates_uniqueness_of :customer_status
end

class EmailAddress < ActiveRecord::Base
  normalizes :customers, :allow_inserts => true
  validates :email_address, :presence => true
  validates_uniqueness_of :email_address
end

class Customer < ActiveRecord::Base
  has_normalized :customer_status
  has_normalized :email_address, :allow_inserts => true
end

after calling normalizes:
  * if :allow_inserts is false (default), a :with_field argument must be passed in
  ** new objects of CustomerStatus may not be created by the app
  ** CustomerStatus objects may be referenced by [] notation, eg CustomerStatus[:new] or CustomerStatus['new']
  ** CustomerStatus is given a has_many association to Customer, has_many options may be passed in to normalizes
  * if :allow_inserts is true
  ** only the has_many association is set up
  ** [] notation may be used only if the :with_field option is passed

after calling has_normalized:
  * if :allow_inserts is false (default)
  ** customer.customer_status = 'new' will only search for CustomerStatus['new'] and assign it if it is found
  * if :allow_inserts is true
  ** customer.email_address = '[email protected]' will search for the email address, and create it if not found
  * all columns on the normalized tables are delegated to the parent object. e.g. customer.email_address
  * static rails-like finders may be used. e.g. Customer.find_by_email_address '[email protected]'
  * new objects may be initialized either with attributes or through later assignment. e.g. Customer.new(:email_address => '[email protected]')

Contributing to normalize_it

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Dave Havlicek. See LICENSE.txt for further details.