/
internships_controller.rb
220 lines (191 loc) · 8.6 KB
/
internships_controller.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
220
class InternshipsController < ApplicationController
before_filter :set_section
def set_section
@section = 'internship'
end
# GET /internships
# GET /internships.xml
def index
@internships = Internship.all
# Very inefficient when users table has thousands of rows.
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @internships }
format.json {
if params[:filters] != nil
@internships = Internship.joins(:locations, :fields, :languages, :academic_focuses).select("internships.id").group("internships.id")
@internships = @internships.where("internships.for_credit = ?", true) if params[:for_credit] == 'true'
@internships = @internships.where("internships.for_credit = ?", false) if params[:for_credit] == 'false'
@internships = @internships.where("internships.is_part_time = ?", true) if params[:part_time] == 'true'
@internships = @internships.where("internships.is_part_time = ?", false) if params[:part_time] == 'false'
@internships = @internships.where("internships.is_full_time = ?", true) if params[:full_time] == 'true'
@internships = @internships.where("internships.is_full_time = ?", false) if params[:full_time] == 'false'
@internships = @internships.where("internships.requires_us_citizenship = ?", true) if params[:us_citizenship] == 'true'
@internships = @internships.where("internships.requires_us_citizenship = ?", false) if params[:us_citizenship] == 'false'
@internships = @internships.where("internships.is_paid = ?", true) if params[:paid] == 'true'
@internships = @internships.where("internships.is_paid = ?", false) if params[:paid] == 'false'
@internships = @internships.where("languages.id IN (?)", params[:languages].split(',') << Language.find_by_name('unspecified')) if params[:languages] != 'null'
@internships = @internships.where("fields.id IN (?)", params[:fields].split(',') << Field.find_by_name('unspecified')) if params[:fields] != 'null'
@internships = @internships.where("fields.industry_id IN (?)", params[:industries].split(',') << Industry.find_by_name('unspecified')) if params[:industries] != 'null'
@internships = @internships.where("internships.provider_id IN (?)", params[:providers].split(',') << Provider.find_by_name('unspecified')) if params[:providers] != 'null'
@internships = @internships.where("locations.id IN (?)", params[:locations].split(',')) if params[:locations] != 'null'
@internships = @internships.where("academic_focuses.id IN (?)", params[:academic_focuses].split(',') << AcademicFocus.find_by_name('unspecified')) if params[:academic_focuses] != 'null'
end
#Format Response
internships = Hash.new
@internships.each do |internship|
internship = Internship.find(internship.id)
internship.locations.each do |location|
internships[location.country.un_code] = Array.new if internships[location.country.un_code] == nil
internships[location.country.un_code] << {
'id' => internship.id,
'name' => internship.name,
'provider_name' => internship.provider.name,
'city' => location.city,
'state' => location.state.name,
'country' => location.country.name,
}
end
end
#send response
render :json => internships
}
end
end
# GET /internships/1
# GET /internships/1.xml
# GET /internships/1.json
def show
@internship = Internship.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @internship }
format.json {
internship = {
'id' => @internship.id,
'name' => @internship.name,
'is_paid' => @internship.is_paid,
'is_full_time' => @internship.is_full_time,
'is_part_time' => @internship.is_part_time,
'stipend' => @internship.stipend,
'description' => @internship.description,
'qualifications' => @internship.qualifications,
'qualifications_academic' => @internship.qualifications_academic,
'for_credit' => @internship.for_credit,
'requires_us_citizenship' => @internship.requires_us_citizenship,
'application_process' => @internship.application_process,
'deadline' => @internship.deadline,
'academic_contact_name' => false,
'provider_contact_name' => false,
'provider_name' => @internship.provider.name,
'locations' => Array.new,
'academic_focuses' => Array.new,
'fields' => Array.new,
'languages' => Array.new,
'financial_assistance_options' => Array.new,
'semesters' => Array.new
}
#These 2 fields are one or the other, so they are set here on thier own so we avoid calling 'name' on an nil object
internship['academic_contact_name'] = @internship.academic_contact.name if @internship.academic_contact != nil
internship['provider_contact_name'] = @internship.provider_contact.name if @internship.provider_contact != nil
@internship.locations.each do |location|
internship['locations'] << {
'city' => location.city,
'state' => location.state.name,
'country' => location.country.name
}
end
@internship.academic_focuses.each do |academic_focus|
internship['academic_focuses'] << {
'name' => academic_focus.name,
'type' => academic_focus.academic_focus_type.name
}
end
@internship.fields.each do |field|
internship['fields'] << {
'name' => field.name,
'industry' => field.industry.name
}
end
@internship.languages.each do |language|
internship['languages'] << {
'name' => language.name
}
end
@internship.financial_assistance_options.each do |financial_assistance_option|
internship['financial_assistance_options'] << {
'name' => financial_assistance_option.name,
'type' => financial_assistance_option.financial_assistance_option_type.name,
'how_much' => financial_assistance_option.how_much,
'qualifications' => financial_assistance_option.qualifications,
'source' => financial_assistance_option.source,
'website' => financial_assistance_option.website
}
end
@internship.semesters.each do |semester|
internship['semesters'] << {
'name' => semester.name
}
end
render :json => internship
}
end
end
# GET /internships/new
# GET /internships/new.xml
def new
@internship = Internship.new
@internship.user = @current_user if @internship.user == nil
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @internship }
end
end
# GET /internships/1/edit
def edit
@internship = Internship.find(params[:id])
end
# POST /internships
# POST /internships.xml
def create
@internship = Internship.new(params[:internship])
respond_to do |format|
if @internship.save
format.html { redirect_to(@internship, :notice => 'Internship was successfully created.') }
format.xml { render :xml => @internship, :status => :created, :location => @internship }
else
format.html { render :action => "new" }
format.xml { render :xml => @internship.errors, :status => :unprocessable_entity }
end
end
end
# PUT /internships/1
# PUT /internships/1.xml
def update
@internship = Internship.find(params[:id])
respond_to do |format|
if @internship.update_attributes(params[:internship])
format.html { redirect_to(@internship, :notice => 'Internship was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @internship.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /internships/1
# DELETE /internships/1.xml
def destroy
@internship = Internship.find(params[:id])
@internship.destroy
respond_to do |format|
format.html { redirect_to(internships_url) }
format.xml { head :ok }
end
end
def dashboard
respond_to do |format|
format.html # dashboard.html.erb
end
end
end