Class: Ekylibre::CultivableZonesExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/cultivable_zones_exchanger.rb

Overview

Import a list of cultivable zone from georeadings Prefer ekylibre/cultivable_zones_json to import directly cultivable zones REMOVEME This exchanger is not very useful in standalone mode

Instance Attribute Summary

Attributes inherited from ActiveExchanger::Base

#file, #supervisor

Instance Method Summary collapse

Methods inherited from ActiveExchanger::Base

build, check, check_by_default, exchanger_name, export, exporters, find, human_name, import, import!, importers, importers_selection, inherited, #initialize, register_exchanger

Constructor Details

This class inherits a constructor from ActiveExchanger::Base

Instance Method Details

#importObject

Create or updates cultivable zones


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/exchangers/ekylibre/cultivable_zones_exchanger.rb', line 7

def import
  rows = CSV.read(file, headers: true).delete_if { |r| r[0].blank? }
  w.count = rows.size

  rows.each do |row|
    r = {
      name: row[0].to_s,
      nature: (row[1].blank? ? nil : row[1].to_sym),
      code: (row[2].blank? ? nil : row[2].to_s),
      georeading_number: (row[3].blank? ? nil : row[3].to_s)
      # members: row[4].blank? ? [] : row[4].to_s.strip.split(/[[:space:]]*\,[[:space:]]*/)
    }.to_struct

    georeading = Georeading.find_by(number: r.georeading_number)
    raise "Cannot find georeading: #{r.georeading_number}" unless georeading
    zone = CultivableZone.find_or_initialize_by(work_number: r.code)
    zone.name = r.name
    zone.shape = georeading.content
    zone.save!
    w.check_point
  end
end