Created
December 14, 2011 16:33
-
-
Save jonleighton/1477319 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
require 'logger' | |
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
# SCHEMA: | |
ActiveRecord::Schema.define do | |
# Unit, Person, Position | |
create_table "business_objects", :id => false, :force => true do |t| | |
t.string "id", :null => false | |
t.string "type", :null => false | |
end | |
# Unit => Position | |
create_table "managements", :force => true do |t| | |
t.string "unit_id", :null => false | |
t.string "position_id", :null => false | |
end | |
# Position => Contract | |
create_table "contract_refers_to_positions", :force => true do |t| | |
t.string "contract_id", :null => false | |
t.string "position_id", :null => false | |
end | |
# Contract => Person | |
create_table "contractings", :force => true do |t| | |
t.string "contract_id", :null => false | |
t.string "person_id", :null => false | |
end | |
# Person properties | |
create_table "person_attributes", :force => true do |t| | |
t.string "person_id", :null => false | |
t.string "first_name", :null => false | |
t.string "last_name", :null => false | |
end | |
end | |
# MODELS: | |
class BusinessObject < ActiveRecord::Base | |
set_primary_key :id | |
end | |
class Unit < BusinessObject | |
has_many :managements, :foreign_key => :unit_id | |
has_many :managers, :through => :managements, :source => :position | |
has_many :managers_contracts, :through => :managers, :source => :contracts | |
has_many :managers_people, :through => :managers_contracts, :source => :people | |
has_many :managers_properties, :through => :managers_people, :source => :properties | |
end | |
class Position < BusinessObject | |
has_many :managements | |
has_many :managed_units, :through => :managements | |
has_many :contract_refers_to_position | |
has_many :contracts, :through => :contract_refers_to_position, :source => :contract | |
end | |
class Contract < BusinessObject | |
has_many :contractings | |
has_many :people, :through => :contractings | |
has_many :contract_refers_to_position | |
has_many :positions, :through => :contract_refers_to_position | |
end | |
class PersonAttribute < ActiveRecord::Base | |
belongs_to :person, :inverse_of => :properties | |
end | |
class Person < BusinessObject | |
has_many :contractings | |
has_many :contracts, :through => :contractings | |
has_many :properties, :class_name => "PersonAttribute" | |
end | |
class Management < ActiveRecord::Base | |
belongs_to :unit | |
belongs_to :position | |
end | |
class ContractRefersToPosition < ActiveRecord::Base | |
belongs_to :position | |
belongs_to :contract | |
end | |
class Contracting < ActiveRecord::Base | |
belongs_to :contract | |
belongs_to :person | |
end | |
# SEEDS: | |
u = Unit.new | |
u.id = "123" | |
u.save! | |
u.managers_properties.to_a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment