diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index ea48373..253b82f 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -1,12 +1,5 @@ class IngredientsController < ApplicationController include Concerns::Finders - before_action :find_ingredient, only: [:adjust] before_action :authorize - - def adjust - 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 0ff68b7..7dcc9d4 100644 --- a/app/controllers/meals_controller.rb +++ b/app/controllers/meals_controller.rb @@ -9,6 +9,7 @@ class MealsController < ApplicationController before_action :find_quantity_by_quantity_id, only: [:toggle_exposure] before_action :find_meal, only: [:edit, :update, :destroy, :edit_notes, :update_notes, :toggle_eaten] + before_action :find_ingredient, only: [:adjust] before_action :authorize def index @@ -65,6 +66,17 @@ class MealsController < ApplicationController prepare_meals end + def adjust + amount = params[:adjustment].to_i + @ingredient.amount += amount if @ingredient.amount > -amount + @ingredient.save + + prepare_meals + @meal = @ingredient.composition + @date = @meal.display_date + @meal_index = @meals_by_date[@date].index(@meal) + end + private def meal_params diff --git a/app/views/ingredients/adjust.js.erb b/app/views/ingredients/adjust.js.erb deleted file mode 100644 index 2e1452f..0000000 --- a/app/views/ingredients/adjust.js.erb +++ /dev/null @@ -1,2 +0,0 @@ -$('tr[id=ingredient-<%= @ingredient.id %>] td.amount span') - .html('<%= j "#{@ingredient.amount}" %>'); diff --git a/app/views/meals/adjust.js.erb b/app/views/meals/adjust.js.erb new file mode 100644 index 0000000..93b359b --- /dev/null +++ b/app/views/meals/adjust.js.erb @@ -0,0 +1,9 @@ +$('tr[id=ingredient-<%= @ingredient.id %>]').replaceWith( + '<%= j render partial: 'meals/show_ingredient', locals: {i: @ingredient} %>' +); +$('tr[id=meal-<%= @meal.id %>]').replaceWith( + '<%= j render partial: 'meals/show', locals: {m: @meal, index: @meal_index} %>' +); +$('tr[id=date-<%= @date.strftime('%Y%m%d') %>]').replaceWith( + '<%= j render partial: 'meals/show_date', locals: {date: @date} %>' +); diff --git a/config/routes.rb b/config/routes.rb index b329c25..6484538 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,7 +8,7 @@ resources :projects, shallow: true do end end resources :ingredients, only: [] do - post 'adjust/:adjustment', to: 'ingredients#adjust', as: :adjust, on: :member + post 'adjust/:adjustment', to: 'meals#adjust', as: :adjust, on: :member end resources :meals, only: [:index, :new, :create, :edit, :update, :destroy] do member do diff --git a/init.rb b/init.rb index 9837c06..0fea894 100644 --- a/init.rb +++ b/init.rb @@ -23,9 +23,8 @@ Redmine::Plugin.register :body_tracking do }, read: true permission :manage_common, { body_trackers: [:defaults], - ingredients: [:adjust], meals: [:new, :create, :edit, :update, :destroy, :edit_notes, :update_notes, - :toggle_eaten, :toggle_exposure], + :toggle_eaten, :toggle_exposure, :adjust], measurement_routines: [:edit], measurements: [:new, :create, :edit, :update, :destroy, :retake, :toggle_exposure], foods: [:new, :create, :edit, :update, :destroy, :toggle, :toggle_exposure,