Defaults/import fixes and ingredients sorting
This commit is contained in:
parent
3ebc2f58d0
commit
9cbd540c43
@ -15,7 +15,7 @@ class BodyTrackersController < ApplicationController
|
|||||||
flash[:notice] = "Loaded #{new_units > 0 ? new_units : "no" } new" \
|
flash[:notice] = "Loaded #{new_units > 0 ? new_units : "no" } new" \
|
||||||
" #{'unit'.pluralize(new_units)}"
|
" #{'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
|
new_quantities = available.length
|
||||||
defaults = Quantity.where(project: nil)
|
defaults = Quantity.where(project: nil)
|
||||||
Quantity.each_with_level(defaults) do |q, level|
|
Quantity.each_with_level(defaults) do |q, level|
|
||||||
|
@ -19,6 +19,7 @@ class IngredientsController < ApplicationController
|
|||||||
redirect_to project_ingredients_url(@project)
|
redirect_to project_ingredients_url(@project)
|
||||||
else
|
else
|
||||||
@ingredients = @project.ingredients.includes(:ref_unit)
|
@ingredients = @project.ingredients.includes(:ref_unit)
|
||||||
|
@ingredient.nutrients.new(ingredient: @ingredient) if @ingredient.nutrients.empty?
|
||||||
render :index
|
render :index
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -112,9 +113,11 @@ class IngredientsController < ApplicationController
|
|||||||
flash[:notice] = "Imported #{ingredients.map(&:persisted?).count(true)} out of" \
|
flash[:notice] = "Imported #{ingredients.map(&:persisted?).count(true)} out of" \
|
||||||
" #{ingredients_params.length} ingredients"
|
" #{ingredients_params.length} ingredients"
|
||||||
skipped = ingredients.select { |i| !i.persisted? }
|
skipped = ingredients.select { |i| !i.persisted? }
|
||||||
skipped_desc = skipped.map { |i| "#{i.name} - #{i.errors.full_messages.join(', ')}" }
|
if skipped.length > 0
|
||||||
flash[:warning] = "Ingredients skipped due to errors:<br>" \
|
skipped_desc = skipped.map { |i| "#{i.name} - #{i.errors.full_messages.join(', ')}" }
|
||||||
" #{skipped_desc.join('<br>').truncate(1024)}"
|
flash[:warning] = "Ingredients skipped due to errors:<br>" \
|
||||||
|
" #{skipped_desc.join('<br>').truncate(1024)}"
|
||||||
|
end
|
||||||
else
|
else
|
||||||
warnings.unshift("Problems encountered during import - fix and try again:")
|
warnings.unshift("Problems encountered during import - fix and try again:")
|
||||||
flash[:warning] = warnings.join("<br>").truncate(1024, omission: '...(and other)')
|
flash[:warning] = warnings.join("<br>").truncate(1024, omission: '...(and other)')
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
class Unit < ActiveRecord::Base
|
class Unit < ActiveRecord::Base
|
||||||
belongs_to :project, required: true
|
belongs_to :project, required: false
|
||||||
|
|
||||||
validates :shortname, presence: true, uniqueness: {scope: :project_id}
|
validates :shortname, presence: true, uniqueness: {scope: :project_id}
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
module BodyTracking
|
module BodyTracking
|
||||||
module ProjectPatch
|
module ProjectPatch
|
||||||
Project.class_eval do
|
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 :quantities, -> { order "lft" }, dependent: :destroy
|
||||||
has_many :units, dependent: :destroy
|
has_many :units, dependent: :destroy
|
||||||
|
Reference in New Issue
Block a user