Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temp CSV export functionality to sample some HDX data in HXL format. #116

Merged
merged 2 commits into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/controllers/drawings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ class DrawingsController < ApplicationController
before_action :check_access_to_drawing, only: [:edit, :update, :destroy]

def index
@drawings = (Drawing.desc.page params[:page]).decorate
drawings = (Drawing.desc.page params[:page])
@drawings = drawings.decorate

respond_to do |format|
format.html
format.js
format.json { render json: @drawings }
format.csv { render text: drawings.to_csv(hxl: params[:hxl].present?) }
end
end

Expand Down
15 changes: 15 additions & 0 deletions app/models/drawing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ class Drawing < ActiveRecord::Base

scope :desc, -> { order("drawings.created_at DESC") }

def self.to_csv(hxl: false)
fields = %w(org country age gender mood_rating description story created_at)

CSV.generate do |csv|
csv << fields.map(&:capitalize)
csv << fields.map { |field| "#" + field } if hxl

# Only export completed entries
complete.each do |drawing|
values = drawing.attributes.values_at(*fields.drop(1))
csv << values.unshift(drawing.user.organisation.name)
end
end
end

def viewer_can_change?(viewer)
viewer.organisation == user.organisation
end
Expand Down
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.expand_path('../boot', __FILE__)
require 'csv'
require 'rails/all'

# Require the gems listed in Gemfile, including any gems
Expand Down