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| %> - - + <% next if m.new_record? %> + +

<%= "#{t '.label_meal'}" %><%= index ? " ##{index+1}" : " (new)" %> <%= ", #{m.eaten_at.time}" if m.eaten_at %>

- <%= action_links(m) %> + <%= meal_links(m) %> + + <% m.ingredients.each do |i| %> + + +

+ <%= 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) %> +

+ + + + + <% end %> <% end %> <% end %> diff --git a/app/views/meals/_show.html.erb b/app/views/meals/_show.html.erb deleted file mode 100644 index e69de29..0000000 diff --git a/config/locales/en.yml b/config/locales/en.yml index 1e1c612..d084d02 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -79,7 +79,6 @@ en: heading_new_meal: 'New meal' index: heading: 'Meals' - show: label_meal: 'Meal' measurements: contextual: diff --git a/config/routes.rb b/config/routes.rb index 71e084d..704ce03 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,9 @@ resources :projects, shallow: true do post 'defaults' end end + resources :ingredients, only: [] do + post 'adjust/:adjustment', to: 'ingredients#adjust', as: :adjust, on: :member + end resources :meals, only: [:index, :new, :create, :edit, :update, :destroy] resources :measurement_routines, only: [:show, :edit] do member do diff --git a/init.rb b/init.rb index a0e8b5b..4fdeaaf 100644 --- a/init.rb +++ b/init.rb @@ -23,6 +23,7 @@ Redmine::Plugin.register :body_tracking do }, read: true permission :manage_common, { body_trackers: [:defaults], + ingredients: [:adjust], meals: [:new, :create, :edit, :update, :destroy], measurement_routines: [:edit], measurements: [:new, :create, :edit, :update, :destroy, :retake, :toggle_column],