Toggling quantity primariness from nutrients view
This commit is contained in:
@@ -3,6 +3,7 @@ class IngredientsController < ApplicationController
|
||||
|
||||
before_action :find_project_by_project_id, only: [:index, :create, :import, :nutrients]
|
||||
before_action :find_ingredient, only: [:destroy, :toggle]
|
||||
before_action :find_quantity, only: [:toggle_nutrient_column]
|
||||
before_action :authorize
|
||||
|
||||
def index
|
||||
@@ -17,22 +18,12 @@ class IngredientsController < ApplicationController
|
||||
def nutrients
|
||||
@ingredient = @project.ingredients.new
|
||||
@ingredient.nutrients.new(ingredient: @ingredient)
|
||||
prepare_nutrients
|
||||
end
|
||||
|
||||
ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit])
|
||||
@primary_quantities = @project.quantities.where(primary: true)
|
||||
@primary_nutrients = {}
|
||||
@extra_nutrients = {}
|
||||
ingredients.each do |i|
|
||||
@primary_nutrients[i] = {}
|
||||
@extra_nutrients[i] = {}
|
||||
i.nutrients.sort_by { |n| n.quantity.lft }.each do |n|
|
||||
if @primary_quantities.include?(n.quantity)
|
||||
@primary_nutrients[i][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]"
|
||||
else
|
||||
@extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]"
|
||||
end
|
||||
end
|
||||
end
|
||||
def toggle_nutrient_column
|
||||
@quantity.toggle_primary!
|
||||
prepare_nutrients
|
||||
end
|
||||
|
||||
def create
|
||||
@@ -189,4 +180,22 @@ class IngredientsController < ApplicationController
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def prepare_nutrients
|
||||
ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit])
|
||||
@primary_quantities = @project.quantities.where(primary: true)
|
||||
@primary_nutrients = {}
|
||||
@extra_nutrients = {}
|
||||
ingredients.each do |i|
|
||||
@primary_nutrients[i] = {}
|
||||
@extra_nutrients[i] = {}
|
||||
i.nutrients.sort_by { |n| n.quantity.lft }.each do |n|
|
||||
if @primary_quantities.include?(n.quantity)
|
||||
@primary_nutrients[i][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]"
|
||||
else
|
||||
@extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -28,7 +28,7 @@ class QuantitiesController < ApplicationController
|
||||
end
|
||||
|
||||
def toggle
|
||||
@quantity.update(primary: !@quantity.primary)
|
||||
@quantity.toggle_primary!
|
||||
@quantities = @project.quantities
|
||||
end
|
||||
|
||||
@@ -67,13 +67,4 @@ class QuantitiesController < ApplicationController
|
||||
:primary
|
||||
)
|
||||
end
|
||||
|
||||
# :find_* methods are called before :authorize,
|
||||
# @project is required for :authorize to succeed
|
||||
def find_quantity
|
||||
@quantity = Quantity.find(params[:id])
|
||||
@project = @quantity.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user