Implement ordering of character sheet features

This commit is contained in:
Trevor Vallender 2024-06-17 16:36:05 +01:00
parent 184da4a392
commit 985e3584aa
9 changed files with 45 additions and 26 deletions

View File

@ -4,25 +4,45 @@ class CharacterSheetFeature < ApplicationRecord
belongs_to :character_sheet_section
belongs_to :featurable, polymorphic: true
validates :order, presence: true,
numericality: { only_integer: true, greater_than: 0 }
validates :order_index, presence: true,
numericality: { only_integer: true, greater_than: 0 },
uniqueness: { scope: :character_sheet_section_id }
validates :slug, presence: true,
length: { maximum: 100 },
uniqueness: { scope: :character_sheet_section_id }
before_validation :set_slug
before_validation :set_order
before_validation :set_order_index
def move(to:)
return if to == order_index
unless character_sheet_section.character_sheet_features.exists?(order_index: to)
update!(order_index: to)
return
end
if to < order_index
character_sheet_section.character_sheet_features.where(order_index: to...order_index)
.update_all("order_index = order_index + 1")
else
character_sheet_section.character_sheet_features.where(order_index: (order_index+1)..to)
.update_all("order_index = order_index - 1")
end
update!(order_index: to)
end
private
def set_order
return if order.present?
def set_order_index
return if order_index.present?
if character_sheet_section.character_sheet_features.any?
self.order = character_sheet_section.character_sheet_features.order(:order).last.order + 1
self.order_index = character_sheet_section.character_sheet_features.order_index(:order_index).last.order_index + 1
else
self.order = 1
self.order_index = 1
end
end

View File

@ -0,0 +1 @@
<%= render character_sheet_feature.featurable %>

View File

@ -4,17 +4,8 @@
<h4><%= character_sheet_section.name %></h4>
<div class="content">
<div id="<%= dom_id(character_sheet_section) %>_stats" class="stats">
<%= render character_sheet_section.stats %>
</div>
<div id="<%= dom_id(character_sheet_section) %>_counters" class="counters">
<%= render character_sheet_section.counters %>
</div>
<div id="<%= dom_id(character_sheet_section) %>_text_fields" class="text-fields">
<%= render character_sheet_section.text_fields %>
</div>
<div id="<%= dom_id(character_sheet_section) %>_sections">
<%= render character_sheet_section.character_sheet_subsections %>
<div id="<%= dom_id(character_sheet_section) %>_features" class="stats">
<%= render character_sheet_section.character_sheet_features.order(:order_index) %>
</div>
<% if @editable %>
<div id="<%= dom_id(character_sheet_section) %>_add_section">

View File

@ -1,4 +1,4 @@
<% content_target = "#{dom_id(@section)}_counters" %>
<% content_target = "#{dom_id(@section)}_features" %>
<%= turbo_stream.append(content_target) do %>
<%= render @counter %>
<% end %>

View File

@ -1,4 +1,4 @@
<%= content_target = "#{dom_id(@section)}_stats" %>
<%= content_target = "#{dom_id(@section)}_features" %>
<%= turbo_stream.append(content_target) do %>
<%= render @stat %>
<% end %>

View File

@ -1,4 +1,4 @@
<% content_target = "#{dom_id(@section)}_text_fields" %>
<% content_target = "#{dom_id(@section)}_features" %>
<%= turbo_stream.append(content_target) do %>
<%= render @text_field %>
<% end %>

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class RenameOrderColumn < ActiveRecord::Migration[7.1]
def change
rename_column :character_sheet_features, :order, :order_index
end
end

4
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_06_17_112523) do
ActiveRecord::Schema[7.1].define(version: 2024_06_17_151532) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -56,7 +56,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_17_112523) do
t.bigint "character_sheet_section_id", null: false
t.string "featurable_type", null: false
t.bigint "featurable_id", null: false
t.integer "order", null: false
t.integer "order_index", null: false
t.string "slug", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false

View File

@ -5,16 +5,16 @@ stats-strength:
<<: *DEFAULTS
character_sheet_section: stats
featurable: strength (Stat)
order: 1
order_index: 1
counter:
<<: *DEFAULTS
character_sheet_section: counters
featurable: hp (Counter)
order: 2
order_index: 2
text_field:
<<: *DEFAULTS
character_sheet_section: info
featurable: background (TextField)
order: 3
order_index: 3