Skip to content

Commit

Permalink
Fixes exchangers transformed by Rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice TEXIER committed Mar 14, 2016
1 parent 1b328bd commit 8f1c3bd
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 121 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Expand Up @@ -229,7 +229,7 @@ GEM
nori (2.6.0)
ntlm-http (0.1.1)
orm_adapter (0.5.0)
paperclip (4.3.5)
paperclip (4.3.6)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
cocaine (~> 0.5.5)
Expand Down Expand Up @@ -282,7 +282,7 @@ GEM
thor (>= 0.18.1, < 2.0)
rainbow (2.1.0)
raindrops (0.16.0)
rake (11.1.0)
rake (11.1.1)
rdoc (4.2.2)
json (~> 1.4)
redis (3.2.2)
Expand Down
133 changes: 68 additions & 65 deletions app/exchangers/ekylibre/equipments_exchanger.rb
@@ -1,80 +1,83 @@
class Ekylibre::EquipmentsExchanger < ActiveExchanger::Base
# Create or updates equipments
def import
building_division = BuildingDivision.first
unless building_division
w.warn 'A default BuildingDivision should help to define default storage for equipments'
end
module Ekylibre
class EquipmentsExchanger < ActiveExchanger::Base
# Create or updates equipments
def import
building_division = BuildingDivision.first
unless building_division
w.warn 'A default BuildingDivision should help to define default storage for equipments'
end

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

rows.each do |row|
r = {
name: row[0].blank? ? nil : row[0].to_s,
variant_reference_name: row[1].blank? ? nil : row[1].downcase.to_sym,
work_number: row[2].blank? ? nil : row[2].to_s,
place_code: row[3].blank? ? nil : row[3].to_s,
born_at: (row[4].blank? ? Date.civil(2000, 2, 2) : row[4]).to_datetime,
brand: row[5].blank? ? nil : row[5].to_s,
model: row[6].blank? ? nil : row[6].to_s,
external: !row[7].blank?,
owner_name: row[7].blank? ? nil : row[7].to_s,
indicators: row[8].blank? ? {} : row[8].to_s.strip.split(/[[:space:]]*\;[[:space:]]*/).collect { |i| i.split(/[[:space:]]*\:[[:space:]]*/) }.inject({}) do |h, i|
h[i.first.strip.downcase.to_sym] = i.second
h
end,
notes: row[9].blank? ? nil : row[9].to_s,
unit_pretax_amount: row[10].blank? ? nil : row[10].to_d,
price_indicator: row[11].blank? ? nil : row[11].to_sym
}.to_struct
rows.each do |row|
r = {
name: row[0].blank? ? nil : row[0].to_s,
variant_reference_name: row[1].blank? ? nil : row[1].downcase.to_sym,
work_number: row[2].blank? ? nil : row[2].to_s,
place_code: row[3].blank? ? nil : row[3].to_s,
born_at: (row[4].blank? ? Date.civil(2000, 2, 2) : row[4]).to_datetime,
brand: row[5].blank? ? nil : row[5].to_s,
model: row[6].blank? ? nil : row[6].to_s,
external: !row[7].blank?,
owner_name: row[7].blank? ? nil : row[7].to_s,
indicators: row[8].blank? ? {} : row[8].to_s.strip.split(/[[:space:]]*\;[[:space:]]*/).collect { |i| i.split(/[[:space:]]*\:[[:space:]]*/) }.inject({}) do |h, i|
h[i.first.strip.downcase.to_sym] = i.second
h
end,
notes: row[9].blank? ? nil : row[9].to_s,
unit_pretax_amount: row[10].blank? ? nil : row[10].to_d,
price_indicator: row[11].blank? ? nil : row[11].to_sym
}.to_struct

if r.variant_reference_name
# find or import from variant reference_nameclature the correct ProductNatureVariant
variant = ProductNatureVariant.import_from_nomenclature(r.variant_reference_name)
pmodel = variant.matching_model
if r.variant_reference_name
# find or import from variant reference_nameclature the correct ProductNatureVariant
variant = ProductNatureVariant.import_from_nomenclature(r.variant_reference_name)
pmodel = variant.matching_model

# create a price
if r.unit_pretax_amount && catalog = Catalog.where(usage: :cost).first && variant.catalog_items.where(catalog_id: catalog.id).empty?
variant.catalog_items.create!(catalog: catalog, all_taxes_included: false, amount: r.unit_pretax_amount, currency: 'EUR') # , indicator_name: r.price_indicator.to_s
end
# create a price
catalog = Catalog.find_by(usage: :cost)
if r.unit_pretax_amount && catalog && catalog.items.where(variant: variant).empty?
variant.catalog_items.create!(catalog: catalog, all_taxes_included: false, amount: r.unit_pretax_amount, currency: 'EUR') # , indicator_name: r.price_indicator.to_s
end

