| Class | VotesController |
| In: |
app/controllers/votes_controller.rb
|
| Parent: | ApplicationController |
GET /votes/add
Vote.
| prompt_id<String>: | Converted to integer. Prompt id of vote. Must belong |
to user.
| skip<String>: | Vote is skip if value ‘1’. |
| item_id<String>: | Item ids of winners. Integer or comman seperated list |
integers. Any zero values are removed.
| voter_id<String>: | Converted to integer. Voter id or anonymous 0 voter. |
Must belong to user.
| ArgumentError: | If no prompt id, prompt doesn‘t belong to user, or no |
voter id.
| PermissionError: | If voter does not belong to user. |
# File app/controllers/votes_controller.rb, line 50
50: def add
51: prompt_id = params[:prompt_id].to_i
52: item_ids = (params[:item_id] && params[:item_id].split(',').map(&:to_i).reject(&:zero?)) || []
53: skip = (params[:skip] == '1' || item_ids.empty?)
54: voter_id = params[:voter_id].to_i
55: # can only vote on your questions' prompts, requires voter
56: raise ArgumentError unless prompt_id > 0 && current_user.prompts.exists?(prompt_id) && voter_id
57: # if non-anonymous voter user must own voter
58: raise PermissionError if voter_id > 0 && Voter.find(voter_id).user_id != current_user.id
59: prompt = Prompt.find(prompt_id)
60: prompt.update_attribute(:active, false) if prompt.active
61: question_id = prompt.question_id
62: all_items = prompt.items
63: items = []
64: old_elos = all_items.inject({}) do |hash, item|
65: hash[item.id] = item.iq(question_id).position
66: hash
67: end
68: @adj = true
69: attributes = { :prompt_id => prompt_id, :voter_id => voter_id }
70: attributes[:tracking] = params[:tracking] unless params[:tracking].nil?
71: attributes[:response_time] = params[:response_time].to_i if params[:response_time].to_i > 0
72: @vote = Vote.new(attributes)
73: raise ArgumentError unless @vote.valid?
74: if skip == true
75: all_items.each do |item|
76: iq = item.iq(question_id)
77: iq.increment!(:ratings)
78: (all_items - [item]).each do |other|
79: iq.increment!(:position, adjust_elo(DRAW_SCORE, iq.position, old_elos[other.id]))
80: end
81: end
82: else
83: items = Item.find(item_ids)
84: # vote invalid if any items not in prompt
85: raise ArgumentError unless (items - all_items).empty?
86: items.each do |item|
87: iq = item.iq(question_id)
88: iq.update_attributes({ :ratings => iq.ratings + 1, :wins => iq.wins + 1 })
89: (all_items - items).each do |loser|
90: iq.increment!(:position, adjust_elo(WIN_SCORE, old_elos[item.id], old_elos[loser.id]))
91: loser.iq(question_id).increment!(:position, adjust_elo(LOSS_SCORE, old_elos[loser.id], old_elos[item.id]))
92: end
93: end
94: (all_items - items).each do |item|
95: iq = item.iq(question_id)
96: iq.update_attributes({ :ratings => iq.ratings + 1, :losses => iq.losses + 1 })
97: end
98: end
99: Stat.vote(question_id, all_items, items)
100: @vote.save!
101: @vote.items << items if defined?(items) && items
102: end
GET /votes/list
Array of votes.
| question_id<String>: | Converted to integer. Optional question id of votes. |
| item_id<String>: | Converted to integer. Optional item id of votes. Must |
belong to user.
| PermissionError: | If question of item does not belong to user. |
# File app/controllers/votes_controller.rb, line 14
14: def list
15: conditions = { 'questions.user_id' => current_user.id }
16: conditions.merge!('questions.id' => params[:question_id]) if params[:question_id].to_i > 0
17: item_id = params[:item_id].to_i
18: conditions.merge!('items_prompts.item_id' => item_id) if item_id > 0
19: options = {
20: :include => [{:prompt => [:items, :question]}],
21: :conditions => conditions,
22: :order => 'votes.id'
23: }
24: options[:limit] = params[:limit] if params[:limit].to_i > 0
25: @votes = Vote.all(options)
26: if item_id > 0
27: options[:conditions].delete('items_prompts.item_id')
28: options[:conditions].merge!('items.id' => item_id)
29: options[:joins] = "INNER JOIN items_prompts ON (items_prompts.prompt_id=prompts.id AND items_prompts.item_id=#{item_id})"
30: options[:include] = [:items, {:prompt => :question}]
31: @votes_items = Vote.all(options)
32: end
33: end