comma-csv / comma

Comma is a small CSV (ie. comma separated values) generation extension for Ruby objects, that lets you seamlessly define a CSV output format via a small DSL

Home Page:https://github.com/comma-csv/comma

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can i dynamically select options for export?

MaksimAbramchuk opened this issue · comments

For example I have model Order

class Order < ActiveRecord::Base
  belongs_to :organization

  comma do
    id
    state

    organization :name
    organization :id  
  end
end

Can i pass any options to csv renderer like {:state, organization: [:id]} to export only this fields?

No, you cannot. You might want to try to use __use___ keyword.

For example, you could define your model like:

class Order < ActiveRecord::Base
  belongs_to :organization

  comma do
    __use__ :base

    id

    organization :name
  end

  comma :base do
    state
    organization :id  
  end
end

Then, Order.where(state: 'done').to_comma(style: :brief) to export only {:state, organization: [:id]}, and to_comma to export all fields.