-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathjobs_controller.rb
More file actions
346 lines (285 loc) · 10.7 KB
/
jobs_controller.rb
File metadata and controls
346 lines (285 loc) · 10.7 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
class JobsController < ApplicationController
# GET /jobs
# GET /jobs.xml
include CASControllerIncludes
include AttribsHelper
# skip_before_filter :verify_authenticity_token, :only => [:auto_complete_for_category_name,
# :auto_complete_for_course_name, :auto_complete_for_proglang_name]
# auto_complete_for :category, :name
# auto_complete_for :course, :name
# auto_complete_for :proglang, :name
#CalNet / CAS Authentication
#before_filter CASClient::Frameworks::Rails::Filter
before_filter :goto_home_unless_logged_in
# Ensures that only logged-in users can create, edit, or delete jobs
before_filter :rm_login_required #, :except => [ :index, :show ]
# Ensures that only the user who created a job -- and no other users -- can edit
# or destroy it.
before_filter :correct_user_access, :only => [ :edit, :update, :resend_activation_email,
:delete, :destroy ]
before_filter :job_accessible, :only => [ :show, :apply ]
# Prohibits a user from watching his/her own job
before_filter :watch_apply_ok_for_job, :only => [ :watch ]
protected
def search_params_hash
h = {}
# booleans
#h[:include_ended] = params[:include_ended] if ActiveRecord::ConnectionAdapters::Column.value_to_boolean(params[:include_ended]) #unless params[param].nil?
# strings, directly copy attribs
[:query, :tags, :page, :per_page, :as, :compensation].each do |param|
h[param] = params[param] unless params[param].blank?
end
# dept. 0 => all
h[:post_status] = params[:post_status] if params[:post_status]
h[:department] = params[:department] if params[:department].to_i > 0
h[:faculty] = params[:faculty] if params[:faculty].to_i > 0
h
end
public
def index #list
# strip out some weird args
# may cause double-request but that's okay
redirect_to(search_params_hash) and return if [:commit, :utf8].any? {|k| !params[k].nil?}
# Advanced search
query_parms = {}
query_parms[:department_id] = params[:department].to_i if params[:department] && params[:department].to_i > 0
query_parms[:faculty_id ] = params[:faculty].to_i if params[:faculty] && params[:faculty].to_i > 0
#query_parms[:include_ended] = ActiveRecord::ConnectionAdapters::Column.value_to_boolean(params[:include_ended])
query_parms[:compensation ] = params[:compensation] if params[:compensation].present?
query_parms[:tags ] = params[:tags] if params[:tags].present?
# will_paginate
query_parms[:page ] = params[:page] || 1
query_parms[:per_page ] = params[:per_page] || 10
@query = params[:query] || ''
@jobs = Job.find_jobs(@query, query_parms)
# Set some view props
@department_id = params[:department] ? params[:department].to_i : 0
@faculty_id = params[:faculty] ? params[:faculty].to_i : 0
@compensation = params[:compensation]
respond_to do |format|
format.html { render :action => :index }
format.xml { render :xml => @jobs }
end
end
# GET /jobs/1
# GET /jobs/1.xml
def show
@job = Job.find(params[:id])
@actions = @job.actions(@current_user)
@curations = @job.curations(@current_user)
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @job }
end
end
# GET /jobs/new
# GET /jobs/new.xml
def new
@job = Job.new(num_positions: 0)
@faculty = Faculty.all
# @faculty = Faculty.where("email != ? OR email != ?", "None", "nil")
@current_owners = @job.owners.select{|i| i != @current_user}
owners = @job.owners + [@job.user]
@owners_list = User.all.select{|i| !(owners).include?(i)}.sort_by{|u| u.name}
end
# GET /jobs/1/edit
def edit
@job = Job.find(params[:id])
@job.mend
@faculty = Faculty.all
@current_owners = @job.owners.select{|i| i != @current_user}
owners = @job.owners + [@job.user]
@owners_list = User.all.sort_by{|u| u.name}
respond_to do |format|
format.html
format.xml
end
end
def resend_activation_email
@job = Job.find(params[:id])
@job.resend_email(true)
flash[:notice] = 'Thank you. The activation email for this listing has '
flash[:notice] << 'been re-sent to its faculty sponsors.'
respond_to do |format|
format.html { redirect_to(@job) }
end
end
# POST /jobs
# POST /jobs.xml
def create
@faculty = Faculty.all # used in form
sponsor = Faculty.find(params[:faculty_id]) rescue nil
@job = Job.create(job_params)
@job.update(primary_contact_id: @current_user.id)
@current_owners = @job.owners.select{|i| i != @current_user}
owners = @job.owners + [@job.user]
@owners_list = User.all.select{|i| !(owners).include?(i)}.sort_by{|u| u.name}
@job.tag_list = @job.field_list
respond_to do |format|
if @job.save
if sponsor
@sponsorship = Sponsorship.find_or_create_by(faculty_id: sponsor.id, job_id: @job.id)
@job.sponsorships << @sponsorship
end
flash[:notice] = 'Thank your for submitting a listing. It should now be available for other people to browse.'
format.html { redirect_to(@job) }
format.xml { render :xml => @job, :status => :created, :location => @job }
else
@faculty_id = params[:faculty_id]
format.html { render 'new' }
format.xml { render :xml => @job.errors, :status => :unprocessable_entity }
end
end
end
# PUT /jobs/1
# PUT /jobs/1.xml
def update
job_params
@job = Job.find(params[:id])
changed_sponsors = update_sponsorships and false # TODO: remove when :active is resolved
@job.update_attribs(params)
@faculty = Faculty.all
@current_owners = @job.owners.select{|i| i != @current_user}
owners = @job.owners + [@job.user]
@owners_list = User.all.sort_by{|u| u.name}
respond_to do |format|
if @job.update_attributes(params[:job])
if params.has_key?(:delete_owners) and params[:delete_owners].to_i >= 0
@job.owners.delete(User.find(params[:delete_owners]))
end
@job.tag_list = @job.field_list
# If the faculty sponsor changed, require activation again.
# (require the faculty to confirm again)
if changed_sponsors
@job.resend_email(true) # sends the email too
end
flash[:notice] = 'Listing was successfully updated.'
if params[:open_ended_end_date] == "true"
@job.end_date = nil
end
@job.save
format.html { redirect_to(@job) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @job.errors, :status => :unprocessable_entity }
end
end
end
# Just the page that asks for confirmation for deletion of the job.
# The actual deletion is performed by the "destroy" action.
def delete
@job = Job.find(params[:id])
respond_to do |format|
format.html
format.xml
end
end
# DELETE /jobs/1
# DELETE /jobs/1.xml
def destroy
@job = Job.find(params[:id])
@job.destroy
respond_to do |format|
flash[:notice] = "Listing deleted successfully."
format.html { redirect_to(jobs_url) }
format.xml { head :ok }
end
end
def activate
# /jobs/activate/job_id?a=xxx
@job = Job.find :first, conditions: { activation_code: params[:a] }
unless @job
flash[:error] = 'Unable to process activation request.'
return redirect_to jobs_url
end
@job.populate_tag_list
@job.skip_handlers = true
unless @job.save
flash[:error] = 'Unsuccessful activation. Please contact us if the problem persists.'
return redirect_to(jobs_url)
end
@job.skip_handlers = false
flash[:notice] = 'Listing activated successfully. Your listing is now available to be viewed by other users.'
redirect_to @job
end
def job_read_more
job = Job.find(params[:id])
render :plain => job.desc
end
def job_read_less
job = Job.find(params[:id])
desc = job.desc.first(100)
desc = desc << "..." if job.desc.length > 100
render :plain => desc
end
def watch
job = Job.find(params[:id])
watch = Watch.new({:user=> @current_user, :job => job})
respond_to do |format|
if watch.save
flash[:notice] = 'Job is now watched. You can find a list of your watched jobs on the dashboard.'
format.html { redirect_to(job) } #:controller=>:dashboard) }
else
flash[:notice] = 'Unsuccessful job watch. Perhaps you\'re already watching this job?'
format.html { redirect_to(job) }
end
end
end
def unwatch
job = Job.find(params[:id])
@current_user.watches.find_by_job_id(job.id).destroy
flash[:notice] = 'Job is now unwatched. You can find a list of your watched jobs on the dashboard.'
redirect_to :back
end
def close_jobs
jobs = Job.where('latest_start_date < ?', Time.now)
jobs.each do |job|
job.update_attribute(:status, 1)
job.save(validate: false)
end
end
protected
# Processes form data for Job.update
def job_params
# TODO FIXME this sets the primary user as whoever is editing, which is quite broken.
params[:job][:user_id] = @current_user.id
params[:job][:end_date] = nil if params[:job].delete(:open_ended_end_date)
# Handles the text_fields for categories, courses, and programming languages
[:category, :course, :proglang].each do |k|
params[:job]["#{k.to_s}_names".to_sym] = params[k][:name]
end
params[:job] = params.require(:job).permit(:title, :desc, :project_type,
:user_id, :department_id, :status, :compensation, :num_positions,
:end_date, :earliest_start_date, :latest_start_date,
:category_names, :course_names, :proglang_names, :question_1, :question_2, :question_3)
[:earliest_start_date, :latest_start_date, :end_date].each do |attribute|
if params[:job][attribute].presence
params[:job][attribute] = Date.parse(params[:job][attribute])
end
end
params[:job]
end
# Saves sponsorship specified in the params page.
# Returns true if sponsorships changed at all for this update,
# and false if they did not.
def update_sponsorships
# TODO allow more than one sponsor
if params[:faculty_id] != '-1'
@job.sponsorships.delete_all
@job.sponsorships.create(faculty_id: params[:faculty_id])
end
return @job.sponsorships
end
####################
# FILTERS #
####################
private
def correct_user_access
job = Job.find(params[:id])
if !job || !job.can_admin?(@current_user)
flash[:error] = "You don't have permissions to edit or delete that listing."
redirect_to :controller => 'dashboard', :action => :index
end
end
end