Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: inherit models from ApplicationRecord #433

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 50 additions & 36 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
class Tag < ActiveRecord::Base
has_closure_tree :dependent => :destroy, :order => :name
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

# connects_to database: { writing: :primary, reading: :primary }
end

class SecondDatabaseRecord < ActiveRecord::Base
self.abstract_class = true

# connects_to database: { writing: :secondary, reading: :secondary }
end
class Tag < ApplicationRecord
has_closure_tree dependent: :destroy, order: :name
before_destroy :add_destroyed_tag

def to_s
Expand All @@ -8,11 +21,11 @@ def to_s

def add_destroyed_tag
# Proof for the tests that the destroy rather than the delete method was called:
DestroyedTag.create(:name => name)
DestroyedTag.create(name: to_s)
end
end

class UUIDTag < ActiveRecord::Base
class UUIDTag < ApplicationRecord
self.primary_key = :uuid
before_create :set_uuid
has_closure_tree dependent: :destroy, order: 'name', parent_column_name: 'parent_uuid'
Expand All @@ -28,62 +41,62 @@ def to_s

def add_destroyed_tag
# Proof for the tests that the destroy rather than the delete method was called:
DestroyedTag.create(:name => name)
DestroyedTag.create(name: to_s)
end
end

class DestroyedTag < ActiveRecord::Base
class DestroyedTag < ApplicationRecord
end

class Group < ActiveRecord::Base
class Group < ApplicationRecord
has_closure_tree_root :root_user
end

class Grouping < ActiveRecord::Base
has_closure_tree_root :root_person, class_name: "User", foreign_key: :group_id
class Grouping < ApplicationRecord
has_closure_tree_root :root_person, class_name: 'User', foreign_key: :group_id
end

class UserSet < ActiveRecord::Base
has_closure_tree_root :root_user, class_name: "Useur"
class UserSet < ApplicationRecord
has_closure_tree_root :root_user, class_name: 'Useur'
end

class Team < ActiveRecord::Base
has_closure_tree_root :root_user, class_name: "User", foreign_key: :grp_id
class Team < ApplicationRecord
has_closure_tree_root :root_user, class_name: 'User', foreign_key: :grp_id
end

class User < ActiveRecord::Base
acts_as_tree :parent_column_name => "referrer_id",
:name_column => 'email',
:hierarchy_class_name => 'ReferralHierarchy',
:hierarchy_table_name => 'referral_hierarchies'
class User < ApplicationRecord
acts_as_tree parent_column_name: 'referrer_id',
name_column: 'email',
hierarchy_class_name: 'ReferralHierarchy',
hierarchy_table_name: 'referral_hierarchies'

has_many :contracts, inverse_of: :user
belongs_to :group # Can't use and don't need inverse_of here when using has_closure_tree_root.

def indirect_contracts
Contract.where(:user_id => descendant_ids)
Contract.where(user_id: descendant_ids)
end

def to_s
email
end
end

class Contract < ActiveRecord::Base
class Contract < ApplicationRecord
belongs_to :user, inverse_of: :contracts
belongs_to :contract_type, inverse_of: :contracts
end

class ContractType < ActiveRecord::Base
class ContractType < ApplicationRecord
has_many :contracts, inverse_of: :contract_type
end

class Label < ActiveRecord::Base
class Label < ApplicationRecord
# make sure order doesn't matter
acts_as_tree :order => :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order"
:numeric_order => true,
:parent_column_name => "mother_id",
:dependent => :destroy
acts_as_tree order: :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order"
numeric_order: true,
parent_column_name: 'mother_id',
dependent: :destroy

def to_s
"#{self.class}: #{name}"
Expand All @@ -99,13 +112,13 @@ class DateLabel < Label
class DirectoryLabel < Label
end

class LabelWithoutRootOrdering < ActiveRecord::Base
class LabelWithoutRootOrdering < ApplicationRecord
# make sure order doesn't matter
acts_as_tree :order => :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order"
:numeric_order => true,
:dont_order_roots => true,
:parent_column_name => "mother_id",
:hierarchy_table_name => "label_hierarchies"
acts_as_tree order: :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order"
numeric_order: true,
dont_order_roots: true,
parent_column_name: 'mother_id',
hierarchy_table_name: 'label_hierarchies'

self.table_name = "#{table_name_prefix}labels#{table_name_suffix}"

Expand All @@ -114,20 +127,21 @@ def to_s
end
end

class CuisineType < ActiveRecord::Base
class CuisineType < ApplicationRecord
acts_as_tree
end

module Namespace
def self.table_name_prefix
'namespace_'
end
class Type < ActiveRecord::Base

class Type < ApplicationRecord
has_closure_tree dependent: :destroy
end
end

class Metal < ActiveRecord::Base
class Metal < ApplicationRecord
self.table_name = "#{table_name_prefix}metal#{table_name_suffix}"
has_closure_tree order: 'sort_order', name_column: 'value'
self.inheritance_column = 'metal_type'
Expand All @@ -139,6 +153,6 @@ class Adamantium < Metal
class Unobtanium < Metal
end

class MenuItem < ActiveRecord::Base
class MenuItem < SecondDatabaseRecord
has_closure_tree touch: true, with_advisory_lock: false
end