diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb new file mode 100644 index 0000000..d9a5029 --- /dev/null +++ b/app/controllers/ingredients_controller.rb @@ -0,0 +1,10 @@ +class IngredientsController < ApplicationController + include Concerns::Finders + + before_action :find_ingredient, only: [:adjust] + before_action :authorize + + def adjust + params.require(:ingredient).permit(:adjustment) + end +end diff --git a/app/helpers/meals_helper.rb b/app/helpers/meals_helper.rb index 1a49f0a..3c6f976 100644 --- a/app/helpers/meals_helper.rb +++ b/app/helpers/meals_helper.rb @@ -1,5 +1,9 @@ module MealsHelper - def action_links(m) + def meal_links(m) delete_link(meal_path(m), {remote: true, data: {}}) if m.persisted? end + def adjust_ingredient_link(i, adjustment) + link_to "%+d" % adjustment, adjust_ingredient_path(i, adjustment: adjustment), + {remote: true, method: :post} + end end diff --git a/app/views/meals/_form.html.erb b/app/views/meals/_form.html.erb index ef41a92..53d8c71 100644 --- a/app/views/meals/_form.html.erb +++ b/app/views/meals/_form.html.erb @@ -13,8 +13,8 @@ <%= label_tag :food_name do %> <%= index > 0 ? '' : t(:field_ingredients) %> * <% end %> - <%= text_field_tag :food_name, nil, {class: "autocomplete autocomplete-label", - style: "width: 80%;"} %> + <%= text_field_tag :food_name, (i.food.name if i.food), + {class: "autocomplete autocomplete-label", style: "width: 80%;"} %> <%= ff.number_field :amount, {style: "width: 8%", step: :any, label: ''} %> <%= i.food.ref_unit.shortname if i.food %> <%= ff.hidden_field :_destroy %> diff --git a/app/views/meals/_index.html.erb b/app/views/meals/_index.html.erb index a30017e..446e560 100644 --- a/app/views/meals/_index.html.erb +++ b/app/views/meals/_index.html.erb @@ -3,14 +3,36 @@
<% @meals.group_by { |m| m.eaten_at.date if m.eaten_at }.each do |d, meals| %> <% meals.each_with_index do |m, index| %> -+ <%= i.food.name %> +
++ <%= adjust_ingredient_link(i, -5) %> + <%= adjust_ingredient_link(i, -1) %> + <%= i.amount %> + <%= adjust_ingredient_link(i, 1) %> + <%= adjust_ingredient_link(i, 5) %> +
+