Module: ActiveRecord::Extensions::FindToCSV::InstanceMethods
- Defined in:
- lib/ar-extensions/csv.rb
Instance Method Summary collapse
-
#to_csv(options = {}) ⇒ Object
Returns CSV data including header rows for the passed in
options
. -
#to_csv_data(options = {}) ⇒ Object
Returns CSV data without any header rows for the passed in
options
.
Instance Method Details
#to_csv(options = {}) ⇒ Object
Returns CSV data including header rows for the passed in options
.
265 266 267 268 269 270 271 |
# File 'lib/ar-extensions/csv.rb', line 265 def to_csv( ={} ) FasterCSV.generate do |csv| headers = self.class.to_csv_headers( ) csv << headers if headers to_csv_data( ).each{ |data| csv << data } end end |
#to_csv_data(options = {}) ⇒ Object
Returns CSV data without any header rows for the passed in options
.
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/ar-extensions/csv.rb', line 251 def to_csv_data( ={} ) fields = self.class.to_csv_fields( ).fields data, model_data = [], fields.inject( [] ) { |arr,field| arr << attributes[field].to_s } if [:include] to_csv_data_for_included_associations( [:include ] ).map do |assoc_csv_data| data << model_data + assoc_csv_data end else data << model_data end data end |