Sha256: 2d8ca97c89609631a3530d0aa906491f1cb51bd2c10600b54ec89f586e6b98d6

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Dataclips
  class InsightsController < ApplicationController
    include ActionController::Live

    def export
      setup_clip

      response.headers['Content-Type'] = 'text/csv'
      response.stream.write CSV.generate(force_quotes: true) { |csv| csv << @headers.values}

      records = @clip.paginate(1)

      response.stream.write CSV.generate(force_quotes: true) { |csv| records.each { |r| csv << r.values } }

      while next_page = records.next_page do
        records = @clip.paginate(next_page)
        response.stream.write CSV.generate(force_quotes: true) { |csv| records.each { |r| csv << r.values } }
      end
    rescue IOError => e
      puts 'Connection closed'
    ensure
      response.stream.close
    end

    def show
      I18n.locale = params[:locale] || I18n.default_locale
      setup_clip

      respond_to do |format|
        format.html
        format.json { process_json(@clip, params[:page]) }
      end
    end

    protected

    def setup_clip
      @insight = Insight.find_by_hash_id(params[:id]) or raise ActiveRecord::RecordNotFound
      @clip_id = @insight.clip_id
      initialize_clip(@clip_id)
      @headers  = localize_headers(@clip_id, @schema.keys)
      @clip = @klass.new @insight.params
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dataclips-0.0.1 app/controllers/dataclips/insights_controller.rb