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 218 @question = current_user.questions.find(params[:id])
219 219  
220 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 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 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 226 :group => "date(choices.created_at)")
227 227 # we want graphs to go from date of first vote -> date of last vote, so adding those two boundries here.
228 228 mindate = Vote.minimum('date(created_at)', :conditions => {:question_id => @question.id})
229 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 233 elsif object_type == 'user_sessions'
234 234 # little more work to do here:
235 235 result = Vote.find(:all, :select => 'date(created_at) as date, voter_id, count(*) as vote_count',
236 236 :conditions => "question_id = #{@question.id}", :group => 'date(created_at), voter_id')
237   - hash = Hash.new(0)
  237 + data = Hash.new(0)
238 238 result.each do |r|
239   - hash[r.date]+=1
  239 + data[r.date]+=1
240 240 end
241 241  
242 242 elsif object_type == 'appearances_by_creation_date'
243 243  
244   - hash = Hash.new()
  244 + array = []
245 245 @question.choices.active.find(:all, :order => :created_at).each do |c|
246 246 relevant_prompts = c.prompts_on_the_left.find(:all, :select => 'id') + c.prompts_on_the_right.find(:all, :select => 'id')
247 247  
248 248 appearances = Appearance.count(:conditions => {:prompt_id => relevant_prompts, :question_id => @question.id})
249 249  
250 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 252 end
253 253  
254 254  
255 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 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 268 end
260 269 end
261 270  
... ...