2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "ostruct"
|
2011-04-18 18:35:22 -04:00
|
|
|
|
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
|
2015-09-22 14:10:38 -04:00
|
|
|
self.ignored_columns = %w(first_name last_name)
|
|
|
|
|
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
|
|
|
|
2015-04-28 08:51:51 -04:00
|
|
|
belongs_to :mentor
|
|
|
|
|
2014-05-08 01:57:49 -04:00
|
|
|
accepts_nested_attributes_for :projects
|
|
|
|
|
2014-10-08 16:58:11 -04:00
|
|
|
has_and_belongs_to_many :shared_computers, class_name: "Computer"
|
|
|
|
|
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) },
|
2016-08-06 13:37:57 -04:00
|
|
|
class_name: "Project",
|
|
|
|
join_table: "developers_projects",
|
|
|
|
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) },
|
2016-08-06 13:37:57 -04:00
|
|
|
class_name: "Project",
|
|
|
|
join_table: "developers_projects",
|
|
|
|
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) },
|
2016-08-06 13:37:57 -04:00
|
|
|
class_name: "Project",
|
|
|
|
join_table: "developers_projects",
|
|
|
|
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
|
|
|
|
|
2016-08-06 13:37:57 -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,
|
2016-08-06 13:37:57 -04:00
|
|
|
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
|
2016-08-06 13:37:57 -04:00
|
|
|
has_many :firms, through: :contracts, source: :firm
|
2014-07-14 20:25:18 -04:00
|
|
|
has_many :comments, ->(developer) { where(body: "I'm #{developer.name}") }
|
|
|
|
has_many :ratings, through: :comments
|
2015-08-24 04:27:06 -04:00
|
|
|
has_one :ship, dependent: :nullify
|
2007-10-10 02:45:13 -04:00
|
|
|
|
2015-09-26 16:07:24 -04:00
|
|
|
belongs_to :firm
|
|
|
|
has_many :contracted_projects, class_name: "Project"
|
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
scope :jamises, -> { where(name: "Jamis") }
|
2008-08-21 11:08:42 -04:00
|
|
|
|
2016-08-06 13:37:57 -04: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|
|
2016-08-06 13:37:57 -04:00
|
|
|
developer.audit_logs.build message: "Computer created"
|
2007-10-10 02:45:13 -04:00
|
|
|
end
|
2008-05-08 01:04:53 -04:00
|
|
|
|
2015-09-22 14:10:38 -04:00
|
|
|
attr_accessor :last_name
|
2016-08-06 12:26:20 -04:00
|
|
|
define_attribute_method "last_name"
|
2015-09-22 14:10:38 -04:00
|
|
|
|
2008-05-08 01:04:53 -04:00
|
|
|
def log=(message)
|
2016-08-06 13:37:57 -04:00
|
|
|
audit_logs.build message: message
|
2008-05-08 01:04:53 -04:00
|
|
|
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
|
|
|
|
|
2017-10-19 08:07:56 -04:00
|
|
|
class SubDeveloper < Developer
|
|
|
|
end
|
|
|
|
|
2017-10-19 03:30:31 -04:00
|
|
|
class SymbolIgnoredDeveloper < ActiveRecord::Base
|
|
|
|
self.table_name = "developers"
|
|
|
|
self.ignored_columns = [:first_name, :last_name]
|
|
|
|
|
|
|
|
attr_accessor :last_name
|
|
|
|
define_attribute_method "last_name"
|
|
|
|
end
|
|
|
|
|
2007-10-10 02:45:13 -04:00
|
|
|
class AuditLog < ActiveRecord::Base
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -04:00
|
|
|
has_and_belongs_to_many :projects, join_table: "developers_projects", foreign_key: "developer_id"
|
2005-11-08 05:19:09 -05:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -04:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -04:00
|
|
|
has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
|
2013-01-11 12:29:30 -05:00
|
|
|
|
|
|
|
def self.default_scope
|
2016-08-06 13:37:57 -04:00
|
|
|
joins(:projects).where(projects: { name: "Active Controller" })
|
2013-01-11 12:29:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-16 13:06:41 -05:00
|
|
|
class DeveloperOrderedBySalary < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
|
|
|
default_scope { order("salary DESC") }
|
2011-04-03 19:07:45 -04:00
|
|
|
|
2016-08-06 12:26:20 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -04:00
|
|
|
default_scope lambda { where(name: "David") }
|
2011-04-18 18:35:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class LazyBlockDeveloperCalledDavid < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -04:00
|
|
|
default_scope { where(name: "David") }
|
2011-04-18 18:35:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class CallableDeveloperCalledDavid < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -04:00
|
|
|
default_scope OpenStruct.new(call: where(name: "David"))
|
2011-04-18 18:35:22 -04:00
|
|
|
end
|
|
|
|
|
2011-04-18 18:15:38 -04:00
|
|
|
class ClassMethodDeveloperCalledDavid < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2011-04-03 19:07:45 -04:00
|
|
|
|
|
|
|
def self.default_scope
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2011-04-28 12:46:26 -04:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
default_scope { where(name: "Jamis") }
|
2016-08-06 12:26:20 -04:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2011-04-28 12:46:26 -04:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
default_scope -> { where(name: "Jamis", salary: 50000) }
|
2011-04-03 19:07:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class InheritedPoorDeveloperCalledJamis < DeveloperCalledJamis
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2011-04-03 19:07:45 -04:00
|
|
|
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2011-04-28 12:46:26 -04:00
|
|
|
|
2016-08-06 13:37:57 -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
|
|
|
|
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2011-04-28 12:46:26 -04:00
|
|
|
|
|
|
|
include SalaryDefaultScope
|
|
|
|
end
|
|
|
|
|
2011-05-23 22:05:06 -04:00
|
|
|
class EagerDeveloperWithDefaultScope < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2016-08-06 13:37:57 -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
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
default_scope OpenStruct.new(call: includes(:projects))
|
2011-05-23 22:05:06 -04:00
|
|
|
end
|
2011-08-13 11:37:44 -04:00
|
|
|
|
|
|
|
class ThreadsafeDeveloper < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "developers"
|
2011-08-13 11:37:44 -04:00
|
|
|
|
|
|
|
def self.default_scope
|
2017-01-01 13:34:17 -05:00
|
|
|
Thread.current[:default_scope_delay].call
|
2011-08-13 11:37:44 -04:00
|
|
|
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
|
2016-12-28 14:15:49 -05:00
|
|
|
|
|
|
|
class DeveloperWithIncorrectlyOrderedHasManyThrough < ActiveRecord::Base
|
|
|
|
self.table_name = "developers"
|
|
|
|
has_many :companies, through: :contracts
|
|
|
|
has_many :contracts, foreign_key: :developer_id
|
|
|
|
end
|