Class Admin::PeopleController
In: app/controllers/admin/people_controller.rb
Parent: Admin::AdminController

Add, delete, and edit Person information. Also merge.

Methods

Included Modules

ApplicationHelper ActionView::Helpers::TextHelper

Public Instance methods

[Source]

     # File app/controllers/admin/people_controller.rb, line 350
350:   def cancel_in_place_edit
351:     person_id = params[:id]
352:     render :update do |page|
353:       page.replace("person_#{person_id}_row", :partial => "person", :locals => { :person => Person.find(person_id) })
354:       page.call :restripeTable, :people_table
355:     end
356:   end

Single membership card

[Source]

     # File app/controllers/admin/people_controller.rb, line 416
416:   def card
417:     @person = Person.find(params[:id])
418:     @people = [@person]
419:     @person.print_card = false
420:     @person.membership_card = true
421:     @person.card_printed_at = RacingAssociation.current.now
422:     @person.save!
423:     
424:     respond_to do |format|
425:       format.pdf do
426:         send_data Card.new.to_pdf(@person), 
427:                   :filename => "card.pdf", 
428:                   :type => "application/pdf"
429:       end
430:     end
431:   end

Membership card stickers/labels

[Source]

     # File app/controllers/admin/people_controller.rb, line 398
398:   def cards
399:     @people = Person.find(:all, :conditions => ['print_card=?', true], :order => 'last_name, first_name')
400:     if @people.empty?
401:       return redirect_to(no_cards_admin_people_path(:format => "html"))
402:     else
403:       Person.update_all("print_card=0, membership_card=1", ['id in (?)', @people.collect{|person| person.id}])
404:     end
405: 
406:     respond_to do |format|
407:       format.pdf do
408:         send_data Card.new.to_pdf(@people), 
409:                   :filename => "cards.pdf", 
410:                   :type => "application/pdf"
411:       end
412:     end
413:   end

Create new Person

Existing RaceNumbers are updated from a Hash: :number => {‘race_number_id’ => {:value => ‘new_value’}}

New numbers are created from arrays: :number_value => […] :discipline_id => […] :number_issuer_id => […] :number_year => year (not array) New blank numbers are ignored

[Source]

     # File app/controllers/admin/people_controller.rb, line 112
112:   def create
113:     expire_cache
114:     @person = Person.create(params[:person])
115:     
116:     if params[:number_value]
117:       params[:number_value].each_with_index do |number_value, index|
118:         unless number_value.blank?
119:           race_number = @person.race_numbers.create(
120:             :discipline_id => params[:discipline_id][index], 
121:             :number_issuer_id => params[:number_issuer_id][index], 
122:             :year => params[:number_year],
123:             :value => number_value
124:           )
125:           unless race_number.errors.empty?
126:             @person.errors.add_to_base(race_number.errors.full_messages)
127:           end
128:         end
129:       end
130:     end
131:     if @person.errors.empty?
132:       if @event
133:         redirect_to(edit_admin_person_path(@person, :event_id => @event.id))
134:       else
135:         redirect_to(edit_admin_person_path(@person))
136:       end
137:     else
138:       @year = current_date.year
139:       @race_numbers = RaceNumber.find(:all, :conditions => ['person_id=? and year=?', @person.id, @year], :order => 'number_issuer_id, discipline_id')
140:       @years = (2005..(RacingAssociation.current.next_year)).to_a.reverse
141:       render :edit
142:     end
143:   end

[Source]

     # File app/controllers/admin/people_controller.rb, line 321
321:   def destroy
322:     @person = Person.find(params[:id])
323:     @person.destroy
324:     respond_to do |format|
325:       format.html {redirect_to admin_people_path}
326:       format.js
327:     end
328:     expire_cache
329:   end

[Source]

     # File app/controllers/admin/people_controller.rb, line 389
389:   def destroy_alias
390:     alias_id = params[:alias_id]
391:     Alias.destroy(alias_id)
392:     render :update do |page|
393:       page.visual_effect(:puff, "alias_#{alias_id}", :duration => 2)
394:     end
395:   end

