From b64a1681d5d2c87cf9fa530bca6cacf2fb6d4897 Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Wed, 10 Feb 2010 15:36:21 -0500 Subject: [PATCH] Exporting data from pairwise db --- app/controllers/questions_controller.rb | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/models/visitor.rb | 1 + config/environment.rb | 3 +++ config/routes.rb | 2 +- 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 6f59432..495c723 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -1,5 +1,8 @@ +require 'fastercsv' + class QuestionsController < InheritedResources::Base respond_to :xml, :json + respond_to :csv, :only => :export #leave the option for xml export here belongs_to :site, :optional => true #has_scope :voted_on_by @@ -41,6 +44,8 @@ class QuestionsController < InheritedResources::Base end end + + def set_autoactivate_ideas_from_abroad authenticate expire_page :action => :index @@ -63,6 +68,64 @@ class QuestionsController < InheritedResources::Base end end + def export + + type = params[:type] + + if type == 'votes' + export_votes + elsif type == 'items' + export_items + else + render :text => "Error! Specify a type of export" + end +# export_type = params[:export_type] +# export_format = params[:export_format] #CSV always now, could expand to xml later + end + + + protected + def export_votes + @question = Question.find(params[:id]) + + outfile = "question_#{@question.id}_votes" + Time.now.strftime("%m-%d-%Y") + ".csv" + headers = ['Vote ID', 'Voter ID', 'Choice Voted on ID', 'Choice Voted on Data', 'Question ID', 'Created at', 'Updated at'] + csv_data = FasterCSV.generate do |csv| + csv << headers + @question.choices.each do |choice| + + choice.votes.each do |v| + csv << [ v.id, v.voter_id, choice.id, choice.data, @question.id, v.created_at, v.updated_at] + end + end + end + + send_data(csv_data, + :type => 'text/csv; charset=iso-8859-1; header=present', + :disposition => "attachment; filename=#{outfile}") + end + + def export_items + @question = Question.find(params[:id], :include => [:choices, :prompts]) + + outfile = "question_#{@question.id}_items_" + Time.now.strftime("%m-%d-%Y") + ".csv" + headers = ['Choice ID', 'Item ID', 'Data', 'Question ID', 'User Submitted', 'Choice Creator ID', + 'Wins', 'Losses', 'Created at', 'Updated at', 'Active', 'Score', 'Local Identifier', + 'Prompts on Left', 'Prompts on Right', 'Prompts Count'] + + csv_data = FasterCSV.generate do |csv| + csv << headers + @question.choices.each do |c| + csv << [ c.id, c.item_id, c.data, c.question_id, c.item.creator != @question.creator, c.item.creator_id, + c.wins, c.losses, c.created_at, c.updated_at, c.active, c.score, c.local_identifier, + c.prompts_on_the_left.size, c.prompts_on_the_right.size, c.prompts_count] + end + end + + send_data(csv_data, + :type => 'text/csv; charset=iso-8859-1; header=present', + :disposition => "attachment; filename=#{outfile}") + end end class String diff --git a/app/models/visitor.rb b/app/models/visitor.rb index a4a53af..51502d1 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -14,6 +14,7 @@ class Visitor < ActiveRecord::Base end def vote_for!(prompt, ordinality) + # Why are there three vote objects created for every actual 'vote'? Why not have each vote have a questionid, promptid and choiceid? question_vote = votes.create!(:voteable_id => prompt.question_id, :voteable_type => "Question") logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question_id}" diff --git a/config/environment.rb b/config/environment.rb index 6b66658..71bbb93 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -55,5 +55,8 @@ Rails::Initializer.run do |config| config.gem "activemerchant", :lib => 'active_merchant', :version => '1.4.2' + config.gem "fastercsv", + :lib => 'fastercsv', + :version => '1.5.1' end diff --git a/config/routes.rb b/config/routes.rb index 6deec97..c030cdc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ ActionController::Routing::Routes.draw do |map| map.resources :clicks - map.resources :questions, :member => { :set_autoactivate_ideas_from_abroad => :put, :activate => :put, :suspend => :put} do |question| + map.resources :questions, :member => { :export => :get, :set_autoactivate_ideas_from_abroad => :put, :activate => :put, :suspend => :put} do |question| question.resources :items question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, :collection => {:single => :get, :index => :get} -- libgit2 0.21.2