Commit b64a1681d5d2c87cf9fa530bca6cacf2fb6d4897
1 parent
a0100454
Exists in
master
and in
1 other branch
Exporting data from pairwise db
Showing
4 changed files
with
68 additions
and
1 deletions
Show diff stats
app/controllers/questions_controller.rb
1 | +require 'fastercsv' | |
2 | + | |
1 | 3 | class QuestionsController < InheritedResources::Base |
2 | 4 | respond_to :xml, :json |
5 | + respond_to :csv, :only => :export #leave the option for xml export here | |
3 | 6 | belongs_to :site, :optional => true |
4 | 7 | #has_scope :voted_on_by |
5 | 8 | |
... | ... | @@ -41,6 +44,8 @@ class QuestionsController < InheritedResources::Base |
41 | 44 | end |
42 | 45 | end |
43 | 46 | |
47 | + | |
48 | + | |
44 | 49 | def set_autoactivate_ideas_from_abroad |
45 | 50 | authenticate |
46 | 51 | expire_page :action => :index |
... | ... | @@ -63,6 +68,64 @@ class QuestionsController < InheritedResources::Base |
63 | 68 | end |
64 | 69 | |
65 | 70 | end |
71 | + def export | |
72 | + | |
73 | + type = params[:type] | |
74 | + | |
75 | + if type == 'votes' | |
76 | + export_votes | |
77 | + elsif type == 'items' | |
78 | + export_items | |
79 | + else | |
80 | + render :text => "Error! Specify a type of export" | |
81 | + end | |
82 | +# export_type = params[:export_type] | |
83 | +# export_format = params[:export_format] #CSV always now, could expand to xml later | |
84 | + end | |
85 | + | |
86 | + | |
87 | + protected | |
88 | + def export_votes | |
89 | + @question = Question.find(params[:id]) | |
90 | + | |
91 | + outfile = "question_#{@question.id}_votes" + Time.now.strftime("%m-%d-%Y") + ".csv" | |
92 | + headers = ['Vote ID', 'Voter ID', 'Choice Voted on ID', 'Choice Voted on Data', 'Question ID', 'Created at', 'Updated at'] | |
93 | + csv_data = FasterCSV.generate do |csv| | |
94 | + csv << headers | |
95 | + @question.choices.each do |choice| | |
96 | + | |
97 | + choice.votes.each do |v| | |
98 | + csv << [ v.id, v.voter_id, choice.id, choice.data, @question.id, v.created_at, v.updated_at] | |
99 | + end | |
100 | + end | |
101 | + end | |
102 | + | |
103 | + send_data(csv_data, | |
104 | + :type => 'text/csv; charset=iso-8859-1; header=present', | |
105 | + :disposition => "attachment; filename=#{outfile}") | |
106 | + end | |
107 | + | |
108 | + def export_items | |
109 | + @question = Question.find(params[:id], :include => [:choices, :prompts]) | |
110 | + | |
111 | + outfile = "question_#{@question.id}_items_" + Time.now.strftime("%m-%d-%Y") + ".csv" | |
112 | + headers = ['Choice ID', 'Item ID', 'Data', 'Question ID', 'User Submitted', 'Choice Creator ID', | |
113 | + 'Wins', 'Losses', 'Created at', 'Updated at', 'Active', 'Score', 'Local Identifier', | |
114 | + 'Prompts on Left', 'Prompts on Right', 'Prompts Count'] | |
115 | + | |
116 | + csv_data = FasterCSV.generate do |csv| | |
117 | + csv << headers | |
118 | + @question.choices.each do |c| | |
119 | + csv << [ c.id, c.item_id, c.data, c.question_id, c.item.creator != @question.creator, c.item.creator_id, | |
120 | + c.wins, c.losses, c.created_at, c.updated_at, c.active, c.score, c.local_identifier, | |
121 | + c.prompts_on_the_left.size, c.prompts_on_the_right.size, c.prompts_count] | |
122 | + end | |
123 | + end | |
124 | + | |
125 | + send_data(csv_data, | |
126 | + :type => 'text/csv; charset=iso-8859-1; header=present', | |
127 | + :disposition => "attachment; filename=#{outfile}") | |
128 | + end | |
66 | 129 | end |
67 | 130 | |
68 | 131 | class String | ... | ... |
app/models/visitor.rb
... | ... | @@ -14,6 +14,7 @@ class Visitor < ActiveRecord::Base |
14 | 14 | end |
15 | 15 | |
16 | 16 | def vote_for!(prompt, ordinality) |
17 | + # Why are there three vote objects created for every actual 'vote'? Why not have each vote have a questionid, promptid and choiceid? | |
17 | 18 | question_vote = votes.create!(:voteable_id => prompt.question_id, :voteable_type => "Question") |
18 | 19 | logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question_id}" |
19 | 20 | ... | ... |
config/environment.rb
config/routes.rb
1 | 1 | ActionController::Routing::Routes.draw do |map| |
2 | 2 | map.resources :clicks |
3 | - map.resources :questions, :member => { :set_autoactivate_ideas_from_abroad => :put, :activate => :put, :suspend => :put} do |question| | |
3 | + map.resources :questions, :member => { :export => :get, :set_autoactivate_ideas_from_abroad => :put, :activate => :put, :suspend => :put} do |question| | |
4 | 4 | question.resources :items |
5 | 5 | question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, |
6 | 6 | :collection => {:single => :get, :index => :get} | ... | ... |