From 6c14632bd5803fc3f11990fb769a8818705f0906 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Mon, 17 Jun 2024 17:12:19 +0100 Subject: [PATCH] Reorder features from frontend --- .../character_sheet_features_controller.rb | 28 +++++++++++++++++++ app/models/user.rb | 1 + .../_character_sheet_section.html.erb | 2 +- .../character_sheet_section.turbo_stream.erb | 3 ++ app/views/stats/_stat.html.erb | 2 ++ config/locales/en.yml | 2 ++ config/routes.rb | 6 ++++ 7 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 app/controllers/character_sheet_features_controller.rb create mode 100644 app/views/character_sheet_sections/character_sheet_section.turbo_stream.erb diff --git a/app/controllers/character_sheet_features_controller.rb b/app/controllers/character_sheet_features_controller.rb new file mode 100644 index 0000000..cfe6434 --- /dev/null +++ b/app/controllers/character_sheet_features_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index bd8766f..4b25495 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,7 @@ class User < ApplicationRecord has_and_belongs_to_many :site_roles has_many :characters, dependent: :destroy 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 :players, dependent: :destroy has_many :counters, through: :character_sheet_sections diff --git a/app/views/character_sheet_sections/_character_sheet_section.html.erb b/app/views/character_sheet_sections/_character_sheet_section.html.erb index 292b5bb..410ef19 100644 --- a/app/views/character_sheet_sections/_character_sheet_section.html.erb +++ b/app/views/character_sheet_sections/_character_sheet_section.html.erb @@ -9,7 +9,7 @@ <% if @editable %>
- <%= render partial: "edit_links", + <%= render partial: "character_sheet_sections/edit_links", locals: { section: character_sheet_section, parent: character_sheet_section, id: nil } %>
<% end %> diff --git a/app/views/character_sheet_sections/character_sheet_section.turbo_stream.erb b/app/views/character_sheet_sections/character_sheet_section.turbo_stream.erb new file mode 100644 index 0000000..48f9439 --- /dev/null +++ b/app/views/character_sheet_sections/character_sheet_section.turbo_stream.erb @@ -0,0 +1,3 @@ +<%= turbo_stream.replace(dom_id(@character_sheet_feature.character_sheet_section)) do %> + <%= render @character_sheet_feature.character_sheet_section %> +<% end %> diff --git a/app/views/stats/_stat.html.erb b/app/views/stats/_stat.html.erb index 9fea926..64654f7 100644 --- a/app/views/stats/_stat.html.erb +++ b/app/views/stats/_stat.html.erb @@ -1,5 +1,7 @@
<% 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, data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: stat.name) }) %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index af97242..cf1bd2b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -145,6 +145,8 @@ en: success: "You have signed out." stats: stat: + down: Move down + up: Move up delete: Delete confirm_delete: Are you sure you want to delete %{name}? new: diff --git a/config/routes.rb b/config/routes.rb index 9de6188..1f3cb0e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,12 @@ Rails.application.routes.draw do resources :password_resets, only: [ :new, :create, :edit, :update ] 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 :counters, only: [ :new, :create ] resources :stats, only: [ :new, :create ]