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 | class QuestionsController < InheritedResources::Base | 3 | class QuestionsController < InheritedResources::Base |
2 | respond_to :xml, :json | 4 | respond_to :xml, :json |
5 | + respond_to :csv, :only => :export #leave the option for xml export here | ||
3 | belongs_to :site, :optional => true | 6 | belongs_to :site, :optional => true |
4 | #has_scope :voted_on_by | 7 | #has_scope :voted_on_by |
5 | 8 | ||
@@ -41,6 +44,8 @@ class QuestionsController < InheritedResources::Base | @@ -41,6 +44,8 @@ class QuestionsController < InheritedResources::Base | ||
41 | end | 44 | end |
42 | end | 45 | end |
43 | 46 | ||
47 | + | ||
48 | + | ||
44 | def set_autoactivate_ideas_from_abroad | 49 | def set_autoactivate_ideas_from_abroad |
45 | authenticate | 50 | authenticate |
46 | expire_page :action => :index | 51 | expire_page :action => :index |
@@ -63,6 +68,64 @@ class QuestionsController < InheritedResources::Base | @@ -63,6 +68,64 @@ class QuestionsController < InheritedResources::Base | ||
63 | end | 68 | end |
64 | 69 | ||
65 | end | 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 | end | 129 | end |
67 | 130 | ||
68 | class String | 131 | class String |
app/models/visitor.rb
@@ -14,6 +14,7 @@ class Visitor < ActiveRecord::Base | @@ -14,6 +14,7 @@ class Visitor < ActiveRecord::Base | ||
14 | end | 14 | end |
15 | 15 | ||
16 | def vote_for!(prompt, ordinality) | 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 | question_vote = votes.create!(:voteable_id => prompt.question_id, :voteable_type => "Question") | 18 | question_vote = votes.create!(:voteable_id => prompt.question_id, :voteable_type => "Question") |
18 | logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question_id}" | 19 | logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question_id}" |
19 | 20 |
config/environment.rb
@@ -55,5 +55,8 @@ Rails::Initializer.run do |config| | @@ -55,5 +55,8 @@ Rails::Initializer.run do |config| | ||
55 | config.gem "activemerchant", | 55 | config.gem "activemerchant", |
56 | :lib => 'active_merchant', | 56 | :lib => 'active_merchant', |
57 | :version => '1.4.2' | 57 | :version => '1.4.2' |
58 | + config.gem "fastercsv", | ||
59 | + :lib => 'fastercsv', | ||
60 | + :version => '1.5.1' | ||
58 | end | 61 | end |
59 | 62 |
config/routes.rb
1 | ActionController::Routing::Routes.draw do |map| | 1 | ActionController::Routing::Routes.draw do |map| |
2 | map.resources :clicks | 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 | question.resources :items | 4 | question.resources :items |
5 | question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, | 5 | question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, |
6 | :collection => {:single => :get, :index => :get} | 6 | :collection => {:single => :get, :index => :get} |