[Source]

     # File app/controllers/admin/people_controller.rb, line 380
380:   def destroy_number
381:     id = params[:id]
382:     RaceNumber.destroy(id)
383:     render :update do |page|
384:       page.visual_effect(:puff, "number_#{id}_row", :duration => 2)
385:       page.remove("number_#{id}_value")
386:     end
387:   end

Unresolved duplicates after import

[Source]

     # File app/controllers/admin/people_controller.rb, line 267
267:   def duplicates
268:     @duplicates = Duplicate.find(:all)
269:     @duplicates.sort! do |x, y|
270:       diff = (x.person.last_name || '') <=> y.person.last_name
271:       if diff == 0
272:         (x.person.first_name || '') <=> y.person.first_name
273:       else
274:         diff
275:       end
276:     end
277:   end

[Source]

    # File app/controllers/admin/people_controller.rb, line 94
94:   def edit
95:     @person = Person.find(params[:id])
96:     @year = current_date.year
97:     @race_numbers = RaceNumber.find(:all, :conditions => ['person_id=? and year=?', @person.id, @year], :order => 'number_issuer_id, discipline_id')
98:     @years = (2005..(RacingAssociation.current.next_year)).to_a.reverse
99:   end

Params

  • excel_layout: "scoring_sheet" for fewer columns — intended for scoring race results. "endicia" for card stickers.
  • include: "print_cards"
  • format: "ppl" for FinishLynx scoring

[Source]

    # File app/controllers/admin/people_controller.rb, line 58
58:   def export
59:     date = current_date
60:     if params['excel_layout'] == 'scoring_sheet'
61:       file_name = 'scoring_sheet.xls'
62:     elsif params['include'] == 'print_cards'
63:       file_name = 'print_cards.xls'
64:     elsif params['format'] == 'ppl'
65:       file_name = 'lynx.ppl'
66:     else
67:       file_name = "people_#{date.year}_#{date.month}_#{date.day}.#{params['format']}"
68:     end
69:     headers['Content-Disposition'] = "filename=\"#{file_name}\""
70: 
71:     @people = Person.find_all_for_export(date, params['include'])
72:     
73:     respond_to do |format|
74:       format.html
75:       format.ppl
76:       format.xls {
77:         if params['excel_layout'] == 'scoring_sheet'
78:           render :template => 'admin/people/scoring_sheet.xls.erb' 
79:         elsif params['excel_layout'] == 'endicia'
80:           render :template => 'admin/people/endicia.xls.erb' 
81:         end
82:       }
83:     end
84:   end

See trac.butlerpress.com/racing_on_rails/wiki/SampleImportFiles for format details and examples.

[Source]

     # File app/controllers/admin/people_controller.rb, line 236
236:   def import
237:     if params[:commit] == 'Cancel'
238:       session[:people_file_path] = nil
239:       redirect_to(:action => 'index')
240: 
241:     elsif params[:commit] == 'Import'
242:       Duplicate.delete_all
243:       path = session[:people_file_path]
244:       if path.blank?
245:         flash[:warn] = "No import file"
246:         return redirect_to(admin_people_path)
247:       end
248:       
249:       people_file = PeopleFile.new(File.new(path))
250:       people_file.import(params[:update_membership], params[:year])
251:       flash[:notice] = "Imported #{pluralize(people_file.created, 'new person')} and updated #{pluralize(people_file.updated, 'existing person')}"
252:       session[:people_file_path] = nil
253:       if people_file.duplicates.empty?
254:         redirect_to(:action => 'index')
255:       else
256:         flash[:warn] = 'Some names in the import file already exist more than once. Match with an existing person or create a new person with the same name.'
257:         redirect_to(:action => 'duplicates')
258:       end
259:       expire_cache
260: 
261:     else
262:       raise "Expected 'Import' or 'Cancel'"
263:     end
264:   end

Search for People by name. This is a ‘like’ search on the concatenated first and last name, and aliases. E.g.,: ‘son’ finds:

 * Sonja Red
 * Charles Sondheim
 * Cheryl Willson
 * Scott Willson
 * Jim Andersen (with an 'Jim Anderson' alias)

