choices_controller.rb
3.8 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
class ChoicesController < InheritedResources::Base
respond_to :xml, :json
actions :show, :index, :create, :update, :new
belongs_to :question
has_scope :active, :type => :boolean, :only => :index
before_filter :authenticate
def index
order = 'score DESC'
order = params[:order].map{|a| "#{a.first} #{a.second}"}.join(',') unless params[:order].blank?
conditions = []
conditions << ['lower(data) like ?', params[:filter][:data].downcase] if params[:filter] && !params[:filter][:data].blank?
@question = current_user.questions.find(params[:question_id])
find_options = {:conditions => {:question_id => @question.id},
:order => order
}
conditions << ["question_id = ?", @question.id]
conditions << ['active = ?', true] unless params[:include_inactive]
find_options[:conditions] = [conditions.map{|c| c[0] }.join(" AND "), *conditions.map{|c| c[1..-1] }.flatten]
find_options.merge!(:limit => params[:limit].to_i) if params[:limit]
find_options.merge!(:offset => params[:offset]) if params[:offset]
@choices = Choice.find(:all, find_options)
index! do |format|
format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => :user_created)}
end
end
def votes
@choice = Choice.find(params[:id])
render :xml => @choice.votes.to_xml
end
def create
visitor_identifier = params[:choice].delete(:visitor_identifier)
visitor = current_user.default_visitor
if visitor_identifier
visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier)
end
params[:choice].merge!(:creator => visitor)
@question = current_user.questions.find(params[:question_id])
params[:choice].merge!(:question_id => @question.id)
@choice = Choice.new(params[:choice])
create!
end
def flag
@question = current_user.questions.find(params[:question_id])
@choice = @question.choices.find(params[:id])
flag_params = {:choice_id => params[:id].to_i, :question_id => params[:question_id].to_i, :site_id => current_user.id}
if explanation = params[:explanation]
flag_params.merge!({:explanation => explanation})
end
if visitor_identifier = params[:visitor_identifier]
visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier)
flag_params.merge!({:visitor_id => visitor.id})
end
respond_to do |format|
if @choice.deactivate!
flag = Flag.create!(flag_params)
format.xml { render :xml => @choice.to_xml, :status => :created }
format.json { render :json => @choice.to_json, :status => :created }
else
format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }
format.json { render :json => @choice.to_json }
end
end
end
def update
# prevent AttributeNotFound error and only update actual Choice columns, since we add extra information in 'show' method
choice_attributes = Choice.new.attribute_names
params[:choice] = params[:choice].delete_if {|key, value| !choice_attributes.include?(key)}
Choice.transaction do
# lock question since we'll need a lock on it later in Choice.update_questions_counter
@question = current_user.questions.find(params[:question_id], :lock => true)
@choice = @question.choices.find(params[:id])
update!
end
end
def show
@question = current_user.questions.find(params[:question_id])
@choice = @question.choices.find(params[:id])
response_options = {}
response_options[:include] = :versions if params[:version] == 'all'
respond_to do |format|
format.xml { render :xml => @choice.to_xml(response_options) }
format.json { render :json => @choice.to_json(response_options) }
end
end
end