Module: RailsData::Import

Defined in:
lib/rails_data/import.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnsObject (readonly)

extend RailsData::Import config do

model BankPayment
column :amount, header: 'My name', field: -> {}
column :email, header: 'Email', field: -> {}

end



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

def columns
  @columns
end

#recordObject (readonly)

extend RailsData::Import config do

model BankPayment
column :amount, header: 'My name', field: -> {}
column :email, header: 'Email', field: -> {}

end



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

def record
  @record
end

Instance Method Details

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_data/import.rb', line 23

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

  @columns[name] = {}

  if header.is_a?(String) && header.size > 0
    @columns[name][:header] = header
  else
    warn 'wrong header type'
  end

  if field.respond_to?(:call)
    @columns[name][:field] = field
  end

  self
end

#config(&block) ⇒ Object



11
12
13
# File 'lib/rails_data/import.rb', line 11

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

#model(class_name) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rails_data/import.rb', line 15

def model(class_name)
  if class_name.respond_to?(:ancestors) && class_name.ancestors.include?(ActiveRecord::Base)
    @record = class_name
  else
    raise 'The collection must be a subclass of ActiveRecord::Base'
  end
end