Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #38 from gurix/features/export_data
Browse files Browse the repository at this point in the history
As an administrator, I want to export my data
  • Loading branch information
gurix committed Jun 24, 2014
2 parents aa97429 + d471c3d commit 5ce9930
Show file tree
Hide file tree
Showing 29 changed files with 260 additions and 73 deletions.
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
PATH
remote: .
specs:
helena (0.2.2)
helena (0.3.0)
bootstrap-sass
breadcrumbs_on_rails
haml
haml-rails
jquery-rails
liquid
mongoid (~> 4.0.0.beta1)
mongoid (~> 4.0.0.rc2)
mongoid-simple-tags
mongoid_orderable
rails (~> 4.1.1)
Expand Down Expand Up @@ -107,15 +107,15 @@ GEM
highline (1.6.21)
hike (1.2.3)
i18n (0.6.9)
i18n-tasks (0.4.5)
i18n-tasks (0.5.1)
activesupport
easy_translate (>= 0.5.0)
erubis
highline
slop (>= 3.5.0)
term-ansicolor
terminal-table
jquery-rails (3.1.0)
jquery-rails (3.1.1)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
Expand All @@ -129,9 +129,9 @@ GEM
mime-types (1.25.1)
mini_portile (0.6.0)
minitest (5.3.5)
mongoid (4.0.0.rc2)
mongoid (4.0.0)
activemodel (~> 4.0)
moped (~> 2.0.0.rc2)
moped (~> 2.0.0)
origin (~> 2.1)
tzinfo (>= 0.3.37)
mongoid-rspec (1.10.0)
Expand All @@ -143,7 +143,7 @@ GEM
mongoid (>= 3.0.3)
mongoid_orderable (4.1.0)
mongoid
moped (2.0.0.rc2)
moped (2.0.0)
bson (~> 2.2)
connection_pool (~> 2.0)
optionable (~> 0.2.0)
Expand Down Expand Up @@ -207,7 +207,7 @@ GEM
rspec-expectations (~> 3.0.0)
rspec-mocks (~> 3.0.0)
rspec-support (~> 3.0.0)
rspec-support (3.0.1)
rspec-support (3.0.2)
ruby-progressbar (1.5.1)
rubyzip (1.1.4)
sass (3.2.19)
Expand Down
29 changes: 29 additions & 0 deletions app/assets/images/helena/icons/export.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/assets/stylesheets/helena/admin/layout.css.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.helena-admin
.actions
font-size: 18pt

.icon
width: 70px
5 changes: 0 additions & 5 deletions app/controllers/helena/admin/question_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def create

if @question_group.save
notify_successful_create_for(@question_group.title)
else
notify_error
end
respond_with @question_group, location: admin_survey_question_groups_path(@survey)
end
Expand All @@ -37,9 +35,6 @@ def update

if @question_group.update_attributes question_group_params
notify_successful_update_for(@question_group.title)
else
notify_error
add_breadcrumb @question_group.title_was
end
respond_with @question_group, location: admin_survey_question_groups_path(@survey)
end
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/helena/admin/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class SessionsController < Admin::ApplicationController
before_filter :load_survey, :add_breadcrumbs

def index
@sessions = @survey.sessions.desc(:created_at)
respond_to do |format|
@sessions = @survey.sessions.desc(:created_at)
format.html
format.json { render json: @sessions }
format.csv { render text: @sessions.to_csv }
end
end

def destroy
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/helena/admin/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def update

def destroy
@version = @survey.versions.find_by id: params[:id]
notify_successful_delete_for(@version.version) if @version.destroy
notify_successful_delete_for(@version.version) if @version.destroy && Helena::Session.where(version: @version).destroy
respond_with @version, location: admin_survey_versions_path(@survey)
end

Expand Down
31 changes: 31 additions & 0 deletions app/models/helena/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,39 @@ def reset_tokens
self.view_token = generate_token(25) until unique_token_for?(:view_token)
end

def self.to_csv
CSV.generate do |csv|
csv << fields.keys + uniq_answer_codes
all.each do |session|
csv << session.attributes.values_at(*fields.keys) + answer_values_in(session)
end
end
end

private

