Class: ActiveKit::Export::Exporter

Inherits:
Bedrock::Bedrocker show all
Defined in:
lib/active_kit/export/exporter.rb

Instance Method Summary collapse

Methods inherited from Bedrock::Bedrocker

#create_attribute, #create_describer, #for, #get_describer_names, #initialize, #run_describer_method

Constructor Details

This class inherits a constructor from ActiveKit::Bedrock::Bedrocker

Instance Method Details

#describer_method(describer, params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_kit/export/exporter.rb', line 4

def describer_method(describer, params)
  case describer.kind
  when :csv
    # The 'all' relation must be captured outside the Enumerator,
    # else it will get reset to all the records of the class.
    all_activerecord_relation = @current_class.all.includes(describer.includes)

    Enumerator.new do |yielder|
      ActiveRecord::Base.connected_to(role: :writing, shard: describer.database.call.to_sym) do
        exporting = self.for(describer.name)

        # Add the headings.
        yielder << CSV.generate_line(exporting.headings) if exporting.headings?

        # Add the values.
        # find_each will ignore any order if set earlier.
        all_activerecord_relation.find_each do |record|
          lines = exporting.lines_for(record: record)
          lines.each { |line| yielder << CSV.generate_line(line) }
        end
      end
    end
  end
end