Module: RailsData::Export

Defined in:
lib/rails_data/export.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collectionObject (readonly)

extend RailsData::Export config do

collect -> (params) { User.default_where(params) }
column :name, header: 'My name', field: -> {}
column :email, header: 'Email', field: -> {}

end



9
10
11
# File 'lib/rails_data/export.rb', line 9

def collection
  @collection
end

#columnsObject (readonly)

extend RailsData::Export config do

collect -> (params) { User.default_where(params) }
column :name, header: 'My name', field: -> {}
column :email, header: 'Email', field: -> {}

end



9
10
11
# File 'lib/rails_data/export.rb', line 9

def columns
  @columns
end

#parametersObject (readonly)

extend RailsData::Export config do

collect -> (params) { User.default_where(params) }
column :name, header: 'My name', field: -> {}
column :email, header: 'Email', field: -> {}

end



9
10
11
# File 'lib/rails_data/export.rb', line 9

def parameters
  @parameters
end

Instance Method Details

#collect(collection) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/rails_data/export.rb', line 17

def collect(collection)
  @collection = collection
  @parameters ||= []
  if collection.respond_to?(:call)
    _params = collection.parameters.to_combined_h
    @parameters << _params[:key] if _params[:key]
  end
end

#column(name, header: nil, field: nil, footer: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rails_data/export.rb', line 26

def column(name, header: nil, field: nil, footer: nil)
  @columns ||= {}
  name = name.to_sym

  if @columns.keys.include?(name)
    warn 'The column is repeated'
  end

  @columns[name] = {}

  if header.nil?
    @columns[name][:header] = name.titleize
  else
    @columns[name][:header] = header
  end
  @columns[name][:field] = field
  @columns[name][:footer] = footer if footer

  if field.respond_to?(:call)
    _params = field.parameters.to_combined_h
    @parameters << _params[:key] if _params[:key]
  end

  self
end

#config(*args, &block) ⇒ Object



13
14
15
# File 'lib/rails_data/export.rb', line 13

def config(*args, &block)
  block.call(*args) if block_given?
end