Class: ExportTo::Base

Inherits:
Struct
  • Object
show all
Defined in:
lib/export_to/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject

Returns the value of attribute object.



17
18
19
# File 'lib/export_to/base.rb', line 17

def object
  @object
end

#recordsObject

Returns the value of attribute records

Returns:

  • (Object)

    the current value of records



2
3
4
# File 'lib/export_to/base.rb', line 2

def records
  @records
end

Instance Method Details

#eachObject



33
34
35
36
37
38
39
# File 'lib/export_to/base.rb', line 33

def each
  data = []
  rows! do |columns, model, x|
    data.push(columns)
  end
  data
end

#each! {|self.class.head_titles, nil, i| ... } ⇒ Object

Yields:

  • (self.class.head_titles, nil, i)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/export_to/base.rb', line 41

def each!
  i = 0

  yield(self.class.head_titles, nil, i)

  join_relation = self.class.join_relation

  each_models do |record, x|
    run_records = if join_relation.present? && record.send(join_relation).present?
      record.send(join_relation)
    else
      [ record ]
    end

    # 指定目前 order 讓 subclass 可以 override
    run_records.each_with_index do |run_record, y|
      i = i + 1
      object = if run_record != record
        self.class.presenter_klass.new(record, run_record, x, y)
      else
        self.class.presenter_klass.new(run_record, nil, x, y)
      end

      each_proc.call(object) if self.class.each_proc.present?

      columns = fetch_columns!(object)

      yield(columns, run_record, i)
    end
  end
end

#to_csvObject



19
20
21
# File 'lib/export_to/base.rb', line 19

def to_csv
  ExportTo::Exporter::Csv.new(self).export
end

#to_xlsObject

舊版 Excel



29
30
31
# File 'lib/export_to/base.rb', line 29

def to_xls
  ExportTo::Exporter::Xlsx.new(self).export
end

#to_xlsxObject

新版 Excel



24
25
26
# File 'lib/export_to/base.rb', line 24

def to_xlsx
  ExportTo::Exporter::Xlsx.new(self).export
end