2011-04-18 18:35:22 -04:00
|
|
|
require 'ostruct'
|
|
|
|
|
2006-04-10 21:10:42 -04:00
|
|
|
module DeveloperProjectsAssociationExtension2
|
|
|
|
def find_least_recent
|
2012-07-27 12:27:47 -04:00
|
|
|
order("id ASC").first
|
2006-04-10 21:10:42 -04:00
|
|
|
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
|
2012-07-27 12:27:47 -04:00
|
|
|
order("id DESC").first
|
2005-11-03 04:06:42 -05:00
|
|
|
end
|
2005-11-06 14:05:42 -05:00
|
|
|
end
|
2008-01-18 02:27:03 -05:00
|
|
|
|
2014-05-08 01:57:49 -04:00
|
|
|
accepts_nested_attributes_for :projects
|
|
|
|
|
2008-01-18 02:27:03 -05:00
|
|
|
has_and_belongs_to_many :projects_extended_by_name,
|
2012-07-13 14:34:40 -04:00
|
|
|
-> { extending(DeveloperProjectsAssociationExtension) },
|
2008-01-18 02:27:03 -05:00
|
|
|
:class_name => "Project",
|
|
|
|
:join_table => "developers_projects",
|
2012-07-13 14:34:40 -04:00
|
|
|
:association_foreign_key => "project_id"
|
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,
|
2012-07-13 14:34:40 -04:00
|
|
|
-> { extending(DeveloperProjectsAssociationExtension, DeveloperProjectsAssociationExtension2) },
|
2008-01-18 02:27:03 -05:00
|
|
|
:class_name => "Project",
|
|
|
|
:join_table => "developers_projects",
|
2012-07-13 14:34:40 -04:00
|
|
|
:association_foreign_key => "project_id"
|
2006-04-10 21:10:42 -04:00
|
|
|
|
2008-01-18 02:27:03 -05:00
|
|
|
has_and_belongs_to_many :projects_extended_by_name_and_block,
|
2012-07-13 14:34:40 -04:00
|
|
|
-> { extending(DeveloperProjectsAssociationExtension) },
|
2008-01-18 02:27:03 -05:00
|
|
|
:class_name => "Project",
|
|
|
|
:join_table => "developers_projects",
|
2012-07-13 14:34:40 -04:00
|
|
|
:association_foreign_key => "project_id" do
|
2007-09-17 17:19:44 -04:00
|
|
|
def find_least_recent
|
2012-07-27 12:27:47 -04:00
|
|
|
order("id ASC").first
|
2007-09-17 17:19:44 -04:00
|
|
|
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'
|
2013-12-12 13:43:36 -05:00
|
|
|
has_and_belongs_to_many :sym_special_projects,
|
|
|
|
:join_table => :developers_projects,
|
|
|
|
:association_foreign_key => 'project_id',
|
|
|
|
:class_name => 'SpecialProject'
|
2005-10-06 13:13:24 -04:00
|
|
|
|
2007-10-10 02:45:13 -04:00
|
|
|
has_many :audit_logs
|
2013-09-27 19:56:49 -04:00
|
|
|
has_many :contracts
|
|
|
|
has_many :firms, :through => :contracts, :source => :firm
|
2007-10-10 02:45:13 -04:00
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :jamises, -> { where(:name => 'Jamis') }
|
2008-08-21 11:08:42 -04:00
|
|
|
|
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
|
2012-12-02 15:24:47 -05:00
|
|
|
|
|
|
|
after_find :track_instance_count
|
|
|
|
cattr_accessor :instance_count
|
|
|
|
|
|
|
|
def track_instance_count
|
|
|
|
self.class.instance_count ||= 0
|
|
|
|
self.class.instance_count += 1
|
|
|
|
end
|
|
|
|
private :track_instance_count
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2011-05-29 11:38:01 -04:00
|
|
|
class DeveloperWithSelect < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { select('name') }
|
2011-05-29 11:38:01 -04:00
|
|
|
end
|
|
|
|
|
2011-06-23 14:57:49 -04:00
|
|
|
class DeveloperWithIncludes < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
has_many :audit_logs, :foreign_key => :developer_id
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { includes(:audit_logs) }
|
2011-06-23 14:57:49 -04:00
|
|
|
end
|
|
|
|
|
2013-01-11 12:29:30 -05:00
|
|
|
class DeveloperFilteredOnJoins < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
|
|
|
|
|
|
|
|
def self.default_scope
|
|
|
|
joins(:projects).where(:projects => { :name => 'Active Controller' })
|
|
|
|
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'
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { order('salary DESC') }
|
2011-04-03 19:07:45 -04:00
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :by_name, -> { order('name DESC') }
|
2008-11-16 13:06:41 -05:00
|
|
|
end
|
2009-05-18 15:27:42 -04:00
|
|
|
|
|
|
|
class DeveloperCalledDavid < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { where("name = 'David'") }
|
2009-05-18 15:27:42 -04:00
|
|
|
end
|
|
|
|
|
2011-04-18 18:35:22 -04:00
|
|
|
class LazyLambdaDeveloperCalledDavid < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
default_scope lambda { where(:name => 'David') }
|
|
|
|
end
|
|
|
|
|
|
|
|
class LazyBlockDeveloperCalledDavid < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
default_scope { where(:name => 'David') }
|
|
|
|
end
|
|
|
|
|
|
|
|
class CallableDeveloperCalledDavid < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
default_scope OpenStruct.new(:call => where(:name => 'David'))
|
|
|
|
end
|
|
|
|
|
2011-04-18 18:15:38 -04:00
|
|
|
class ClassMethodDeveloperCalledDavid < ActiveRecord::Base
|
2009-05-18 15:27:42 -04:00
|
|
|
self.table_name = 'developers'
|
2011-04-03 19:07:45 -04:00
|
|
|
|
|
|
|
def self.default_scope
|
2011-04-18 18:15:38 -04:00
|
|
|
where(:name => 'David')
|
2011-04-03 19:07:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-24 19:07:38 -04:00
|
|
|
class ClassMethodReferencingScopeDeveloperCalledDavid < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :david, -> { where(:name => 'David') }
|
2011-05-24 19:07:38 -04:00
|
|
|
|
|
|
|
def self.default_scope
|
|
|
|
david
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class LazyBlockReferencingScopeDeveloperCalledDavid < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :david, -> { where(:name => 'David') }
|
2011-05-24 19:07:38 -04:00
|
|
|
default_scope { david }
|
|
|
|
end
|
|
|
|
|
2011-04-18 18:15:38 -04:00
|
|
|
class DeveloperCalledJamis < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2011-04-28 12:46:26 -04:00
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { where(:name => 'Jamis') }
|
|
|
|
scope :poor, -> { where('salary < 150000') }
|
2014-02-23 06:14:05 -05:00
|
|
|
scope :david, -> { where name: "David" }
|
|
|
|
scope :david2, -> { unscoped.where name: "David" }
|
2009-05-18 15:27:42 -04:00
|
|
|
end
|
2010-02-26 05:06:55 -05:00
|
|
|
|
|
|
|
class PoorDeveloperCalledJamis < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2011-04-28 12:46:26 -04:00
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope -> { where(:name => 'Jamis', :salary => 50000) }
|
2011-04-03 19:07:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class InheritedPoorDeveloperCalledJamis < DeveloperCalledJamis
|
|
|
|
self.table_name = 'developers'
|
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope -> { where(:salary => 50000) }
|
2011-04-03 19:07:45 -04:00
|
|
|
end
|
|
|
|
|
2011-04-18 18:15:38 -04:00
|
|
|
class MultiplePoorDeveloperCalledJamis < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2011-04-28 12:46:26 -04:00
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope -> { where(:name => 'Jamis') }
|
|
|
|
default_scope -> { where(:salary => 50000) }
|
2011-04-28 12:46:26 -04:00
|
|
|
end
|
2011-04-03 19:07:45 -04:00
|
|
|
|
2011-04-28 12:46:26 -04:00
|
|
|
module SalaryDefaultScope
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
included { default_scope { where(:salary => 50000) } }
|
2010-06-28 17:17:56 -04:00
|
|
|
end
|
2011-04-28 12:46:26 -04:00
|
|
|
|
|
|
|
class ModuleIncludedPoorDeveloperCalledJamis < DeveloperCalledJamis
|
|
|
|
self.table_name = 'developers'
|
|
|
|
|
|
|
|
include SalaryDefaultScope
|
|
|
|
end
|
|
|
|
|
2011-05-23 22:05:06 -04:00
|
|
|
class EagerDeveloperWithDefaultScope < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2012-07-13 14:34:40 -04:00
|
|
|
has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
|
2011-05-23 22:05:06 -04:00
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { includes(:projects) }
|
2011-05-23 22:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class EagerDeveloperWithClassMethodDefaultScope < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2012-07-13 14:34:40 -04:00
|
|
|
has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
|
2011-05-23 22:05:06 -04:00
|
|
|
|
|
|
|
def self.default_scope
|
|
|
|
includes(:projects)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class EagerDeveloperWithLambdaDefaultScope < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2012-07-13 14:34:40 -04:00
|
|
|
has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
|
2011-05-23 22:05:06 -04:00
|
|
|
|
|
|
|
default_scope lambda { includes(:projects) }
|
|
|
|
end
|
|
|
|
|
|
|
|
class EagerDeveloperWithBlockDefaultScope < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2012-07-13 14:34:40 -04:00
|
|
|
has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
|
2011-04-28 12:46:26 -04:00
|
|
|
|
2011-05-23 22:05:06 -04:00
|
|
|
default_scope { includes(:projects) }
|
|
|
|
end
|
|
|
|
|
|
|
|
class EagerDeveloperWithCallableDefaultScope < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
2012-07-13 14:34:40 -04:00
|
|
|
has_and_belongs_to_many :projects, -> { order('projects.id') }, :foreign_key => 'developer_id', :join_table => 'developers_projects'
|
2011-05-23 22:05:06 -04:00
|
|
|
|
|
|
|
default_scope OpenStruct.new(:call => includes(:projects))
|
|
|
|
end
|
2011-08-13 11:37:44 -04:00
|
|
|
|
|
|
|
class ThreadsafeDeveloper < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
|
|
|
|
def self.default_scope
|
|
|
|
sleep 0.05 if Thread.current[:long_default_scope]
|
|
|
|
limit(1)
|
|
|
|
end
|
|
|
|
end
|
2012-12-10 14:22:30 -05:00
|
|
|
|
|
|
|
class CachedDeveloper < ActiveRecord::Base
|
|
|
|
self.table_name = "developers"
|
|
|
|
self.cache_timestamp_format = :number
|
|
|
|
end
|