module CsvRails::ActiveModel::ClassMethods

Public Instance Methods

csv_fields() click to toggle source
# File lib/csv_rails/active_model.rb, line 26
def csv_fields
  if self.is_a?(::ActiveRecord::Relation)
    @klass.attribute_names
  else
    attribute_names
  end
end
csv_header(fields, scope=nil) click to toggle source
# File lib/csv_rails/active_model.rb, line 16
def csv_header(fields, scope=nil)
  fields.map{|f|
    if scope
      I18n.t("#{scope}.#{f}", :default => human_attribute_name(f))
    else
      human_attribute_name(f)
    end
  }
end
to_csv(opts={}) click to toggle source
# File lib/csv_rails/active_model.rb, line 5
def to_csv(opts={})
  fields = opts[:fields] || csv_fields
  header = csv_header(fields, opts.delete(:i18n_scope))
  all = if self.respond_to?(:to_a)
          to_a
        else
          send(:all).to_a
        end
  all.to_csv(opts.update(:fields => fields, :header => header))
end