From 7cb3ac4134f0719ad45b07e7072d42cfbc94c167 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 1 Dec 2019 20:02:45 +0100 Subject: [PATCH] Preliminary Measurement edit/update --- app/controllers/measurements_controller.rb | 15 ++++++++- app/views/measurements/_index.html.erb | 38 +++++++++++++++++++--- app/views/quantities/_index.html.erb | 14 ++++---- config/routes.rb | 2 +- init.rb | 2 +- 5 files changed, 55 insertions(+), 16 deletions(-) diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb index a2903b8..27ab643 100644 --- a/app/controllers/measurements_controller.rb +++ b/app/controllers/measurements_controller.rb @@ -2,7 +2,7 @@ class MeasurementsController < ApplicationController menu_item :body_trackers before_action :find_project_by_project_id, only: [:index, :create] - before_action :find_measurement, only: [:destroy, :retake] + before_action :find_measurement, only: [:edit, :update, :destroy, :retake] before_action :authorize def index @@ -25,6 +25,19 @@ class MeasurementsController < ApplicationController end end + def edit + prepare_measurements + render :index + end + + def update + if @measurement.update(measurement_params) + flash[:notice] = 'Updated measurement' + end + prepare_measurements + render :index + end + def destroy # FIXME: don't destroy if there are any readout values if @measurement.destroy diff --git a/app/views/measurements/_index.html.erb b/app/views/measurements/_index.html.erb index 5021bee..5d41ef6 100644 --- a/app/views/measurements/_index.html.erb +++ b/app/views/measurements/_index.html.erb @@ -2,8 +2,8 @@ - + @@ -12,17 +12,45 @@ <% @measurements.each do |m| %> <% next if m.new_record? %> - + + <% next unless (m == @measurement) && + ((action_name == "edit") || + (action_name == "update" && @measurement.errors.present?)) %> + + + <% end %>
<%= l(:field_name) %> <%= l(:field_taken_at_date) %><%= l(:field_name) %> <%= l(:field_source) %> <%= l(:field_action) %>
<%= m.name %> <%= m.taken_at.strftime("%F %R") %><%= m.name %> <%= m.source.name if m.source.present? %> <%= link_to l(:button_retake), retake_measurement_path(m), { - remote: true, - class: "icon icon-reload" - } %> + remote: true, + class: "icon icon-reload" + } %> + <%= link_to l(:button_edit), edit_measurement_path(m), { + remote: true, + class: "icon icon-edit" + } %> <%= delete_link measurement_path(m), {remote: true, data: {}} %>
+
+ <%= labelled_form_for @measurement, + url: measurement_path(@measurement), + method: :patch, remote: true, + html: {id: 'measurement-edit-form', name: 'measurement-edit-form'} do |f| %> + + <%= render partial: 'measurements/form', locals: {f: f} %> + +
+

+ <%= submit_tag l(:button_save) %> + <%= link_to l(:button_cancel), "#", + onclick: '$(this).closest("tr").remove(); return false;' %> +

+
+ <% end %> +
+
diff --git a/app/views/quantities/_index.html.erb b/app/views/quantities/_index.html.erb index 99707a3..67f4f0e 100644 --- a/app/views/quantities/_index.html.erb +++ b/app/views/quantities/_index.html.erb @@ -50,18 +50,16 @@ <%= q.description %> <%= checked_image q.formula.present? %> - <%= - link_to l(:button_edit), edit_quantity_path(q), { - remote: true, - class: "icon icon-edit" - } - %> + <%= link_to l(:button_edit), edit_quantity_path(q), { + remote: true, + class: "icon icon-edit" + } %> <%= delete_link quantity_path(q), {remote: true, data: {}} %> <% next unless (q == @quantity) && - ((action_name == "edit") || (action_name == "update" && @quantity.errors.present?)) - %> + ((action_name == "edit") || + (action_name == "update" && @quantity.errors.present?)) %>
diff --git a/config/routes.rb b/config/routes.rb index a9f7db9..021c487 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,7 @@ resources :projects, shallow: true do resources :body_trackers, only: [:index] do collection do post 'defaults' - resources :measurements, only: [:index, :create, :destroy] do + resources :measurements, only: [:index, :create, :edit, :update, :destroy] do get 'retake', on: :member end resources :ingredients, only: [:index, :create, :destroy] do diff --git a/init.rb b/init.rb index 1df1fef..94120c8 100644 --- a/init.rb +++ b/init.rb @@ -24,7 +24,7 @@ Redmine::Plugin.register :body_tracking do }, read: true permission :manage_common, { body_trackers: [:defaults], - measurements: [:create, :destroy, :retake], + measurements: [:create, :edit, :update, :destroy, :retake], ingredients: [:create, :destroy, :toggle, :import, :toggle_nutrient_column], sources: [:create, :destroy], quantities: [:create, :edit, :update, :destroy, :toggle, :move],