Skip to content

Instantly share code, notes, and snippets.

@ihatov08
Forked from stereoscott/active_admin.rb
Created March 1, 2017 00:08
Show Gist options
  • Save ihatov08/828a8923dedd353abe49e5f7545529fb to your computer and use it in GitHub Desktop.
Save ihatov08/828a8923dedd353abe49e5f7545529fb to your computer and use it in GitHub Desktop.

Revisions

  1. @stereoscott stereoscott revised this gist Oct 17, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion csv_stream.rb
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ def index

    def render_csv
    set_csv_headers
    self_response_body = csv_lines
    self.response_body = csv_lines
    end

    def set_csv_headers
  2. @stereoscott stereoscott revised this gist Oct 17, 2013. 2 changed files with 20 additions and 14 deletions.
    4 changes: 2 additions & 2 deletions active_admin.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # if you want to monkey patch every controller, put this in initializers/active_admin.rb
    class ActiveAdmin::ResourceController
    ActiveAdmin::ResourceController.class_eval do
    include ActiveAdmin::CSVStream
    end
    end
    30 changes: 18 additions & 12 deletions csv_stream.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    # put in an autoloaded path, like lib/active_admin

    require 'csv'

    module ActiveAdmin

    module CSVStream

    include ActionController::Live
    # using ActionController::Live breaks warden
    # see https://github.com/plataformatec/devise/issues/2332
    # include ActionController::Live

    def apply_pagination(chain)
    chain = super unless formats.include?(:csv)
    @@ -22,7 +24,7 @@ def index

    def render_csv
    set_csv_headers
    csv_lines
    self_response_body = csv_lines
    end

    def set_csv_headers
    @@ -33,17 +35,21 @@ def csv_lines
    default = ActiveAdmin.application.csv_options
    options = default.merge active_admin_config.csv_builder.options
    columns = active_admin_config.csv_builder.columns

    CSV.generate(options) do |csv|
    response.stream.write columns.map(&:name).to_csv
    collection.each do |resource|
    row = columns.map do |column|
    call_method_or_proc_on resource, column.data
    end.to_csv
    response.stream.write row

    Enumerator.new do |y|
    CSV.generate(options) do |csv|
    y << columns.map(&:name).to_csv
    #response.stream.write columns.map(&:name).to_csv
    collection.each do |resource|
    row = columns.map do |column|
    call_method_or_proc_on resource, column.data
    end.to_csv
    y << row
    #response.stream.write row
    end
    end
    end
    response.stream.close
    #response.stream.close
    end

    end
  3. @stereoscott stereoscott revised this gist Oct 15, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion active_admin.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # if you want to monkey patch every controller, put this in initializers/active_admin.rb
    module ActiveAdmin::ResourceController
    class ActiveAdmin::ResourceController
    include ActiveAdmin::CSVStream
    end
  4. @stereoscott stereoscott revised this gist Oct 15, 2013. 3 changed files with 8 additions and 2 deletions.
    4 changes: 4 additions & 0 deletions active_admin.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # if you want to monkey patch every controller, put this in initializers/active_admin.rb
    module ActiveAdmin::ResourceController
    include ActiveAdmin::CSVStream
    end
    4 changes: 3 additions & 1 deletion csv_stream.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # put in an autoload path, like lib/active_admin
    # put in an autoloaded path, like lib/active_admin

    require 'csv'
    module ActiveAdmin

    module CSVStream
    2 changes: 1 addition & 1 deletion users.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #admin/users.rb
    #admin/users.rb to use in a single controller
    ActiveAdmin.register User do
    controller do
    include ActiveAdmin::CSVStream
  5. @stereoscott stereoscott revised this gist Oct 15, 2013. 2 changed files with 7 additions and 0 deletions.
    1 change: 1 addition & 0 deletions csv_stream.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # put in an autoload path, like lib/active_admin
    module ActiveAdmin

    module CSVStream
    6 changes: 6 additions & 0 deletions users.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #admin/users.rb
    ActiveAdmin.register User do
    controller do
    include ActiveAdmin::CSVStream
    end
    end
  6. @stereoscott stereoscott created this gist Oct 15, 2013.
    48 changes: 48 additions & 0 deletions csv_stream.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    module ActiveAdmin

    module CSVStream

    include ActionController::Live

    def apply_pagination(chain)
    chain = super unless formats.include?(:csv)
    chain
    end

    def index
    index! do |format|
    format.csv { render_csv }
    end
    end

    private

    def render_csv
    set_csv_headers
    csv_lines
    end

    def set_csv_headers
    headers["Content-Type"] = "text/csv"
    end

    def csv_lines
    default = ActiveAdmin.application.csv_options
    options = default.merge active_admin_config.csv_builder.options
    columns = active_admin_config.csv_builder.columns

    CSV.generate(options) do |csv|
    response.stream.write columns.map(&:name).to_csv
    collection.each do |resource|
    row = columns.map do |column|
    call_method_or_proc_on resource, column.data
    end.to_csv
    response.stream.write row
    end
    end
    response.stream.close
    end

    end

    end