choices_controller.rb
4.97 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
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
if params[:limit]
@question = current_user.questions.find(params[:question_id])
find_options = {:conditions => {:question_id => @question.id},
:limit => params[:limit].to_i,
:order => 'score DESC'
}
find_options[:conditions].merge!(:active => true) unless params[:include_inactive]
find_options[:include] = [:creator]
if(params[:ignore_flagged])
find_options[:include] << :flags
find_options[:conditions].merge!({:flags => {:id => nil}})
end
find_options.merge!(:offset => params[:offset]) if params[:offset]
@choices = Choice.find(:all, find_options)
else
@question = current_user.questions.find(params[:question_id])
sort_by = "score"
sort_order = "DESC"
if !params[:order].blank?
if params[:order][:sort_order].downcase == "asc" or params[:order][:sort_order].downcase == "desc"
sort_order = params[:order][:sort_order].downcase
end
case params[:order][:sort_by].downcase
when "name"
sort_by = "choices.data"
when "date"
sort_by = "choices.created_at"
when "author"
sort_by = "visitors.identifier"
else
sort_by = "score"
end
end
order = "#{sort_by} #{sort_order}"
find_options = { :include => [:creator] }
find_options.merge!({ :order => order })
if (!params[:reproved].blank?)
@choices = @question.choices(true).reproved.find(:all, find_options)
else
if params[:inactive_ignore_flagged]
@choices = @question.choices(true).inactive_ignore_flagged.find(:all, find_options)
elsif params[:inactive]
@choices = @question.choices(true).inactive.find(:all, find_options)
else
unless params[:include_inactive]
@choices = @question.choices(true).active.find(:all, find_options)
else
@choices = @question.choices.find(:all, find_options)
end
end
end
end
index! do |format|
format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier])}
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