# create the owner if not exist
if r.external == true
owner = Entity.where(last_name: r.owner_name.to_s).first
owner ||= Entity.create!(born_at: Time.zone.today, last_name: r.owner_name.to_s, currency: Preference[:currency], language: Preference[:language], nature: :organization)
else
owner = Entity.of_company
end
# create the owner if not exist
if r.external == true
owner = Entity.where(last_name: r.owner_name.to_s).first
owner ||= Entity.create!(born_at: Time.zone.today, last_name: r.owner_name.to_s, currency: Preference[:currency], language: Preference[:language], nature: :organization)
else
owner = Entity.of_company
end

container = Product.find_by_work_number(r.place_code)
container ||= building_division
container = Product.find_by_work_number(r.place_code)
container ||= building_division

# create the equipment
equipment = pmodel.create!(variant_id: variant.id, name: r.name, initial_born_at: r.born_at, initial_owner: owner, initial_container: container, default_storage: container, work_number: r.work_number)
# create the equipment
equipment = pmodel.create!(variant_id: variant.id, name: r.name, initial_born_at: r.born_at, initial_owner: owner, initial_container: container, default_storage: container, work_number: r.work_number)

# create indicators linked to equipment
r.indicators.each do |indicator, value|
if indicator.to_sym == :population
equipment.move!(value, at: r.born_at)
else
equipment.read!(indicator, value, at: r.born_at, force: true)
# create indicators linked to equipment
r.indicators.each do |indicator, value|
if indicator.to_sym == :population
equipment.move!(value, at: r.born_at)
else
equipment.read!(indicator, value, at: r.born_at, force: true)
end
end
end

# attach georeading if exist for equipment
if equipment
if georeading = Georeading.find_by(number: r.work_number, nature: :polygon)
equipment.read!(:shape, georeading.content, at: r.born_at, force: true)
end
if georeading = Georeading.find_by(number: r.work_number, nature: :point)
equipment.read!(:geolocation, georeading.content, at: r.born_at, force: true)
# attach georeading if exist for equipment
if equipment
if georeading = Georeading.find_by(number: r.work_number, nature: :polygon)
equipment.read!(:shape, georeading.content, at: r.born_at, force: true)
end
if georeading = Georeading.find_by(number: r.work_number, nature: :point)
equipment.read!(:geolocation, georeading.content, at: r.born_at, force: true)
end
end
end

w.check_point
else
w.warn "Need a Variant for #{r.name}"
w.check_point
else
w.warn "Need a Variant for #{r.name}"
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/exchangers/ekylibre/matters_exchanger.rb
Expand Up @@ -89,7 +89,8 @@ def import
pmodel = variant.nature.matching_model

# create a price
if r.unit_pretax_amount && catalog = Catalog.where(usage: :cost).first && variant.catalog_items.where(catalog_id: catalog.id).empty?
catalog = Catalog.find_by(usage: :cost)
if r.unit_pretax_amount && catalog && catalog.items.where(variant: variant).empty?
variant.catalog_items.create!(catalog: catalog, all_taxes_included: false, amount: r.unit_pretax_amount, currency: 'EUR')
end

Expand Down
2 changes: 1 addition & 1 deletion app/exchangers/ekylibre/settings_exchanger.rb
Expand Up @@ -269,7 +269,7 @@ def create_records(records, *args)
attributes = attributes.with_indifferent_access
attributes[main_column] ||= identifier.to_s
model.reflect_on_all_associations.each do |reflection|
if attributes[reflection.name] && (attributes[reflection.name].class >= ActiveRecord::Base)
if attributes[reflection.name] && !attributes[reflection.name].is_a?(ActiveRecord::Base)
attributes[reflection.name] = find_record(reflection.class_name.tableize, attributes[reflection.name].to_s)
end
end
Expand Down
107 changes: 55 additions & 52 deletions app/exchangers/ekylibre/workers_exchanger.rb
@@ -1,68 +1,71 @@
class Ekylibre::WorkersExchanger < ActiveExchanger::Base
def import
building_division = BuildingDivision.first
module Ekylibre
class WorkersExchanger < ActiveExchanger::Base
def import
building_division = BuildingDivision.first

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