Store previous search in session and cookie as ‘person_name’. Limit results to RacingAssociation.current.search_results_limit

Params

  • name

Assigns

  • person: Array of People

[Source]

    # File app/controllers/admin/people_controller.rb, line 30
30:   def index
31:     if params['format'] == 'ppl' || params['format'] == 'xls'
32:       return export
33:     end
34:     
35:     @people = []
36:     @name = params[:term] || params[:name] || session[:person_name] || cookies[:person_name] || ''
37:     @name.strip!
38:     session['person_name'] = @name
39:     cookies[:person_name] = { :value => @name, :expires => Time.zone.now + 36000 }
40:     if @name.blank?
41:       @people = []
42:     else
43:       @people = Person.find_all_by_name_like(@name, RacingAssociation.current.search_results_limit)
44:       @people = @people + Person.find_by_number(@name)
45:       @people = @people.stable_sort_by(:first_name).stable_sort_by(:last_name)
46:     end
47:     if @people.size == RacingAssociation.current.search_results_limit
48:       flash[:notice] = "First #{RacingAssociation.current.search_results_limit} people"
49:     end
50: 
51:     @current_year = current_date.year
52:   end

[Source]

     # File app/controllers/admin/people_controller.rb, line 341
341:   def merge
342:     person_to_merge_id = params[:id].gsub('person_', '')
343:     @person_to_merge = Person.find(person_to_merge_id)
344:     @merged_person_name = @person_to_merge.name
345:     @existing_person = Person.find(params[:target_id])
346:     @existing_person.merge(@person_to_merge)
347:     expire_cache
348:   end

Show choices for Person merge

[Source]

     # File app/controllers/admin/people_controller.rb, line 332
332:   def merge?(original_name, existing_people, person)
333:     @person = person
334:     @existing_people = existing_people
335:     @original_name = original_name
336:     render :update do |page| 
337:       page.replace_html("person_#{@person.id}_row", :partial => "merge_confirm", :locals => { :person => @person })
338:     end
339:   end

[Source]

    # File app/controllers/admin/people_controller.rb, line 86
86:   def new
87:     @year = current_date.year
88:     @person = Person.new
89:     @race_numbers = []
90:     @years = (2005..(RacingAssociation.current.next_year)).to_a.reverse
91:     render :edit
92:   end

[Source]

     # File app/controllers/admin/people_controller.rb, line 371
371:   def new_number
372:     render :update do |page|
373:       page.insert_html(
374:         :before, 'new_number_button_row', 
375:         :partial => '/admin/people/new_number', 
376:         :locals => {:discipline_id => Discipline[:road].id})
377:     end
378:   end

[Source]

     # File app/controllers/admin/people_controller.rb, line 358
358:   def number_year_changed
359:     @year = params[:year] || current_date.year
360:     if params[:id]
361:       @person = Person.find(params[:id])
362:       @race_numbers = RaceNumber.find(:all, :conditions => ['person_id=? and year=?', @person.id, @year], :order => 'number_issuer_id, discipline_id')
363:     else
364:       @person = Person.new
365:       @race_numbers = []
366:     end
367:     @years = (2005..(RacingAssociation.current.next_year)).to_a.reverse
368:     render(:partial => '/admin/people/numbers', :locals => {:year => @year.to_i, :years => @years, :person => @person, :race_numbers => @race_numbers})
369:   end

Preview contents of new members file from event registration service website like SignMeUp or Active.com.

[Source]

     # File app/controllers/admin/people_controller.rb, line 212
212:   def preview_import
213:     if params[:people_file].blank?
214:       flash[:warn] = "Choose a file of people to import first"
215:       return redirect_to(:action => :index) 
216:     end
217: 
218:     path = "#{Dir.tmpdir}/#{params[:people_file].original_filename}"
219:     File.open(path, "wb") do |f|
220:       f.print(params[:people_file].read)
221:     end
222: 
223:     temp_file = File.new(path)
224:     @people_file = PeopleFile.new(temp_file)
225:     if @people_file
226:       assign_years
227:       session[:people_file_path] = temp_file.path
228:     else
229:       redirect_to(:action => :index)
230:     end
231:     
232:     render("preview_import", :layout => "admin/people/preview_import")
233:   end

