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
|
module DeveloperProjectsAssociationExtension
def find_most_recent
find(:first, :order => "id DESC")
end
end
module DeveloperProjectsAssociationExtension2
def find_least_recent
find(:first, :order => "id ASC")
end
end
class Developer < ActiveRecord::Base
has_and_belongs_to_many :projects do
def find_most_recent
find(:first, :order => "id DESC")
end
end
has_and_belongs_to_many :projects_extended_by_name,
:class_name => "Project",
:join_table => "developers_projects",
:association_foreign_key => "project_id",
:extend => DeveloperProjectsAssociationExtension
has_and_belongs_to_many :projects_extended_by_name_twice,
:class_name => "Project",
:join_table => "developers_projects",
:association_foreign_key => "project_id",
:extend => [DeveloperProjectsAssociationExtension, DeveloperProjectsAssociationExtension2]
has_and_belongs_to_many :projects_extended_by_name_and_block,
:class_name => "Project",
:join_table => "developers_projects",
:association_foreign_key => "project_id",
:extend => DeveloperProjectsAssociationExtension do
def find_least_recent
find(:first, :order => "id ASC")
end
end
has_and_belongs_to_many :special_projects, :join_table => 'developers_projects', :association_foreign_key => 'project_id'
has_many :audit_logs
scope :jamises, :conditions => {: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
def log=(message)
audit_logs.build :message => message
end
def self.all_johns
self.with_exclusive_scope :find => where(:name => 'John') do
self.all
end
end
end
class AuditLog < ActiveRecord::Base
belongs_to :developer, :validate => true
belongs_to :unvalidated_developer, :class_name => 'Developer'
end
DeveloperSalary = Struct.new(:amount)
class DeveloperWithAggregate < ActiveRecord::Base
self.table_name = 'developers'
composed_of :salary, :class_name => 'DeveloperSalary', :mapping => [%w(salary amount)]
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 DeveloperOrderedBySalary < ActiveRecord::Base
self.table_name = 'developers'
def self.default_scope
order('salary DESC')
end
scope :by_name, order('name DESC')
def self.all_ordered_by_name
with_scope(:find => { :order => 'name DESC' }) do
find(:all)
end
end
end
class DeveloperCalledDavid < ActiveRecord::Base
self.table_name = 'developers'
def self.default_scope
where "name = 'David'"
end
end
class DeveloperCalledJamis < ActiveRecord::Base
self.table_name = 'developers'
def self.default_scope
where :name => 'Jamis'
end
scope :poor, where('salary < 150000')
end
class AbstractDeveloperCalledJamis < ActiveRecord::Base
self.abstract_class = true
def self.default_scope
where :name => 'Jamis'
end
end
class PoorDeveloperCalledJamis < ActiveRecord::Base
self.table_name = 'developers'
def self.default_scope
where :name => 'Jamis', :salary => 50000
end
end
class InheritedPoorDeveloperCalledJamis < DeveloperCalledJamis
self.table_name = 'developers'
def self.default_scope
super.where :salary => 50000
end
end
ActiveSupport::Deprecation.silence do
class DeprecatedDeveloperOrderedBySalary < ActiveRecord::Base
self.table_name = 'developers'
default_scope :order => 'salary DESC'
def self.by_name
order('name DESC')
end
def self.all_ordered_by_name
with_scope(:find => { :order => 'name DESC' }) do
find(:all)
end
end
end
class DeprecatedDeveloperCalledDavid < ActiveRecord::Base
self.table_name = 'developers'
default_scope :conditions => "name = 'David'"
end
class DeprecatedDeveloperCalledJamis < ActiveRecord::Base
self.table_name = 'developers'
default_scope :conditions => { :name => 'Jamis' }
end
class DeprecatedPoorDeveloperCalledJamis < ActiveRecord::Base
self.table_name = 'developers'
default_scope :conditions => { :name => 'Jamis', :salary => 50000 }
end
end
|