rows.each do |row|
r = {
name: row[0].blank? ? nil : row[0].to_s,
first_name: row[1],
last_name: row[2],
variant_reference_name: row[3].downcase.to_sym,
work_number: row[4],
place_code: row[5],
born_at: (row[6].blank? ? Date.civil(1980, 2, 2) : Date.parse(row[6]).to_datetime),
notes: row[7].to_s,
unit_pretax_amount: row[8].blank? ? nil : row[8].to_d,
price_indicator: row[9].blank? ? nil : row[9].to_sym,
email: row[10]
}.to_struct
rows.each do |row|
r = {
name: row[0].blank? ? nil : row[0].to_s,
first_name: row[1],
last_name: row[2],
variant_reference_name: row[3].downcase.to_sym,
work_number: row[4],
place_code: row[5],
born_at: (row[6].blank? ? Date.civil(1980, 2, 2) : Date.parse(row[6]).to_datetime),
notes: row[7].to_s,
unit_pretax_amount: row[8].blank? ? nil : row[8].to_d,
price_indicator: row[9].blank? ? nil : row[9].to_sym,
email: row[10]
}.to_struct

# Find or import from variant reference_name the correct ProductNatureVariant
unless variant = ProductNatureVariant.find_by(reference_name: r.variant_reference_name)
variant = ProductNatureVariant.import_from_nomenclature(r.variant_reference_name)
end
pmodel = variant.matching_model
# Find or import from variant reference_name the correct ProductNatureVariant
unless variant = ProductNatureVariant.find_by(reference_name: r.variant_reference_name)
variant = ProductNatureVariant.import_from_nomenclature(r.variant_reference_name)
end
pmodel = variant.matching_model

# create a price
if r.unit_pretax_amount && catalog = Catalog.where(usage: :cost).first && variant.catalog_items.where(catalog_id: catalog.id).empty?
variant.catalog_items.create!(catalog: catalog, all_taxes_included: false, amount: r.unit_pretax_amount, currency: 'EUR') # , indicator_name: r.price_indicator.to_s
end
# create a price
catalog = Catalog.find_by(usage: :cost)
if r.unit_pretax_amount && catalog && catalog.items.where(variant: variant).empty?
variant.catalog_items.create!(catalog: catalog, all_taxes_included: false, amount: r.unit_pretax_amount, currency: 'EUR') # , indicator_name: r.price_indicator.to_s
end

# create the owner if not exist
unless person = Entity.contacts.find_by(first_name: r.first_name, last_name: r.last_name)
person = Entity.create!(first_name: r.first_name, last_name: r.last_name, born_at: r.born_at, nature: :contact)
end
# create the owner if not exist
unless person = Entity.contacts.find_by(first_name: r.first_name, last_name: r.last_name)
person = Entity.create!(first_name: r.first_name, last_name: r.last_name, born_at: r.born_at, nature: :contact)
end

person.emails.find_or_create_by!(coordinate: r.email) if r.email.present?
person.emails.find_or_create_by!(coordinate: r.email) if r.email.present?

# create the user
if person && r.email.present? && !User.where(person_id: person.id).any?
unless user = User.find_by(email: r.email)
password = User.generate_password(100, :hard)
user = User.create!(first_name: r.first_name, last_name: r.last_name, person: person, email: r.email, password: password, password_confirmation: password, language: Preference[:language], role: Role.order(:id).first)
# create the user
if person && r.email.present? && !User.where(person_id: person.id).any?
unless user = User.find_by(email: r.email)
password = User.generate_password(100, :hard)
user = User.create!(first_name: r.first_name, last_name: r.last_name, person: person, email: r.email, password: password, password_confirmation: password, language: Preference[:language], role: Role.order(:id).first)
end
unless user.person
user.person = person
user.save!
end
end
unless user.person
user.person = person
user.save!
end
end

owner = Entity.of_company
owner = Entity.of_company

container = Product.find_by(work_number: r.place_code) || building_division
container = Product.find_by(work_number: r.place_code) || building_division

# create the worker
worker = pmodel.create!(variant: variant, name: r.name, initial_born_at: r.born_at, initial_owner: owner, default_storage: container, work_number: r.work_number, person: person)
# create the worker
worker = pmodel.create!(variant: variant, name: r.name, initial_born_at: r.born_at, initial_owner: owner, default_storage: container, work_number: r.work_number, person: person)

# attach georeading if exist for worker
if georeading = Georeading.find_by(number: r.work_number, nature: :point)
worker.read!(:geolocation, georeading.content, at: r.born_at, force: true)
end
# attach georeading if exist for worker
if georeading = Georeading.find_by(number: r.work_number, nature: :point)
worker.read!(:geolocation, georeading.content, at: r.born_at, force: true)
end

w.check_point
w.check_point
end
end
end
end

0 comments on commit 8f1c3bd

Please sign in to comment.