1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
# frozen_string_literal: true
require "models/computer"
class Developer < ActiveRecord::Base
module TimestampAliases
extend ActiveSupport::Concern
included do
alias_attribute :created_at, :legacy_created_at
alias_attribute :updated_at, :legacy_updated_at
alias_attribute :created_on, :legacy_created_on
alias_attribute :updated_on, :legacy_updated_on
end
end
include TimestampAliases
module ProjectsAssociationExtension2
def find_least_recent
order("id ASC").first
end
end
self.ignored_columns = %w(first_name last_name)
has_and_belongs_to_many :projects do
def find_most_recent
order("id DESC").first
end
end
def self.target
:__target__ # Used by delegation_test.rb
end
belongs_to :mentor
belongs_to :strict_loading_mentor, strict_loading: true, foreign_key: :mentor_id, class_name: "Mentor"
belongs_to :strict_loading_off_mentor, strict_loading: false, foreign_key: :mentor_id, class_name: "Mentor"
accepts_nested_attributes_for :projects
has_and_belongs_to_many :shared_computers, class_name: "Computer"
has_many :computers, foreign_key: :developer
has_and_belongs_to_many :projects_extended_by_name,
-> { extending(ProjectsAssociationExtension) },
class_name: "Project",
join_table: "developers_projects",
association_foreign_key: "project_id"
has_and_belongs_to_many :projects_extended_by_name_twice,
-> { extending(ProjectsAssociationExtension, ProjectsAssociationExtension2) },
class_name: "Project",
join_table: "developers_projects",
association_foreign_key: "project_id"
has_and_belongs_to_many :projects_extended_by_name_and_block,
-> { extending(ProjectsAssociationExtension) },
class_name: "Project",
join_table: "developers_projects",
association_foreign_key: "project_id" do
def find_least_recent
order("id ASC").first
end
end
has_and_belongs_to_many :strict_loading_projects,
join_table: :developers_projects,
association_foreign_key: :project_id,
class_name: "Project",
strict_loading: true
has_and_belongs_to_many :special_projects, join_table: "developers_projects", association_foreign_key: "project_id"
has_and_belongs_to_many :sym_special_projects,
join_table: :developers_projects,
association_foreign_key: "project_id",
class_name: "SpecialProject"
has_many :audit_logs
has_many :required_audit_logs, class_name: "AuditLogRequired"
has_many :strict_loading_audit_logs, -> { strict_loading }, class_name: "AuditLog"
has_many :strict_loading_opt_audit_logs, strict_loading: true, class_name: "AuditLog"
has_many :contracts
has_many :firms, through: :contracts, source: :firm
has_many :comments, ->(developer) { where(body: "I'm #{developer.name}") }
has_many :ratings, through: :comments
has_one :ship, dependent: :nullify
has_one :strict_loading_ship, strict_loading: true, class_name: "Ship"
belongs_to :firm
has_many :contracted_projects, class_name: "Project"
scope :jamises, -> { where(name: "Jamis") }
validates_inclusion_of :salary, in: 50000..200000
validates_length_of :name, within: 3..20
before_create do |developer|
developer.audit_logs.build message: "Computer created"
end
attribute :last_name
def log=(message)
audit_logs.build message: message
end
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
end
class SubDeveloper < Developer
end
class SpecialDeveloper < ActiveRecord::Base
self.table_name = "developers"
has_many :special_contracts, foreign_key: "developer_id"
end
class SymbolIgnoredDeveloper < ActiveRecord::Base
self.table_name = "developers"
self.ignored_columns = [:first_name, :last_name]
attribute :last_name
end
class AuditLog < ActiveRecord::Base
belongs_to :developer, validate: true
belongs_to :unvalidated_developer, class_name: "Developer"
self.attributes_for_inspect = [:id, :message]
end
class AuditLogRequired < ActiveRecord::Base
self.table_name = "audit_logs"
belongs_to :developer, required: true
end
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
class DeveloperWithSelect < ActiveRecord::Base
self.table_name = "developers"
default_scope { select("name") }
end
class DeveloperwithDefaultMentorScopeNot < ActiveRecord::Base
self.table_name = "developers"
default_scope -> { where(mentor_id: 1) }
end
class DeveloperWithDefaultMentorScopeAllQueries < ActiveRecord::Base
self.table_name = "developers"
default_scope -> { where(mentor_id: 1) }, all_queries: true
end
class DeveloperWithDefaultNilableFirmScopeAllQueries < ActiveRecord::Base
self.table_name = "developers"
firm_id = nil # Could be something like Current.firm_id
default_scope -> { where(firm_id: firm_id) if firm_id }, all_queries: true
end
module MentorDefaultScopeNotAllQueries
extend ActiveSupport::Concern
included { default_scope { where(mentor_id: 1) } }
end
class DeveloperWithIncludedMentorDefaultScopeNotAllQueriesAndDefaultScopeFirmWithAllQueries < ActiveRecord::Base
include MentorDefaultScopeNotAllQueries
self.table_name = "developers"
firm_id = 10 # Could be something like Current.firm_id
default_scope -> { where(firm_id: firm_id) if firm_id }, all_queries: true
end
class DeveloperWithIncludes < ActiveRecord::Base
self.table_name = "developers"
has_many :audit_logs, foreign_key: :developer_id
default_scope { includes(:audit_logs) }
end
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
class DeveloperOrderedBySalary < ActiveRecord::Base
include Developer::TimestampAliases
self.table_name = "developers"
default_scope { order("salary DESC") }
scope :by_name, -> { order("name DESC") }
end
class DeveloperCalledDavid < ActiveRecord::Base
self.table_name = "developers"
default_scope { where("name = 'David'") }
end
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 Struct.new(:call).new(where(name: "David"))
end
class ClassMethodDeveloperCalledDavid < ActiveRecord::Base
self.table_name = "developers"
def self.default_scope
where(name: "David")
end
end
class ClassMethodReferencingScopeDeveloperCalledDavid < ActiveRecord::Base
self.table_name = "developers"
scope :david, -> { where(name: "David") }
def self.default_scope
david
end
end
class LazyBlockReferencingScopeDeveloperCalledDavid < ActiveRecord::Base
self.table_name = "developers"
scope :david, -> { where(name: "David") }
default_scope { david }
end
class DeveloperCalledJamis < ActiveRecord::Base
include Developer::TimestampAliases
self.table_name = "developers"
default_scope { where(name: "Jamis") }
scope :poor, -> { where("salary < 150000") }
scope :david, -> { where name: "David" }
scope :david2, -> { unscoped.where name: "David" }
end
class PoorDeveloperCalledJamis < ActiveRecord::Base
self.table_name = "developers"
default_scope -> { where(name: "Jamis", salary: 50000) }
end
class InheritedPoorDeveloperCalledJamis < DeveloperCalledJamis
self.table_name = "developers"
default_scope -> { where(salary: 50000) }
end
class MultiplePoorDeveloperCalledJamis < ActiveRecord::Base
self.table_name = "developers"
default_scope { }
default_scope -> { where(name: "Jamis") }
default_scope -> { where(salary: 50000) }
end
module SalaryDefaultScope
extend ActiveSupport::Concern
included { default_scope { where(salary: 50000) } }
end
class ModuleIncludedPoorDeveloperCalledJamis < DeveloperCalledJamis
self.table_name = "developers"
include SalaryDefaultScope
end
class EagerDeveloperWithDefaultScope < ActiveRecord::Base
self.table_name = "developers"
has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
default_scope { includes(:projects) }
end
class EagerDeveloperWithClassMethodDefaultScope < 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
includes(:projects)
end
end
class EagerDeveloperWithLambdaDefaultScope < ActiveRecord::Base
self.table_name = "developers"
has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
default_scope lambda { includes(:projects) }
end
class EagerDeveloperWithBlockDefaultScope < ActiveRecord::Base
self.table_name = "developers"
has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
default_scope { includes(:projects) }
end
class EagerDeveloperWithCallableDefaultScope < ActiveRecord::Base
self.table_name = "developers"
has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
default_scope Struct.new(:call).new(includes(:projects))
end
class ThreadsafeDeveloper < ActiveRecord::Base
self.table_name = "developers"
def self.default_scope
Thread.current[:default_scope_delay].call
limit(1)
end
end
class CachedDeveloper < ActiveRecord::Base
include Developer::TimestampAliases
self.table_name = "developers"
self.cache_timestamp_format = :number
end
class DeveloperWithIncorrectlyOrderedHasManyThrough < ActiveRecord::Base
self.table_name = "developers"
has_many :companies, through: :contracts
has_many :contracts, foreign_key: :developer_id
end
class DeveloperName < ActiveRecord::Type::String
def deserialize(value)
"Developer: #{value}"
end
end
class AttributedDeveloper < ActiveRecord::Base
self.table_name = "developers"
attribute :name, DeveloperName.new
self.ignored_columns += ["name"]
end
class ColumnNamesCachedDeveloper < ActiveRecord::Base
self.table_name = "developers"
self.ignored_columns += ["name"] if column_names.include?("name")
end
class AuditRequiredDeveloper < ActiveRecord::Base
self.table_name = "developers"
has_many :required_audit_logs, class_name: "AuditLogRequired"
end
|