1
0

Defaults/import fixes and ingredients sorting

This commit is contained in:
cryptogopher 2019-09-23 00:15:40 +02:00
parent 3ebc2f58d0
commit 9cbd540c43
4 changed files with 9 additions and 6 deletions

View File

@ -15,7 +15,7 @@ class BodyTrackersController < ApplicationController
flash[:notice] = "Loaded #{new_units > 0 ? new_units : "no" } new" \
" #{'unit'.pluralize(new_units)}"
available = Quantity.where(project: @project).map { |q| [[q.name, q.domain], q] }.to_h
available = @project.quantities.map { |q| [[q.name, q.domain], q] }.to_h
new_quantities = available.length
defaults = Quantity.where(project: nil)
Quantity.each_with_level(defaults) do |q, level|

View File

@ -19,6 +19,7 @@ class IngredientsController < ApplicationController
redirect_to project_ingredients_url(@project)
else
@ingredients = @project.ingredients.includes(:ref_unit)
@ingredient.nutrients.new(ingredient: @ingredient) if @ingredient.nutrients.empty?
render :index
end
end
@ -112,9 +113,11 @@ class IngredientsController < ApplicationController
flash[:notice] = "Imported #{ingredients.map(&:persisted?).count(true)} out of" \
" #{ingredients_params.length} ingredients"
skipped = ingredients.select { |i| !i.persisted? }
if skipped.length > 0
skipped_desc = skipped.map { |i| "#{i.name} - #{i.errors.full_messages.join(', ')}" }
flash[:warning] = "Ingredients skipped due to errors:<br>" \
" #{skipped_desc.join('<br>').truncate(1024)}"
end
else
warnings.unshift("Problems encountered during import - fix and try again:")
flash[:warning] = warnings.join("<br>").truncate(1024, omission: '...(and other)')

View File

@ -1,5 +1,5 @@
class Unit < ActiveRecord::Base
belongs_to :project, required: true
belongs_to :project, required: false
validates :shortname, presence: true, uniqueness: {scope: :project_id}
end

View File

@ -1,7 +1,7 @@
module BodyTracking
module ProjectPatch
Project.class_eval do
has_many :ingredients, dependent: :destroy
has_many :ingredients, -> { order "name" }, dependent: :destroy
has_many :quantities, -> { order "lft" }, dependent: :destroy
has_many :units, dependent: :destroy