forked from evenwestvang/skoolgate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.rb
140 lines (112 loc) · 3.49 KB
/
models.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
# db.municipalities.ensureIndex({location : "2d"})
class County
include Mongoid::Document
field :name
field :link_name, :type => String
field :result_average, :type => Float
field :location, :type => Array
references_many :municipalities, :index => true
references_many :schools, :index => true
index :name
index :link_name
index [[ :location, Mongo::GEO2D ]]
after_create :print_tick
def print_tick
print "C "
end
end
class Municipality
include Mongoid::Document
field :name
field :link_name, :type => String
field :student_body_count, :type => Integer
field :result_average, :type => Float
field :location, :type => Array
field :year_averages, :type => Hash
index :name
index :link_name
index [[ :location, Mongo::GEO2D ]]
referenced_in :county
references_many :schools, :index => true
scope :by_county, lambda { |county| { :where => { :county_id => county.id } } }
after_create :add_to_counties
def add_to_counties
self.county.municipalities << self
self.county.save!
print "M "
end
end
class School
include Mongoid::Document
field :address, :type => String
field :name, :type => String
field :link_name, :type => String
field :student_body_count, :type => Integer
field :location, :type => Array # latitude longitude
field :result_average, :type => Float
field :year_averages, :type => Hash
referenced_in :county
referenced_in :municipality
# embeds
embeds_many :annual_results
index :name
index :link_name
scope :by_county, lambda { |county| { :where => { :county_id => county.id } } }
scope :by_municipality, lambda { |municipality| { :where => { :municipality_id => municipality.id }}}
scope :school_name, lambda { |school_name| { :where => {"annual_results.school_name" => school_name }}}
scope :in_year, lambda { |year| { :where => {"annual_results.year" => year } } }
scope :not_in_year, lambda { |year| { :excludes => {"annual_results.year" => year } } }
after_create :add_to_municipalities
def find_school_names
self.annual_results.map(&:school_name).uniq
end
def find_school_name
self.annual_results.school_name
end
def add_to_municipalities
self.municipality.schools << self
self.municipality.save!
self.county.schools << self
self.county.save!
end
def add_annual_result year, school_name, subject = nil
if subject.result.nil?
print
return
end
annual_result = self.annual_results.in_year(year).first
if annual_result.nil?
annual_result = AnnualResult.new(:year => year, :school_name => school_name)
self.annual_results << annual_result
print year.to_s[-1]
end
annual_result.subjects << subject if subject
self.save!
return annual_result
end
end
class AnnualResult
include Mongoid::Document
field :school_name, :type => String
field :year, :type => Integer
field :result_average, :type => Float
# embeds
embeds_many :subjects
embedded_in :school, :inverse_of => :annual_results
scope :in_year, lambda { |year| { :where => { :year => year } } }
validates_presence_of :year
validates_presence_of :school_name
end
class Subject
include Mongoid::Document
field :school_year, :type => Integer
field :test_code, :type => String
field :result, :type => Float
field :normalized_result, :type => Float
# embeds
embedded_in :annual_result, :inverse_of => :subjects
# validations
validates_presence_of :school_year
validates_presence_of :test_code
validates_presence_of :result
end