forked from autolab/Autolab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
association_cache.rb
executable file
·219 lines (177 loc) · 5.8 KB
/
association_cache.rb
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
class AssociationCache
attr_reader :course,
:course_user_data,
:assessments,
:sorted_assessments,
:auds,
:latest_submissions,
:latest_submission_scores,
:assessments_before
def initialize(course)
@course = course
yield self
setup_associations
end
def load_course_user_data(find_options = {})
@course_user_data = {}
@course.course_user_data.where(find_options[:conditions]).each do |cud|
@course_user_data[cud.id] = cud
end
end
def load_assessments(_find_options = {})
@assessments = {}
@sorted_assessments = @course.assessments.ordered.load
setup_assessments_before @sorted_assessments
@sorted_assessments.each do |asmt|
@assessments[asmt.id] = asmt
end
end
# NOTE
# might need to batch this (find_each) at some point.
# as of 4/1/13, faster unbatched.
def load_auds(find_options = {})
@auds = {}
@course.assessment_user_data.where(find_options[:conditions]).each do |aud|
@auds[au_key aud.assessment_id, aud.course_user_datum_id] = aud
end
end
def load_latest_submissions(find_options = {})
@latest_submissions = {}
@course.submissions.latest.where(find_options[:conditions]).each do |sub|
@latest_submissions[au_key sub.assessment_id, sub.course_user_datum_id] = sub
end
end
def load_latest_submission_scores(find_options = {})
@latest_submission_scores = {}
Score.on_latest_submissions.for_course(@course).where(find_options[:conditions]).each do |score|
@latest_submission_scores[score.submission_id] = [] unless @latest_submission_scores[score.submission_id]
@latest_submission_scores[score.submission_id] << score
end
@latest_submission_scores.default = []
end
private
def setup_associations
@assessments.each_value do |asmt|
asmt.association_cache = self
end if @assessments
(@auds.each_value do |aud|
aud.association_cache = self
end) if @auds
(@latest_submissions.each_value do |ls|
ls.association_cache = self
end) if @latest_submissions
@course.association_cache = self
@course_user_data.each_value do |cud|
cud.association_cache = self
end if @course_user_data
end
def setup_assessments_before(sorted_assessments)
@assessments_before = {}
sorted_assessments.each_with_index do |asmt, i|
@assessments_before[asmt.id] = sorted_assessments[i - 1] if i > 0
end
end
end
def au_key(assessment_id, cud_id)
"a_#{assessment_id}/u_#{cud_id}"
end
module CUDAssociationCache
def self.included(base)
base.alias_method_chain :course, :cache
end
def course_with_cache
@course || course_without_cache
end
def association_cache=(cache)
@ass_cache = cache
@course = cache.course if cache.course
end
end
module CourseAssociationCache
def self.included(base)
base.alias_method_chain :course_user_data, :cache
base.alias_method_chain :assessments, :cache
end
def course_user_data_with_cache
@course_user_data || course_user_data_without_cache
end
def assessments_with_cache
@assessments || assessments_without_cache
end
def association_cache=(cache)
@ass_cache = cache
@cuds = cache.course_user_data.values if cache.course_user_data
@assessments = cache.sorted_assessments if cache.sorted_assessments
end
end
module AssessmentAssociationCache
def self.included(base)
base.alias_method_chain :assessment_before, :cache
base.alias_method_chain :aud_for, :cache
base.alias_method_chain :course, :cache
end
def assessment_before_with_cache
asmt_before_cache = @ass_cache && @ass_cache.assessments_before
asmt_before_cache ? asmt_before_cache[id] : assessment_before_without_cache
end
def aud_for_with_cache(cud_id)
aud_cache = @ass_cache && @ass_cache.auds
aud_cache ? aud_cache[au_key id, cud_id] : aud_for_without_cache(cud_id)
end
def course_with_cache
@course || course_without_cache
end
def association_cache=(cache)
@ass_cache = cache
@course = cache.course if cache.course
end
end
module AUDAssociationCache
def self.included(base)
base.alias_method_chain :assessment, :cache
base.alias_method_chain :course_user_datum, :cache
base.alias_method_chain :latest_submission, :cache
end
def latest_submission_with_cache
@latest_submission || latest_submission_without_cache
end
def course_user_datum_with_cache
@cud || course_user_datum_without_cache
end
def assessment_with_cache
@assessment || assessment_without_cache
end
def association_cache=(cache)
@ass_cache = cache
@assessment = cache.assessments[assessment_id] if cache.assessments
@cud = cache.course_user_data[course_user_datum_id] if cache.course_user_data
@latest_submission = cache.latest_submissions[au_key assessment_id, course_user_datum_id] if cache.latest_submissions
end
end
module LatestSubmissionAssociationCache
def self.included(base)
base.alias_method_chain :assessment, :cache
base.alias_method_chain :course_user_datum, :cache
base.alias_method_chain :aud, :cache
base.alias_method_chain :scores, :cache
end
def aud_with_cache
@aud || aud_without_cache
end
def course_user_datum_with_cache
@cud || course_user_datum_without_cache
end
def assessment_with_cache
@assessment || assessment_without_cache
end
def scores_with_cache
@scores || scores_without_cache
end
def association_cache=(cache)
@ass_cache = cache
@assessment = cache.assessments[assessment_id] if cache.assessments
@cud = cache.course_user_data[course_user_datum_id] if cache.course_user_data
@aud = cache.auds[au_key assessment_id, course_user_datum_id] if cache.auds
@scores = cache.latest_submission_scores[id] if cache.latest_submission_scores
end
end