2005-11-06 14:05:42 -05:00
|
|
|
module DeveloperProjectsAssociationExtension
|
|
|
|
def find_most_recent
|
|
|
|
find(:first, :order => "id DESC")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-04-10 21:10:42 -04:00
|
|
|
module DeveloperProjectsAssociationExtension2
|
|
|
|
def find_least_recent
|
|
|
|
find(:first, :order => "id ASC")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
class Developer < ActiveRecord::Base
|
2005-11-06 14:05:42 -05:00
|
|
|
has_and_belongs_to_many :projects do
|
2005-11-03 04:06:42 -05:00
|
|
|
def find_most_recent
|
|
|
|
find(:first, :order => "id DESC")
|
|
|
|
end
|
2005-11-06 14:05:42 -05:00
|
|
|
end
|
2008-01-18 02:27:03 -05:00
|
|
|
|
|
|
|
has_and_belongs_to_many :projects_extended_by_name,
|
|
|
|
:class_name => "Project",
|
|
|
|
:join_table => "developers_projects",
|
2005-11-06 14:05:42 -05:00
|
|
|
:association_foreign_key => "project_id",
|
|
|
|
:extend => DeveloperProjectsAssociationExtension
|
2005-11-03 04:06:42 -05:00
|
|
|
|
2008-01-18 02:27:03 -05:00
|
|
|
has_and_belongs_to_many :projects_extended_by_name_twice,
|
|
|
|
:class_name => "Project",
|
|
|
|
:join_table => "developers_projects",
|
2006-04-10 21:10:42 -04:00
|
|
|
:association_foreign_key => "project_id",
|
|
|
|
:extend => [DeveloperProjectsAssociationExtension, DeveloperProjectsAssociationExtension2]
|
|
|
|
|
2008-01-18 02:27:03 -05:00
|
|
|
has_and_belongs_to_many :projects_extended_by_name_and_block,
|
|
|
|
:class_name => "Project",
|
|
|
|
:join_table => "developers_projects",
|
2007-09-17 17:19:44 -04:00
|
|
|
:association_foreign_key => "project_id",
|
|
|
|
:extend => DeveloperProjectsAssociationExtension do
|
|
|
|
def find_least_recent
|
|
|
|
find(:first, :order => "id ASC")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-03 04:52:59 -04:00
|
|
|
has_and_belongs_to_many :special_projects, :join_table => 'developers_projects', :association_foreign_key => 'project_id'
|
2005-10-06 13:13:24 -04:00
|
|
|
|
2007-10-10 02:45:13 -04:00
|
|
|
has_many :audit_logs
|
|
|
|
|
2008-08-21 11:08:42 -04:00
|
|
|
named_scope :jamises, :conditions => {:name => 'Jamis'}
|
|
|
|
|
2004-12-19 06:25:55 -05:00
|
|
|
validates_inclusion_of :salary, :in => 50000..200000
|
|
|
|
validates_length_of :name, :within => 3..20
|
2007-10-10 02:45:13 -04:00
|
|
|
|
|
|
|
before_create do |developer|
|
|
|
|
developer.audit_logs.build :message => "Computer created"
|
|
|
|
end
|
2008-05-08 01:04:53 -04:00
|
|
|
|
|
|
|
def log=(message)
|
|
|
|
audit_logs.build :message => message
|
|
|
|
end
|
2007-10-10 02:45:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class AuditLog < ActiveRecord::Base
|
2008-06-11 07:39:56 -04:00
|
|
|
belongs_to :developer, :validate => true
|
|
|
|
belongs_to :unvalidated_developer, :class_name => 'Developer'
|
2004-12-15 20:32:35 -05:00
|
|
|
end
|
2005-05-19 12:39:50 -04:00
|
|
|
|
|
|
|
DeveloperSalary = Struct.new(:amount)
|
|
|
|
class DeveloperWithAggregate < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
composed_of :salary, :class_name => 'DeveloperSalary', :mapping => [%w(salary amount)]
|
|
|
|
end
|
2005-11-08 05:19:09 -05:00
|
|
|
|
|
|
|
class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
has_and_belongs_to_many :projects, :join_table => 'developers_projects', :foreign_key => 'developer_id'
|
|
|
|
before_destroy :raise_if_projects_empty!
|
|
|
|
|
|
|
|
def raise_if_projects_empty!
|
|
|
|
raise if projects.empty?
|
|
|
|
end
|
|
|
|
end
|
2008-11-16 13:06:41 -05:00
|
|
|
|
|
|
|
class DeveloperOrderedBySalary < ActiveRecord::Base
|
2008-11-17 15:10:59 -05:00
|
|
|
self.table_name = 'developers'
|
|
|
|
default_scope :order => 'salary DESC'
|
|
|
|
named_scope :by_name, :order => 'name DESC'
|
2008-11-16 13:06:41 -05:00
|
|
|
|
2008-11-17 15:10:59 -05:00
|
|
|
def self.all_ordered_by_name
|
|
|
|
with_scope(:find => { :order => 'name DESC' }) do
|
|
|
|
find(:all)
|
2008-11-16 13:06:41 -05:00
|
|
|
end
|
2008-11-17 15:10:59 -05:00
|
|
|
end
|
2008-11-16 13:06:41 -05:00
|
|
|
end
|