def self.answer_values_in(session)
answers = []
answer_codes.each do |code|
answers << session.answers.where(code: code).first.try(&:value)
end
answers
end

def self.answer_codes
answer_codes = []
all.each do |session|
answer_codes += session.answers.map(&:code) - answer_codes
end
answer_codes
end

def self.uniq_answer_codes
answer_codes.map do |code|
fields.keys.include?(code) ? "answer_#{code}" : code
end
end

def generate_token(size)
SecureRandom.base64(size).delete('/+=')[0, size]
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/helena/admin/question_groups/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
%tbody
- @question_groups.each do |question_group|
%tr[question_group]
%td.position{ data: { title: Helena::QuestionGroup.human_attribute_name(:position) } }
%td.position
= question_group.position
%td.title{ data: { title: Helena::QuestionGroup.human_attribute_name(:title) } }
%td.title
= question_group.title
%td
.btn-group.btn-group-sm
Expand Down
8 changes: 4 additions & 4 deletions app/views/helena/admin/questions/_labels.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
%tbody
= form.simple_fields_for :labels do |label|
%tr[label.object]
%td.position{ data: { title: Helena::Label.human_attribute_name(:position) } }
%td.position
= label.input :position, as: :integer, label: false
%td.preselected{ data: { title: Helena::Label.human_attribute_name(:preselected) } }
%td.preselected
= label.input :preselected, as: :boolean, wrapper: :checkbox, label: Helena::Label.human_attribute_name(:preselected)
%td.text{ data: { title: Helena::Label.human_attribute_name(:text) } }
%td.text
= label.input :text, label: false
%td.value{ data: { title: Helena::Label.human_attribute_name(:value) } }
%td.value
= label.input :value, label: false
%td.delete
- unless label.object.new_record?
Expand Down
10 changes: 5 additions & 5 deletions app/views/helena/admin/questions/_sub_questions.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
%tbody
= form.simple_fields_for :sub_questions do |sub_question|
%tr[sub_question.object]
%td.position{ data: { title: Helena::SubQuestion.human_attribute_name(:position) } }
%td.position
= sub_question.input :position, as: :integer, label: false
%td.code{ data: { title: Helena::SubQuestion.human_attribute_name(:code) } }
%td.code
= sub_question.input :code, label: false, hint: t('shared.code_hint')
- if @question.is_a?(Helena::Questions::CheckboxGroup)
%td.preselected{ data: { title: Helena::SubQuestion.human_attribute_name(:preselected) } }
%td.preselected
= sub_question.input :preselected, as: :boolean, wrapper: :checkbox
%td.text{ data: { title: Helena::SubQuestion.human_attribute_name(:text) } }
%td.text
= sub_question.input :text, label: false
- if @question.is_a?(Helena::Questions::CheckboxGroup)
%td.value{ data: { title: Helena::SubQuestion.human_attribute_name(:value) } }
%td.value
= sub_question.input :value, label: false, as: :hidden, value: 1
%td.delete
- unless sub_question.object.new_record?
Expand Down
8 changes: 4 additions & 4 deletions app/views/helena/admin/questions/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
%tbody
- @questions.each_with_index do |question, index|
%tr[question]
%td.position{ data: { title: Helena::Question.human_attribute_name(:position) } }
%td.position
= question.position
%td.code{ data: { title: Helena::Question.human_attribute_name(:code) } }
%td.code
= question.code
%td.type{ data: { title: Helena::Question.human_attribute_name(:type) } }
%td.type
= question.class.model_name.human
- if question.try :required
%span.glyphicon.glyphicon-flag{ title: Helena::Question.human_attribute_name(:require) }
%td.question_text{ data: { title: Helena::Question.human_attribute_name(:question_text) } }
%td.question_text
= question.question_text
%td
.btn-group.btn-group-sm
Expand Down
23 changes: 16 additions & 7 deletions app/views/helena/admin/sessions/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
.helena-admin
%h2
= Helena::Session.model_name.human(count: 2)
= "(#{@sessions.count})"
- if @sessions.empty?
.alert.alert-warning
= t '.empty'
- else
.actions
= link_to admin_survey_sessions_path(@survey, format: :csv) do
= image_tag('helena/icons/export.svg', class: :icon)
= t('.export_sessions_csv')
= link_to admin_survey_sessions_path(@survey, format: :json) do
= t('.export_sessions_json')
%h2
= Helena::Session.model_name.human(count: 2)
= "(#{@sessions.count})"
.table-responsive
%table.table.sessions
%thead
%tr
%th.updated_at= Helena::Session.human_attribute_name(:updated_at)
%th.token= Helena::Session.human_attribute_name(:token)
%th.completed= Helena::Session.human_attribute_name(:completed)
%th.version= Helena::Session.human_attribute_name(:version)
%th.answers= Helena::Session.human_attribute_name(:answers)
%th
%tbody
- @sessions.each do |session|
%tr[session]
%td.updated_at{ data: { title: Helena::Session.human_attribute_name(:updated_at) } }
%td.updated_at
= time_ago_in_words(session.updated_at)
%td.token{ data: { title: Helena::Session.human_attribute_name(:token) } }
%td.token
= session.token
%td.completed{ data: { title: Helena::Session.human_attribute_name(:completed) } }
%td.completed
.label{class: "label-#{session.completed? ? 'success' : 'danger'}"}
= t ".#{session.completed}"
%td.answers{ data: { title: Helena::Session.human_attribute_name(:answers) } }
%td.version
= @survey.versions.find(session.version_id).version if session.version_id
%td.answers
= session.answers.count

