diff --git a/app/controllers/body_trackers_controller.rb b/app/controllers/body_trackers_controller.rb index cfee2c0..ac5f37e 100644 --- a/app/controllers/body_trackers_controller.rb +++ b/app/controllers/body_trackers_controller.rb @@ -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| diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index fac62f7..10cdc24 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -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? } - skipped_desc = skipped.map { |i| "#{i.name} - #{i.errors.full_messages.join(', ')}" } - flash[:warning] = "Ingredients skipped due to errors:
" \ - " #{skipped_desc.join('
').truncate(1024)}" + if skipped.length > 0 + skipped_desc = skipped.map { |i| "#{i.name} - #{i.errors.full_messages.join(', ')}" } + flash[:warning] = "Ingredients skipped due to errors:
" \ + " #{skipped_desc.join('
').truncate(1024)}" + end else warnings.unshift("Problems encountered during import - fix and try again:") flash[:warning] = warnings.join("
").truncate(1024, omission: '...(and other)') diff --git a/app/models/unit.rb b/app/models/unit.rb index 7aeff43..7b44d77 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -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 diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index 7a3ba46..debed83 100644 --- a/lib/body_tracking/project_patch.rb +++ b/lib/body_tracking/project_patch.rb @@ -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