Skip to content

Commit

Permalink
Merge pull request #116 from empowerhack/csv-hxl-export-spike
Browse files Browse the repository at this point in the history
Temp CSV export functionality to sample some HDX data in HXL format.
  • Loading branch information
krissy authored Oct 3, 2016
2 parents c76f367 + cf9ed16 commit eb71921
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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

0 comments on commit eb71921

Please sign in to comment.