%td
Expand Down
9 changes: 6 additions & 3 deletions app/views/helena/admin/versions/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
%tr
%th.version= Helena::Version.human_attribute_name(:version)
%th.notes= Helena::Version.human_attribute_name(:notes)
%th.sessions= Helena::Session.model_name.human(count: 2)
%th.created_at= Helena::Version.human_attribute_name(:created_at)
%th
%tbody
- @versions.each do |version|
%tr[version]
%td.version{ data: { title: Helena::Version.human_attribute_name(:version) } }
%td.version
= version.version
%td.notes{ data: { title: Helena::Version.human_attribute_name(:notes) } }
%td.notes
= version.notes
%td.created_at{ data: { title: Helena::Version.human_attribute_name(:created_at) } }
%td.sessions
= Helena::Session.where(version: version).count
%td.created_at
= time_ago_in_words(version.created_at)
%td
.btn-group
Expand Down
2 changes: 2 additions & 0 deletions config/locales/views/admin/sessions/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ en:
'false': "No"
'true': "Yes"
edit: Edit
export_sessions_csv: I want to export all session from this survey to my local computer as CSV
export_sessions_json: ... or as JSON
4 changes: 2 additions & 2 deletions helena.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ survey/test development, longitudinal studies and instant feedback.'
s.test_files = `git ls-files -- {spec}/*`.split("\n")

s.add_dependency 'rails', '~> 4.1.1'
s.add_dependency 'mongoid', '~> 4.0.0.beta1'
s.add_dependency 'mongoid', '~> 4.0.0.rc2'
s.add_dependency 'mongoid_orderable'
s.add_dependency 'mongoid-simple-tags'
s.add_dependency 'haml'
Expand All @@ -33,7 +33,7 @@ survey/test development, longitudinal studies and instant feedback.'
s.add_dependency 'liquid'

s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'rspec-collection_matchers'
s.add_development_dependency 'rspec-collection_matchers'
s.add_development_dependency 'mongoid-rspec'
s.add_development_dependency 'factory_girl_rails'
s.add_development_dependency 'database_cleaner'
Expand Down
1 change: 1 addition & 0 deletions lib/helena.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'mongoid_orderable'
require 'mongoid-simple-tags'
require 'liquid'
require 'csv'

module Helena
end
2 changes: 1 addition & 1 deletion lib/helena/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Helena
VERSION = '0.2.2'
VERSION = '0.3.0'
end
2 changes: 2 additions & 0 deletions lib/helena/version_publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ def self.publish(version)
copied_version = version.dup
copied_version.survey = version.survey
copied_version.version = newest_version_of(version.survey) + 1
copied_version.created_at = DateTime.now
copied_version.updated_at = DateTime.now
copied_version
end

Expand Down
Loading

0 comments on commit 5ce9930

Please sign in to comment.