[Source]

     # File app/controllers/admin/people_controller.rb, line 279
279:   def resolve_duplicates
280:     @duplicates = Duplicate.find(:all)
281:     @duplicates.each do |duplicate|
282:       id = params[duplicate.to_param]
283:       if id == 'new'
284:         duplicate.person.save!
285:       elsif !id.blank?
286:         person = Person.update(id, duplicate.new_attributes)
287:         unless person.valid?
288:           raise ActiveRecord::RecordNotSaved.new(person.errors.full_messages.join(', '))
289:         end
290:       end
291:     end
292:   
293:     Duplicate.delete_all
294:     redirect_to(:action => 'index')
295:   end

Inline update. Merge with existing Person if names match

[Source]

     # File app/controllers/admin/people_controller.rb, line 298
298:   def set_person_name
299:     @person = Person.find(params[:id])
300:     new_name = params[:value]
301:     original_name = @person.name
302:     @person.name = new_name
303: 
304:     people_with_same_name = @person.people_with_same_name
305:     unless people_with_same_name.empty?
306:       return merge?(original_name, people_with_same_name, @person)
307:     end
308:     
309:     @person.update_attribute(:name, params[:value])
310:     expire_cache
311:     render :text => @person.name
312:   end

Toggle membership on or off

[Source]

     # File app/controllers/admin/people_controller.rb, line 315
315:   def toggle_member
316:     person = Person.find(params[:id])
317:     person.toggle! :member
318:     render :partial => "shared/member", :locals => { :record => person }
319:   end

Update existing Person.

Existing RaceNumbers are updated from a Hash: :number => {‘race_number_id’ => {:value => ‘new_value’}}

New numbers are created from arrays: :number_value => […] :discipline_id => […] :number_issuer_id => […] :number_year => year (not array) New blank numbers are ignored

[Source]

     # File app/controllers/admin/people_controller.rb, line 156
156:   def update
157:     begin
158:       expire_cache
159:       @person = Person.find(params[:id])
160:       @person.update_attributes(params[:person])
161:       if params[:number]
162:         for number_id in params[:number].keys
163:           begin
164:             number = RaceNumber.find(number_id)
165:             number_params = params[:number][number_id]
166:             if number.value != params[:number][number_id][:value]
167:               number_params[:updated_by] = current_person.name
168:               RaceNumber.update(number_id, number_params)
169:             end
170:           rescue Exception => e
171:             logger.warn("#{e} updating RaceNumber #{number_id}")
172:           end
173:         end
174:       end
175:     
176:       if params[:number_value]
177:         params[:number_value].each_with_index do |number_value, index|
178:           unless number_value.blank?
179:             race_number = @person.race_numbers.create(
180:               :discipline_id => params[:discipline_id][index], 
181:               :number_issuer_id => params[:number_issuer_id][index], 
182:               :year => params[:number_year],
183:               :value => number_value,
184:               :updated_by => current_person.name
185:             )
186:             unless race_number.errors.empty?
187:               @person.errors.add_to_base(race_number.errors.full_messages)
188:             end
189:           end
190:         end
191:       end
192:     rescue ActiveRecord::MultiparameterAssignmentErrors => e
193:       e.errors.each do |multi_param_error|
194:         @person.errors.add(multi_param_error.attribute)
195:       end
196:     end
197: 
198:     if @person.errors.empty?
199:       if @event
200:         return redirect_to(edit_admin_person_path(@person, :event_id => @event.id))
201:       else
202:         return redirect_to(edit_admin_person_path(@person))
203:       end
204:     end
205:     @years = (2005..(RacingAssociation.current.next_year)).to_a.reverse
206:     @year = params[:year] || RacingAssociation.current.effective_year
207:     @race_numbers = RaceNumber.find(:all, :conditions => ['person_id=? and year=?', @person.id, @year], :order => 'number_issuer_id, discipline_id')
208:     render :edit
209:   end

[Validate]