Commit bf6495cee8384259b9bf00ac7d80c187aa6fcbdf

Authored by Luke Baker
1 parent 7448e455

update xml response format to be valid xml

Showing 1 changed file with 19 additions and 10 deletions   Show diff stats
app/controllers/questions_controller.rb
@@ -218,44 +218,53 @@ class QuestionsController < InheritedResources::Base @@ -218,44 +218,53 @@ class QuestionsController < InheritedResources::Base
218 @question = current_user.questions.find(params[:id]) 218 @question = current_user.questions.find(params[:id])
219 219
220 if object_type == 'votes' 220 if object_type == 'votes'
221 - hash = Vote.count(:conditions => "question_id = #{@question.id}", :group => "date(created_at)") 221 + data = Vote.count(:conditions => "question_id = #{@question.id}", :group => "date(created_at)")
222 elsif object_type == 'skips' 222 elsif object_type == 'skips'
223 - hash = Skip.count(:conditions => {:question_id => @question.id}, :group => "date(created_at)") 223 + data = Skip.count(:conditions => {:question_id => @question.id}, :group => "date(created_at)")
224 elsif object_type == 'user_submitted_ideas' 224 elsif object_type == 'user_submitted_ideas'
225 - hash = Choice.count(:conditions => "choices.question_id = #{@question.id} AND choices.creator_id <> #{@question.creator_id}", 225 + data = Choice.count(:conditions => "choices.question_id = #{@question.id} AND choices.creator_id <> #{@question.creator_id}",
226 :group => "date(choices.created_at)") 226 :group => "date(choices.created_at)")
227 # we want graphs to go from date of first vote -> date of last vote, so adding those two boundries here. 227 # we want graphs to go from date of first vote -> date of last vote, so adding those two boundries here.
228 mindate = Vote.minimum('date(created_at)', :conditions => {:question_id => @question.id}) 228 mindate = Vote.minimum('date(created_at)', :conditions => {:question_id => @question.id})
229 maxdate = Vote.maximum('date(created_at)', :conditions => {:question_id => @question.id}) 229 maxdate = Vote.maximum('date(created_at)', :conditions => {:question_id => @question.id})
230 230
231 - hash[mindate] = 0 if !hash.include?(mindate)  
232 - hash[maxdate] = 0 if !hash.include?(maxdate) 231 + data[mindate] = 0 if !data.include?(mindate)
  232 + data[maxdate] = 0 if !data.include?(maxdate)
233 elsif object_type == 'user_sessions' 233 elsif object_type == 'user_sessions'
234 # little more work to do here: 234 # little more work to do here:
235 result = Vote.find(:all, :select => 'date(created_at) as date, voter_id, count(*) as vote_count', 235 result = Vote.find(:all, :select => 'date(created_at) as date, voter_id, count(*) as vote_count',
236 :conditions => "question_id = #{@question.id}", :group => 'date(created_at), voter_id') 236 :conditions => "question_id = #{@question.id}", :group => 'date(created_at), voter_id')
237 - hash = Hash.new(0) 237 + data = Hash.new(0)
238 result.each do |r| 238 result.each do |r|
239 - hash[r.date]+=1 239 + data[r.date]+=1
240 end 240 end
241 241
242 elsif object_type == 'appearances_by_creation_date' 242 elsif object_type == 'appearances_by_creation_date'
243 243
244 - hash = Hash.new() 244 + array = []
245 @question.choices.active.find(:all, :order => :created_at).each do |c| 245 @question.choices.active.find(:all, :order => :created_at).each do |c|
246 relevant_prompts = c.prompts_on_the_left.find(:all, :select => 'id') + c.prompts_on_the_right.find(:all, :select => 'id') 246 relevant_prompts = c.prompts_on_the_left.find(:all, :select => 'id') + c.prompts_on_the_right.find(:all, :select => 'id')
247 247
248 appearances = Appearance.count(:conditions => {:prompt_id => relevant_prompts, :question_id => @question.id}) 248 appearances = Appearance.count(:conditions => {:prompt_id => relevant_prompts, :question_id => @question.id})
249 249
250 #initialize key to list if it doesn't exist 250 #initialize key to list if it doesn't exist
251 - (hash[c.created_at.to_date] ||= []) << { :data => c.data, :appearances => appearances} 251 + array << {:date => c.created_at.to_date, :data => c.data, :appearances => appearances}
252 end 252 end
253 253
254 254
255 end 255 end
256 256
  257 + # all but appearances_by_creation_date create data hash that needs
  258 + # to be converted to array
  259 + if data && !array
  260 + array = []
  261 + data.each do |key, value|
  262 + array << {:date => key, :count => value}
  263 + end
  264 + end
  265 +
257 respond_to do |format| 266 respond_to do |format|
258 - format.xml { render :xml => hash.to_xml and return} 267 + format.xml { render :xml => array.to_xml and return}
259 end 268 end
260 end 269 end
261 270