Skip to content

Commit 7ba50f7

Browse files
authored
test: inherit models from ApplicationRecord (#433)
1 parent 149d59c commit 7ba50f7

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

spec/support/models.rb

+50-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
class Tag < ActiveRecord::Base
2-
has_closure_tree :dependent => :destroy, :order => :name
1+
# frozen_string_literal: true
2+
3+
class ApplicationRecord < ActiveRecord::Base
4+
self.abstract_class = true
5+
6+
# connects_to database: { writing: :primary, reading: :primary }
7+
end
8+
9+
class SecondDatabaseRecord < ActiveRecord::Base
10+
self.abstract_class = true
11+
12+
# connects_to database: { writing: :secondary, reading: :secondary }
13+
end
14+
class Tag < ApplicationRecord
15+
has_closure_tree dependent: :destroy, order: :name
316
before_destroy :add_destroyed_tag
417

518
def to_s
@@ -8,11 +21,11 @@ def to_s
821

922
def add_destroyed_tag
1023
# Proof for the tests that the destroy rather than the delete method was called:
11-
DestroyedTag.create(:name => name)
24+
DestroyedTag.create(name: to_s)
1225
end
1326
end
1427

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

2942
def add_destroyed_tag
3043
# Proof for the tests that the destroy rather than the delete method was called:
31-
DestroyedTag.create(:name => name)
44+
DestroyedTag.create(name: to_s)
3245
end
3346
end
3447

35-
class DestroyedTag < ActiveRecord::Base
48+
class DestroyedTag < ApplicationRecord
3649
end
3750

38-
class Group < ActiveRecord::Base
51+
class Group < ApplicationRecord
3952
has_closure_tree_root :root_user
4053
end
4154

42-
class Grouping < ActiveRecord::Base
43-
has_closure_tree_root :root_person, class_name: "User", foreign_key: :group_id
55+
class Grouping < ApplicationRecord
56+
has_closure_tree_root :root_person, class_name: 'User', foreign_key: :group_id
4457
end
4558

46-
class UserSet < ActiveRecord::Base
47-
has_closure_tree_root :root_user, class_name: "Useur"
59+
class UserSet < ApplicationRecord
60+
has_closure_tree_root :root_user, class_name: 'Useur'
4861
end
4962

50-
class Team < ActiveRecord::Base
51-
has_closure_tree_root :root_user, class_name: "User", foreign_key: :grp_id
63+
class Team < ApplicationRecord
64+
has_closure_tree_root :root_user, class_name: 'User', foreign_key: :grp_id
5265
end
5366

54-
class User < ActiveRecord::Base
55-
acts_as_tree :parent_column_name => "referrer_id",
56-
:name_column => 'email',
57-
:hierarchy_class_name => 'ReferralHierarchy',
58-
:hierarchy_table_name => 'referral_hierarchies'
67+
class User < ApplicationRecord
68+
acts_as_tree parent_column_name: 'referrer_id',
69+
name_column: 'email',
70+
hierarchy_class_name: 'ReferralHierarchy',
71+
hierarchy_table_name: 'referral_hierarchies'
5972

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

6376
def indirect_contracts
64-
Contract.where(:user_id => descendant_ids)
77+
Contract.where(user_id: descendant_ids)
6578
end
6679

6780
def to_s
6881
email
6982
end
7083
end
7184

72-
class Contract < ActiveRecord::Base
85+
class Contract < ApplicationRecord
7386
belongs_to :user, inverse_of: :contracts
7487
belongs_to :contract_type, inverse_of: :contracts
7588
end
7689

77-
class ContractType < ActiveRecord::Base
90+
class ContractType < ApplicationRecord
7891
has_many :contracts, inverse_of: :contract_type
7992
end
8093

81-
class Label < ActiveRecord::Base
94+
class Label < ApplicationRecord
8295
# make sure order doesn't matter
83-
acts_as_tree :order => :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order"
84-
:numeric_order => true,
85-
:parent_column_name => "mother_id",
86-
:dependent => :destroy
96+
acts_as_tree order: :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order"
97+
numeric_order: true,
98+
parent_column_name: 'mother_id',
99+
dependent: :destroy
87100

88101
def to_s
89102
"#{self.class}: #{name}"
@@ -99,13 +112,13 @@ class DateLabel < Label
99112
class DirectoryLabel < Label
100113
end
101114

102-
class LabelWithoutRootOrdering < ActiveRecord::Base
115+
class LabelWithoutRootOrdering < ApplicationRecord
103116
# make sure order doesn't matter
104-
acts_as_tree :order => :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order"
105-
:numeric_order => true,
106-
:dont_order_roots => true,
107-
:parent_column_name => "mother_id",
108-
:hierarchy_table_name => "label_hierarchies"
117+
acts_as_tree order: :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order"
118+
numeric_order: true,
119+
dont_order_roots: true,
120+
parent_column_name: 'mother_id',
121+
hierarchy_table_name: 'label_hierarchies'
109122

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

@@ -114,20 +127,21 @@ def to_s
114127
end
115128
end
116129

117-
class CuisineType < ActiveRecord::Base
130+
class CuisineType < ApplicationRecord
118131
acts_as_tree
119132
end
120133

121134
module Namespace
122135
def self.table_name_prefix
123136
'namespace_'
124137
end
125-
class Type < ActiveRecord::Base
138+
139+
class Type < ApplicationRecord
126140
has_closure_tree dependent: :destroy
127141
end
128142
end
129143

130-
class Metal < ActiveRecord::Base
144+
class Metal < ApplicationRecord
131145
self.table_name = "#{table_name_prefix}metal#{table_name_suffix}"
132146
has_closure_tree order: 'sort_order', name_column: 'value'
133147
self.inheritance_column = 'metal_type'
@@ -139,6 +153,6 @@ class Adamantium < Metal
139153
class Unobtanium < Metal
140154
end
141155

142-
class MenuItem < ActiveRecord::Base
156+
class MenuItem < SecondDatabaseRecord
143157
has_closure_tree touch: true, with_advisory_lock: false
144158
end

0 commit comments

Comments
 (0)