Module: Plutonium::Resource::Controllers::ExportCsv
- Extended by:
- ActiveSupport::Concern
- Included in:
- Plutonium::Resource::Controller
- Defined in:
- lib/plutonium/resource/controllers/export_csv.rb
Overview
Streams the current resource collection as a CSV download.
Auto-mounted on every Plutonium resource via the ‘interactive_resource_actions` routing concern (see Plutonium::Routing::MapperExtensions). Gated by the `export_csv?` policy method, which defaults to `false` — export is strictly opt-in (enable it by overriding `export_csv?` to return true).
The exported rows are exactly the index’s filtered collection (‘filtered_resource_collection`) — same search, filters, scope, and tenant/parent scoping — but NOT paginated: every matching record is exported. Rows are streamed (a lazy Enumerator body + `find_each`) so memory stays flat regardless of row count.
Columns come from ‘policy.permitted_attributes_for_export` (defaults to the index columns), with the primary key always prepended as the first column. Per-field output and headers are customizable through the definition’s ‘export` DSL.
‘find_each` iterates in primary-key order, so the file does not preserve the index’s current sort (filters/search/scope still apply).
Streaming uses a lazy Enumerator response body rather than ‘send_stream` — the latter lives in ActionController::Live, which would turn every resource action into a threaded streaming response. The Enumerator body streams through Rack on its own.
Constant Summary collapse
- INVALID_COLUMN =
Placeholder written when a column is neither an ‘export` block nor a real attribute on the record, so the export degrades to a usable file instead of a mid-stream NoMethodError (which would truncate the already-committed download).
"<<invalid column>>"
Instance Method Summary collapse
-
#export_csv ⇒ Object
GET /<resources>/export_csv.
Instance Method Details
#export_csv ⇒ Object
GET /<resources>/export_csv
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/plutonium/resource/controllers/export_csv.rb', line 52 def export_csv response.headers["Content-Type"] = "text/csv; charset=utf-8" response.headers["Content-Disposition"] = ActionDispatch::Http::ContentDisposition.format(disposition: "attachment", filename: export_csv_filename) # Defeat proxy/`Rack::ETag` buffering so rows flush as they're read. response.headers["X-Accel-Buffering"] = "no" response.headers["Cache-Control"] = "no-cache" self.response_body = export_csv_lines end |