diff --git a/app/controllers/drawings_controller.rb b/app/controllers/drawings_controller.rb index f8ad5f3..0b55170 100644 --- a/app/controllers/drawings_controller.rb +++ b/app/controllers/drawings_controller.rb @@ -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 diff --git a/app/models/drawing.rb b/app/models/drawing.rb index 4c0394c..7582c6d 100644 --- a/app/models/drawing.rb +++ b/app/models/drawing.rb @@ -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 diff --git a/config/application.rb b/config/application.rb index a22bd83..115e302 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,4 +1,5 @@ require File.expand_path('../boot', __FILE__) +require 'csv' require 'rails/all' # Require the gems listed in Gemfile, including any gems