Module: ActiveRecord::Extensions::FindToCSV::ClassMethods

Defined in:
lib/ar-extensions/csv.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#find_with_csv(*args) ⇒ Object

:nodoc:



130
131
132
133
134
# File 'lib/ar-extensions/csv.rb', line 130

def find_with_csv( *args ) # :nodoc:
  results = find_without_csv( *args )
  results.extend( ArrayInstanceMethods ) if results.is_a?( Array )
  results
end

#to_csv_fields(options = {}) ⇒ Object

:nodoc:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ar-extensions/csv.rb', line 136

def to_csv_fields( options={} ) # :nodoc:
  fields_to_headers, fields = {}, []
  
  headers = options[:headers]
  case headers
  when Array
    fields = headers.map{ |e| e.to_s }
  when Hash
    headers = headers.inject( {} ){ |hsh,(k,v)| hsh[k.to_s] = v ; hsh }
    fields = headers.keys.sort
    fields.each { |field| fields_to_headers[field] = headers[field] }
  else
    fields = to_csv_fields_for_nil
  end
  
  if options[:only]
    specified_fields = options[:only].map{ |e| e.to_s }
    fields.delete_if{ |field| not specified_fields.include?( field ) }
  elsif options[:except]
    excluded_fields = options[:except].map{ |e| e.to_s }
    fields.delete_if{ |field| excluded_fields.include?( field ) }
  end

  fields.each{ |field| fields_to_headers[field] = field } if fields_to_headers.empty?

  FieldMap.new( fields, fields_to_headers )
end

#to_csv_headers(options = {}) ⇒ Object

Returns an array of CSV headers passed in the array of options.



165
166
167
168
169
170
171
172
173
# File 'lib/ar-extensions/csv.rb', line 165

def to_csv_headers( options={} )
  options = { :headers=>true, :naming=>":header" }.merge( options )
  return nil if not options[:headers]

  fieldmap = to_csv_fields( options )
  headers = fieldmap.headers
  headers.push( *to_csv_headers_for_included_associations( options[ :include ] ).flatten )
  headers.map{ |header| options[:naming].gsub( /:header/, header ).gsub( /:model/, self.name.downcase ) }
end