module ActiveRecordCsvExtension::ClassMethods

add your static(class) methods here

Public Instance Methods

belongs_to_associations() click to toggle source

Iterate over all the belongs_to associations and see if it has a name. We don’t worry about _id because that by default is on the database table that ActiveRecord is interpreting

# File lib/acts_as_csv.rb, line 57
def belongs_to_associations
  self.reflect_on_all_associations(:belongs_to).collect do |assoc|
    klass = assoc.options[:class_name].try(:constantize) || assoc.name.to_s.classify.constantize
    if klass.column_names.include?('name')
      "#{assoc.name}.name"
    end
  end
end
csv_columns() click to toggle source

Array of methods that we will export out of a model object. Also used as the header row for the CSV file

# File lib/acts_as_csv.rb, line 34
def csv_columns
  columns = column_names + belongs_to_associations + has_one_associations + has_many_associations + optional_csv_attributes - filter_names
  columns.compact!
  columns.uniq!
  columns.flatten!
  columns
end
filter_names() click to toggle source

Lets you filter out sensitive information

# File lib/acts_as_csv.rb, line 44
def filter_names
  ['ssn', 'dob', 'password', 'cc', 'credit_card', 'cvv', 'drivers_license_number', 'drivers_license_state']
end
has_many_associations() click to toggle source

count all the has_many associations methods

# File lib/acts_as_csv.rb, line 82
def has_many_associations
  self.reflect_on_all_associations(:has_many).collect {|assoc| "#{assoc.name}.count"}
end
has_one_associations() click to toggle source

Iterate over all the has_one associations and see if the object has a name attribute else print out the id

# File lib/acts_as_csv.rb, line 69
def has_one_associations
  self.reflect_on_all_associations(:has_one).collect do |assoc|
    klass = assoc.options[:class_name].try(:constantize) || assoc.name.to_s.classify.constantize
    if klass.column_names.include?('name')
      "#{assoc.name}.name"
    else
      "#{assoc.name}.id"
    end
  end
end
optional_csv_attributes() click to toggle source

Lets us add additional csv_columns at the model level. Override in app/moderls/order.rb, etc

# File lib/acts_as_csv.rb, line 50
def optional_csv_attributes
  []
end