module ActiveRecordCsvExtension

Extend the ability to create csv from ActiveRecord models

Public Instance Methods

to_csv() click to toggle source

Take an instance and turn it into an array using the csv columns specified at the class level

# File lib/acts_as_csv.rb, line 11
def to_csv
   self.class.csv_columns.collect {|method| try(method) || try_association_methods(method)  }
end
try_association_methods(method) click to toggle source

Attempt to run a has_many count associated method by seeing if there is a chained method in the string with a ‘.’ We use this to support the has_one, belongs_to and has_many column names that we are generating while giving the developer the flexibility to override those methods

# File lib/acts_as_csv.rb, line 19
def try_association_methods(method)
  base_method = method
  chained_method = method.scan(/\.\w{1,}/).try(:first)
  return nil unless chained_method
  base_method.gsub!(chained_method,'')
  chained_method.gsub!('.','')
  try(base_method).try(chained_method)
end