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 :character_sheet_section
belongs_to :featurable, polymorphic: true belongs_to :featurable, polymorphic: true
validates :order, presence: true, validates :order_index, presence: true,
numericality: { only_integer: true, greater_than: 0 } numericality: { only_integer: true, greater_than: 0 },
uniqueness: { scope: :character_sheet_section_id }
validates :slug, presence: true, validates :slug, presence: true,
length: { maximum: 100 }, length: { maximum: 100 },
uniqueness: { scope: :character_sheet_section_id } uniqueness: { scope: :character_sheet_section_id }
before_validation :set_slug 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 private
def set_order def set_order_index
return if order.present? return if order_index.present?
if character_sheet_section.character_sheet_features.any? 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 else
self.order = 1 self.order_index = 1
end end
end end

View File

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

View File

@ -4,17 +4,8 @@
<h4><%= character_sheet_section.name %></h4> <h4><%= character_sheet_section.name %></h4>
<div class="content"> <div class="content">
<div id="<%= dom_id(character_sheet_section) %>_stats" class="stats"> <div id="<%= dom_id(character_sheet_section) %>_features" class="stats">
<%= render character_sheet_section.stats %> <%= render character_sheet_section.character_sheet_features.order(:order_index) %>
</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> </div>
<% if @editable %> <% if @editable %>
<div id="<%= dom_id(character_sheet_section) %>_add_section"> <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 %> <%= turbo_stream.append(content_target) do %>
<%= render @counter %> <%= render @counter %>
<% end %> <% 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 %> <%= turbo_stream.append(content_target) do %>
<%= render @stat %> <%= render @stat %>
<% end %> <% 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 %> <%= turbo_stream.append(content_target) do %>
<%= render @text_field %> <%= render @text_field %>
<% end %> <% 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. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" 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.bigint "character_sheet_section_id", null: false
t.string "featurable_type", null: false t.string "featurable_type", null: false
t.bigint "featurable_id", 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.string "slug", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false

View File

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