From 66cd7926e85b7fc486e188ba663b9227ed71f1f8 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 19 Apr 2020 21:50:59 +0200 Subject: [PATCH] Added adjusting ingredient amount --- app/controllers/concerns/finders.rb | 7 +++++++ app/controllers/ingredients_controller.rb | 4 +++- app/controllers/meals_controller.rb | 5 +---- app/views/ingredients/adjust.js.erb | 2 ++ app/views/meals/_show.html.erb | 4 ++-- 5 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 app/views/ingredients/adjust.js.erb diff --git a/app/controllers/concerns/finders.rb b/app/controllers/concerns/finders.rb index 6ad7216..451d473 100644 --- a/app/controllers/concerns/finders.rb +++ b/app/controllers/concerns/finders.rb @@ -8,6 +8,13 @@ module Concerns::Finders render_404 end + def find_ingredient + @ingredient = Ingredient.find(params[:id]) + @project = @ingredient.composition.project + rescue ActiveRecord::RecordNotFound + render_404 + end + def find_food @food = Food.find(params[:id]) @project = @food.project diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index d9a5029..ea48373 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -5,6 +5,8 @@ class IngredientsController < ApplicationController before_action :authorize def adjust - params.require(:ingredient).permit(:adjustment) + amount = params[:adjustment].to_i + @ingredient.amount += amount if @ingredient.amount > -amount + @ingredient.save end end diff --git a/app/controllers/meals_controller.rb b/app/controllers/meals_controller.rb index c8948f9..2af26b1 100644 --- a/app/controllers/meals_controller.rb +++ b/app/controllers/meals_controller.rb @@ -40,14 +40,11 @@ class MealsController < ApplicationController end def update_notes - if @meal.update(params.require(:meal).permit(:notes)) - flash[:notice] = 'Updated meal notes' unless @meal.previous_changes.empty? - end + @meal.update(params.require(:meal).permit(:notes)) end def toggle_eaten @meal.toggle_eaten! - flash[:notice] = 'Updated meal status' prepare_meals end diff --git a/app/views/ingredients/adjust.js.erb b/app/views/ingredients/adjust.js.erb new file mode 100644 index 0000000..2e1452f --- /dev/null +++ b/app/views/ingredients/adjust.js.erb @@ -0,0 +1,2 @@ +$('tr[id=ingredient-<%= @ingredient.id %>] td.amount span') + .html('<%= j "#{@ingredient.amount}" %>'); diff --git a/app/views/meals/_show.html.erb b/app/views/meals/_show.html.erb index b13801a..a55c88e 100644 --- a/app/views/meals/_show.html.erb +++ b/app/views/meals/_show.html.erb @@ -21,8 +21,8 @@ <%= i.food.name %> - - <%= adjust_ingredient_links(i) { raw " #{i.amount} " } %> + + <%= adjust_ingredient_links(i) { raw " #{i.amount} " } %> <% end %>