diff --git a/app/controllers/units_controller.rb b/app/controllers/units_controller.rb index b6c60a0..800bfe8 100644 --- a/app/controllers/units_controller.rb +++ b/app/controllers/units_controller.rb @@ -2,7 +2,7 @@ class UnitsController < ApplicationController before_action only: [:new] do find_unit if params[:id].present? end - before_action :find_unit, only: [:edit, :update, :destroy] + before_action :find_unit, only: [:edit, :update, :rebase, :destroy] before_action except: :index do raise AccessForbidden unless current_user.at_least(:active) @@ -30,7 +30,7 @@ class UnitsController < ApplicationController end def update - if @unit.update(unit_params) + if @unit.update(unit_params.except(:base_id)) flash.now[:notice] = t(".success") run_and_render :index else @@ -38,6 +38,16 @@ class UnitsController < ApplicationController end end + def rebase + permitted = params.require(:unit).permit(:base_id) + if permitted[:base_id].blank? && @unit.multiplier != 1 + permitted.merge!(multiplier: 1) + flash.now[:notice] = t(".multiplier_reset", symbol: @unit.symbol) + end + + run_and_render :index if @unit.update(permitted) + end + def destroy if @unit.destroy flash.now[:notice] = t(".success") diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 18e133c..b031f08 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -56,7 +56,7 @@ module ApplicationHelper end def tabular_fields_for(record_name, record_object = nil, options = {}, &block) - flash.now[:alert] = record_name.errors.full_messages unless record_name.errors.empty? + render_errors(record_name) fields_for(record_name, record_object, **options, &block) end @@ -94,6 +94,10 @@ module ApplicationHelper image_element_to(:link, name, image, options, html_options) end + def render_errors(record) + flash.now[:alert] = record.errors.full_messages unless record.errors.empty? + end + def render_flash_messages flash.map do |entry, messages| Array(messages).map do |message| diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb index 42ea279..774d42c 100644 --- a/app/views/units/index.html.erb +++ b/app/views/units/index.html.erb @@ -48,7 +48,8 @@ event.preventDefault(); var params = new URLSearchParams(); - params.append("unit[base_id]", event.target.closest("tr").getAttribute("data-drop-id")); + var base_id = event.currentTarget.getAttribute("data-drop-id").split("_").pop(); + params.append("unit[base_id]", base_id); fetch(event.dataTransfer.getData("text/plain"), { body: params, @@ -57,7 +58,7 @@ "X-CSRF-Token": document.head.querySelector("meta[name=csrf-token]").content, "X-Requested-With": "XMLHttpRequest" }, - method: "PATCH" + method: "POST" }) .then(response => response.text()) .then(html => Turbo.renderStreamMessage(html)) diff --git a/app/views/units/rebase.turbo_stream.erb b/app/views/units/rebase.turbo_stream.erb new file mode 100644 index 0000000..cf4f95a --- /dev/null +++ b/app/views/units/rebase.turbo_stream.erb @@ -0,0 +1 @@ +<% render_errors @unit %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 8710f5b..969ee3f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -54,6 +54,8 @@ en: success: Created new unit update: success: Updated unit + rebase: + multiplier_reset: Multiplier of "%{symbol}" has been reset to 1, due to repositioning destroy: success: Deleted unit users: diff --git a/config/routes.rb b/config/routes.rb index 0aa8f2d..c977e9b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,11 @@ Rails.application.routes.draw do devise_for :users, path: '', path_names: {registration: 'profile'}, controllers: {registrations: :registrations} - resources :units, except: [:show], path_names: {new: '(/:id)/new'} + resources :units, except: [:show], path_names: {new: '(/:id)/new'} do + member do + post :rebase + end + end resources :users, only: [:index, :show, :update] do member do