Reorder features from frontend

This commit is contained in:
Trevor Vallender 2024-06-17 17:12:19 +01:00
parent 985e3584aa
commit 6c14632bd5
7 changed files with 43 additions and 1 deletions

View File

@ -0,0 +1,28 @@
# frozen_string_literal: true
class CharacterSheetFeaturesController < ApplicationController
before_action :set_character_sheet_feature
before_action :set_character
def reorder_down
@character_sheet_feature.move(to: @character_sheet_feature.order_index - 1)
@editable = true
render "character_sheet_sections/character_sheet_section"
end
def reorder_up
@character_sheet_feature.move(to: @character_sheet_feature.order_index + 1)
@editable = true
render "character_sheet_sections/character_sheet_section"
end
private
def set_character
@character = @character_sheet_feature.featurable.character
end
def set_character_sheet_feature
@character_sheet_feature = Current.user.character_sheet_features.find_by(featurable_id: params[:id])
end
end

View File

@ -7,6 +7,7 @@ class User < ApplicationRecord
has_and_belongs_to_many :site_roles has_and_belongs_to_many :site_roles
has_many :characters, dependent: :destroy has_many :characters, dependent: :destroy
has_many :character_sheet_sections, through: :characters has_many :character_sheet_sections, through: :characters
has_many :character_sheet_features, through: :character_sheet_sections
has_many :owned_tables, foreign_key: :owner_id, class_name: "Table" has_many :owned_tables, foreign_key: :owner_id, class_name: "Table"
has_many :players, dependent: :destroy has_many :players, dependent: :destroy
has_many :counters, through: :character_sheet_sections has_many :counters, through: :character_sheet_sections

View File

@ -9,7 +9,7 @@
</div> </div>
<% if @editable %> <% if @editable %>
<div id="<%= dom_id(character_sheet_section) %>_add_section"> <div id="<%= dom_id(character_sheet_section) %>_add_section">
<%= render partial: "edit_links", <%= render partial: "character_sheet_sections/edit_links",
locals: { section: character_sheet_section, parent: character_sheet_section, id: nil } %> locals: { section: character_sheet_section, parent: character_sheet_section, id: nil } %>
</div> </div>
<% end %> <% end %>

View File

@ -0,0 +1,3 @@
<%= turbo_stream.replace(dom_id(@character_sheet_feature.character_sheet_section)) do %>
<%= render @character_sheet_feature.character_sheet_section %>
<% end %>

View File

@ -1,5 +1,7 @@
<div class="stat" id="<%= dom_id(stat) %>"> <div class="stat" id="<%= dom_id(stat) %>">
<% if @editable %> <% if @editable %>
<%= link_to t(".down"), reorder_down_character_sheet_feature_path(stat), data: { turbo_method: :patch } %>
<%= link_to t(".up"), reorder_up_character_sheet_feature_path(stat), data: { turbo_method: :patch } %>
<%= link_to(t(".delete"), stat, <%= link_to(t(".delete"), stat,
data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: stat.name) }) %> data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: stat.name) }) %>
<% end %> <% end %>

View File

@ -145,6 +145,8 @@ en:
success: "You have signed out." success: "You have signed out."
stats: stats:
stat: stat:
down: Move down
up: Move up
delete: Delete delete: Delete
confirm_delete: Are you sure you want to delete %{name}? confirm_delete: Are you sure you want to delete %{name}?
new: new:

View File

@ -15,6 +15,12 @@ Rails.application.routes.draw do
resources :password_resets, only: [ :new, :create, :edit, :update ] resources :password_resets, only: [ :new, :create, :edit, :update ]
resources :sessions, only: [ :new, :create, :destroy ] resources :sessions, only: [ :new, :create, :destroy ]
resources :character_sheet_features do
member do
patch :reorder_down
patch :reorder_up
end
end
resources :character_sheet_sections, only: [ :destroy ] do resources :character_sheet_sections, only: [ :destroy ] do
resources :counters, only: [ :new, :create ] resources :counters, only: [ :new, :create ]
resources :stats, only: [ :new, :create ] resources :stats, only: [ :new, :create ]