diff --git a/app/models/character_sheet_feature.rb b/app/models/character_sheet_feature.rb
index d4de852..f30e197 100644
--- a/app/models/character_sheet_feature.rb
+++ b/app/models/character_sheet_feature.rb
@@ -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
diff --git a/app/views/character_sheet_features/_character_sheet_feature.html.erb b/app/views/character_sheet_features/_character_sheet_feature.html.erb
new file mode 100644
index 0000000..3aded4c
--- /dev/null
+++ b/app/views/character_sheet_features/_character_sheet_feature.html.erb
@@ -0,0 +1 @@
+<%= render character_sheet_feature.featurable %>
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 7164b4a..292b5bb 100644
--- a/app/views/character_sheet_sections/_character_sheet_section.html.erb
+++ b/app/views/character_sheet_sections/_character_sheet_section.html.erb
@@ -4,17 +4,8 @@
-
- <%= render character_sheet_section.stats %>
-
-
- <%= render character_sheet_section.counters %>
-
-
- <%= render character_sheet_section.text_fields %>
-
-
- <%= render character_sheet_section.character_sheet_subsections %>
+
+ <%= render character_sheet_section.character_sheet_features.order(:order_index) %>
<% if @editable %>
diff --git a/app/views/counters/create.turbo_stream.erb b/app/views/counters/create.turbo_stream.erb
index aa41d2e..b5ff8b6 100644
--- a/app/views/counters/create.turbo_stream.erb
+++ b/app/views/counters/create.turbo_stream.erb
@@ -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 %>
diff --git a/app/views/stats/create.turbo_stream.erb b/app/views/stats/create.turbo_stream.erb
index 09ba523..deb080c 100644
--- a/app/views/stats/create.turbo_stream.erb
+++ b/app/views/stats/create.turbo_stream.erb
@@ -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 %>
diff --git a/app/views/text_fields/create.turbo_stream.erb b/app/views/text_fields/create.turbo_stream.erb
index 280f6e3..c448203 100644
--- a/app/views/text_fields/create.turbo_stream.erb
+++ b/app/views/text_fields/create.turbo_stream.erb
@@ -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 %>
diff --git a/db/migrate/20240617151532_rename_order_column.rb b/db/migrate/20240617151532_rename_order_column.rb
new file mode 100644
index 0000000..342f263
--- /dev/null
+++ b/db/migrate/20240617151532_rename_order_column.rb
@@ -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
diff --git a/db/schema.rb b/db/schema.rb
index c44ae48..6e6e466 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -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
diff --git a/test/fixtures/character_sheet_features.yml b/test/fixtures/character_sheet_features.yml
index 8f48837..c0988a9 100644
--- a/test/fixtures/character_sheet_features.yml
+++ b/test/fixtures/character_sheet_features.yml
@@ -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