| Class | ItemsController |
| In: |
app/controllers/items_controller.rb
|
| Parent: | ApplicationController |
POST /items/add
Added Item.
| active<String>: | Converted to boolean. If not nil item is activated. |
| tracking<String>: | String of data to be stored with item. |
| voter_id<String>: | Converted to Integer. Optional ID, if passed and |
greater than zero items are linked to this voter. Voter must be present and be owner by the user.
Formatted XML of item to add and question to add item to.
| PermissionError: | If any questions do not belong to user. |
| PermissionError: | If any voters do not belong to user. |
# File app/controllers/items_controller.rb, line 52
52: def add
53: return unless request.post?
54: xml = LibXML::XML::Parser.parse(request.raw_post)
55: active = !params[:active].nil?
56: @items = xml.find("/items/item").inject([]) do |items, item|
57: questions = item.find("questions/question").inject([]) do |arr, question|
58: arr << current_user.questions.find(question.attributes["id"])
59: end
60: attributes = {:user_id => current_user.id, :data => item.find("data").first.content, :active => active}
61: attributes.merge!(:tracking => params[:tracking]) if params[:tracking]
62: if (voter_id = params[:voter_id].to_i) > 0
63: raise PermissionError unless current_user.voters.find(voter_id)
64: attributes.merge!(:voter_id => voter_id)
65: end
66: current_item = Item.create(attributes)
67: current_item.questions << questions
68: items << current_item
69: end
70: end
POST /items/delete
ID of item deleted and whether or not item was succesfully deleted.
| id<String>: | Converted to integer. ID of item. |
| PermissionError: | If item does not belong to user. |
# File app/controllers/items_controller.rb, line 79
79: def delete
80: return unless request.post?
81: item = Item.find(@id = params[:id], :conditions => { :user_id => current_user.id })
82: item.destroy
83: @success = !Item.exists?(@id)
84: end
GET /items/list
Array of items.
| limit<String>: | Converted to integer. Number of items to return. |
| offset<String>: | Converted to integer. Item to begin returning with. |
| order<String>: | Order to return items. If ASC items returned in ascending |
order, otherwise items returned in descending order.
| question_id<String>: | Converted to integer. Question to return items for. |
| data<String>: | Converted to boolean. If exists return data for items. |
| rank_algorithm<String>: | Name or ID of rank algorithm. Default order is |
by created at date.
| PermissionError: | If question does not belong to user. |
# File app/controllers/items_controller.rb, line 100
100: def list
101: limit = params[:limit].to_i
102: offset = params[:offset].to_i
103: order = (params[:order] && params[:order].downcase == 'asc') ? 'ASC' : 'DESC'
104: question_id = params[:question_id].to_i
105: options = list_options(limit, offset, question_id)
106: @data = !params[:data].nil?
107: case parse_rank_algo_id(params[:rank_algorithm])
108: when 1
109: options[:order] = 'items_questions.position'
110: if question_id > 0
111: @score = lambda { |i| i.items_questions.first(:conditions => { :question_id => question_id }).position }
112: else
113: @score = lambda do |i|
114: iqs = i.items_questions
115: iqs.sumup(:position).to_f / iqs.length
116: end
117: end
118: when 2
119: # set score to percent wins after case
120: when 3
121: # unset limit and truncate after sort
122: options[:limit] = nil if limit > 0
123: @items = Item.all(options)
124: stats = ewp_all(@items, question_id).inject({}) do |h, stat|
125: h[stat.item_id.to_i] = stat_to_percent(stat)
126: h
127: end
128: for item in @items
129: item.score = stats[item.id] || 0
130: end
131: @items = @items.sort_by { |el| -el.score }
132: @items = @items.first(limit) if limit > 0
133: @score = lambda { |i| i.score }
134: when 4
135: res = row_list(question_id)
136: if res
137: res = res.inject({}) do |h, stat|
138: h[stat[1].to_i] = stat[0]
139: h
140: end
141: @items = Item.all(options)
142: for item in @items
143: item.score = res[item.id] || 0
144: end
145: @items = @items.sort_by { |el| -el.score }
146: @score = lambda { |i| i.score }
147: else
148: @items = []
149: end
150: else
151: options[:order] = 'items.created_at'
152: end
153: unless @score
154: if question_id > 0
155: @score = lambda do |i|
156: iq = i.items_questions.first(:conditions => { :question_id => question_id })
157: wins = iq.wins.to_f
158: total = iq.wins + iq.losses
159: total.zero? ? 0 : (100 * (wins/total)).round
160: end
161: else
162: @score = lambda do |i|
163: iqs = i.items_questions.all
164: wins = iqs.sumup(:wins).to_f
165: total = wins + iqs.sumup(:losses)
166: total.zero? ? 0 : ((100 * (wins/total)) / iqs.length).round
167: end
168: end
169: end
170: unless @items
171: options[:order] += " #{order}"
172: @items = Item.all(options)
173: end
174: end
GET /items/1
Item by id.
| id<String>: | Converted to integer. ID of item. |
| rank_algorithm<String>: | Converted to integer. ID or name of rank algorithm. |
If present score attribute is returned with score of this item according to specified rank algorithm. Default value is nil.
| PermissionError: | If item does not belong to user. |
# File app/controllers/items_controller.rb, line 17
17: def show
18: @item = Item.find(params[:id], :conditions => { :user_id => current_user.id }, :include => [ :questions ])
19: case parse_rank_algo_id(params[:rank_algorithm])
20: when 1
21: @score = lambda { |qid| @item.items_questions.first(:conditions => { :question_id => qid }).position }
22: when 2
23: @score = lambda do |qid|
24: iq = i.items_questions.first(:conditions => { :question_id => qid })
25: wins = iq.wins.to_if
26: total = iq.wins + iq.losses
27: total.zero? ? 0 : (100 * (wins/total)).round
28: end
29: when 3
30: @score = lambda do |qid|
31: ewp(@item, qid)
32: end
33: else
34: @score = lambda { |qid| 0 }
35: end
36: end