From eb43c4be21acdef9f665b7adb8a4e78e601deb78 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 21 Sep 2019 23:48:59 +0200 Subject: [PATCH] Import fixes --- app/controllers/ingredients_controller.rb | 9 ++++++++- db/migrate/001_create_units.rb | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 7d42dad..ce43d42 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -42,11 +42,17 @@ class IngredientsController < ApplicationController CSV.foreach(params[:file].path, headers: true).with_index(2) do |row, line| r = row.to_h + unless r.has_key?('Name') + warnings << "Line 1: required 'Name' column is missing" if line == 2 + end i = { name: r.delete('Name'), + ref_amount: 100.0, + ref_unit: units['g'], group: r.delete('Group') || :other, nutrients_attributes: [] } + r.each do |col, val| if col.blank? warnings << "Line 1: column header missing" if line == 2 @@ -64,8 +70,8 @@ class IngredientsController < ApplicationController " in column #{col}" unless column_units[quantity] end end - next if quantities[quantity].blank? || val.blank? + next if val.blank? amount, amount_unit_sn, * = val.rstrip.partition(/\[.*\]$/) unit = nil if amount_unit_sn.present? @@ -80,6 +86,7 @@ class IngredientsController < ApplicationController end end + next if quantities[quantity].blank? if quantity == 'Reference' i.update({ ref_amount: amount.to_d, diff --git a/db/migrate/001_create_units.rb b/db/migrate/001_create_units.rb index 34d22e5..1d7da34 100644 --- a/db/migrate/001_create_units.rb +++ b/db/migrate/001_create_units.rb @@ -36,12 +36,18 @@ class CreateUnits < ActiveRecord::Migration reversible do |dir| dir.up do - Unit.create project: nil, shortname: "%", name: "percent" Unit.create project: nil, shortname: "g", name: "gram" + Unit.create project: nil, shortname: "mg", name: "milligram" + Unit.create project: nil, shortname: "ug", name: "microgram" Unit.create project: nil, shortname: "kg", name: "kilogram" + Unit.create project: nil, shortname: "kcal", name: "kilocalorie" + Unit.create project: nil, shortname: "%", name: "percent" # https://www.fsai.ie/uploadedFiles/Consol_Reg1169_2011.pdf # https://www.fsai.ie/legislation/food_legislation/food_information_fic/nutrition_labelling.html + e = Quantity.create project: nil, domain: :diet, parent: nil, name: "Energy", + description: "Total energy" + Quantity.create project: nil, domain: :diet, parent: nil, name: "Proteins", description: "Total amount of proteins"