forked from fixin.me/fixin.me
Unit rebase as a separate action
This commit is contained in:
parent
05b0c66216
commit
1966c782bc
@ -2,7 +2,7 @@ class UnitsController < ApplicationController
|
|||||||
before_action only: [:new] do
|
before_action only: [:new] do
|
||||||
find_unit if params[:id].present?
|
find_unit if params[:id].present?
|
||||||
end
|
end
|
||||||
before_action :find_unit, only: [:edit, :update, :destroy]
|
before_action :find_unit, only: [:edit, :update, :rebase, :destroy]
|
||||||
|
|
||||||
before_action except: :index do
|
before_action except: :index do
|
||||||
raise AccessForbidden unless current_user.at_least(:active)
|
raise AccessForbidden unless current_user.at_least(:active)
|
||||||
@ -30,7 +30,7 @@ class UnitsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
if @unit.update(unit_params)
|
if @unit.update(unit_params.except(:base_id))
|
||||||
flash.now[:notice] = t(".success")
|
flash.now[:notice] = t(".success")
|
||||||
run_and_render :index
|
run_and_render :index
|
||||||
else
|
else
|
||||||
@ -38,6 +38,16 @@ class UnitsController < ApplicationController
|
|||||||
end
|
end
|
||||||
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
|
def destroy
|
||||||
if @unit.destroy
|
if @unit.destroy
|
||||||
flash.now[:notice] = t(".success")
|
flash.now[:notice] = t(".success")
|
||||||
|
@ -56,7 +56,7 @@ module ApplicationHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def tabular_fields_for(record_name, record_object = nil, options = {}, &block)
|
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)
|
fields_for(record_name, record_object, **options, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -94,6 +94,10 @@ module ApplicationHelper
|
|||||||
image_element_to(:link, name, image, options, html_options)
|
image_element_to(:link, name, image, options, html_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_errors(record)
|
||||||
|
flash.now[:alert] = record.errors.full_messages unless record.errors.empty?
|
||||||
|
end
|
||||||
|
|
||||||
def render_flash_messages
|
def render_flash_messages
|
||||||
flash.map do |entry, messages|
|
flash.map do |entry, messages|
|
||||||
Array(messages).map do |message|
|
Array(messages).map do |message|
|
||||||
|
@ -48,7 +48,8 @@
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
var params = new URLSearchParams();
|
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"), {
|
fetch(event.dataTransfer.getData("text/plain"), {
|
||||||
body: params,
|
body: params,
|
||||||
@ -57,7 +58,7 @@
|
|||||||
"X-CSRF-Token": document.head.querySelector("meta[name=csrf-token]").content,
|
"X-CSRF-Token": document.head.querySelector("meta[name=csrf-token]").content,
|
||||||
"X-Requested-With": "XMLHttpRequest"
|
"X-Requested-With": "XMLHttpRequest"
|
||||||
},
|
},
|
||||||
method: "PATCH"
|
method: "POST"
|
||||||
})
|
})
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(html => Turbo.renderStreamMessage(html))
|
.then(html => Turbo.renderStreamMessage(html))
|
||||||
|
1
app/views/units/rebase.turbo_stream.erb
Normal file
1
app/views/units/rebase.turbo_stream.erb
Normal file
@ -0,0 +1 @@
|
|||||||
|
<% render_errors @unit %>
|
@ -54,6 +54,8 @@ en:
|
|||||||
success: Created new unit
|
success: Created new unit
|
||||||
update:
|
update:
|
||||||
success: Updated unit
|
success: Updated unit
|
||||||
|
rebase:
|
||||||
|
multiplier_reset: Multiplier of "%{symbol}" has been reset to 1, due to repositioning
|
||||||
destroy:
|
destroy:
|
||||||
success: Deleted unit
|
success: Deleted unit
|
||||||
users:
|
users:
|
||||||
|
@ -2,7 +2,11 @@ Rails.application.routes.draw do
|
|||||||
devise_for :users, path: '', path_names: {registration: 'profile'},
|
devise_for :users, path: '', path_names: {registration: 'profile'},
|
||||||
controllers: {registrations: :registrations}
|
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
|
resources :users, only: [:index, :show, :update] do
|
||||||
member do
|
member do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user