-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmodels.rb
More file actions
94 lines (73 loc) · 2.62 KB
/
models.rb
File metadata and controls
94 lines (73 loc) · 2.62 KB
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
class ContactBook < ActiveRecord::Base
has_many :contacts
has_many :contacts_with_scope, ->(_contact_book) { desc }, class_name: "Contact", foreign_key: :contact_book_id
has_many :employees, through: :contacts
has_many :companies
has_many :company_employees, through: :companies, source: :employees
has_many :parents
has_many :children, through: :parents
has_many_aggregate :companies, :count, :count, "*"
has_many_aggregate :companies, :count_with_max_ids_set, :count, "*", max_ids_per_query: 2
has_many_aggregate :employees, :count, :count, "*"
has_many_aggregate :company_employees, :count, :count, "*"
has_many_aggregate :children, :count, :count, "*"
has_many :companies_with_blank_email_address, -> { joins(:email_address).where(email_addresses: { address: "" }) }, class_name: "Company"
has_many_aggregate :companies_with_blank_email_address, :count, :count, "*", table_alias_name: "contacts"
end
class Contact < ActiveRecord::Base
belongs_to :contact_book
belongs_to :contact_owner, polymorphic: true
has_many :addresses
has_many :phone_numbers
has_one :email_address
has_many :employees
has_many_aggregate :addresses, :max_street_length, :maximum, "LENGTH(street)"
has_many_aggregate :phone_numbers, :count, :count, "id"
has_many_aggregate :addresses, :count, :count, "*"
scope :desc, ->{ order(id: :desc) }
end
class Company < Contact
end
class Employee < Contact
belongs_to :contact
end
class ParentsChild < ActiveRecord::Base
belongs_to :parent
belongs_to :child
end
class Parent < Contact
has_many :parents_child
has_many :children, through: :parents_child
end
class Child < Contact
has_many :parents_child
has_many :parents, through: :parents_child
end
class Address < ActiveRecord::Base
belongs_to :contact
belongs_to :country
end
class EmailAddress < ActiveRecord::Base
belongs_to :contact
end
class PhoneNumber < ActiveRecord::Base
belongs_to :contact
end
class Country < ActiveRecord::Base
has_many :addresses
has_many :contacts, through: :addresses
has_many :contact_owners, through: :contacts, source_type: 'ContactOwner'
has_many :states, primary_key: 'name', foreign_key: "region"
has_many_aggregate :contacts, :count, :count, "*"
has_many_aggregate :contact_owners, :count, :count, "*"
has_many_aggregate :states, :count, :count, "*"
end
class State < ActiveRecord::Base
belongs_to :country, foreign_key: 'region'
end
class ContactOwner < ActiveRecord::Base
has_many :contacts, as: :contact_owner
has_many :addresses, through: :contacts
has_many_aggregate :contacts, :count, :count, "*"
has_many_aggregate :addresses, :count, :count, "*"
end