From a1c2364554e1e16e0f1436478dc477dafa2c3cf4 Mon Sep 17 00:00:00 2001 From: Sean Dickinson Date: Sat, 31 Oct 2026 16:36:59 -0400 Subject: [PATCH 1/2] feat: let ordered records move up and down Positions are now required, and ordered records can swap places with a neighbor. Co-Authored-By: Claude Opus 5.5 --- app/models/concerns/ordered.rb | 31 +++++++++- ...uire_positions_for_contents_and_modules.rb | 25 +++++++++ db/schema.rb | 7 ++- test/models/content_module_test.rb | 11 ++++ test/models/content_test.rb | 56 +++++++++++++++++++ 5 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20261031202333_require_positions_for_contents_and_modules.rb diff --git a/app/models/concerns/ordered.rb b/app/models/concerns/ordered.rb index c846074..fc4d227 100644 --- a/app/models/concerns/ordered.rb +++ b/app/models/concerns/ordered.rb @@ -1,13 +1,32 @@ # Orders records by an integer +position+ column. New records go to the end of their siblings. +# and existing ones can swap places with a neighbor. module Ordered extend ActiveSupport::Concern + DIRECTIONS = %w[up down].freeze + included do scope :ordered, -> { order(:position) } before_create :append_to_siblings end + def move(direction) + case direction + when "up" then move_up + when "down" then move_down + else raise ArgumentError, "Unknown direction: #{direction.inspect}" + end + end + + def move_up + swap_position_with position_siblings.where(position: ...position).reorder(position: :desc).first + end + + def move_down + swap_position_with position_siblings.where(position: (position + 1)..).reorder(:position).first + end + private # This method is required to override. It is the relation of records this record is @@ -15,7 +34,17 @@ module Ordered # # @return [ActiveRecord::Relation] def position_siblings - raise NotImplementedError, "#{self.class} must define #position_siblings to be ordered" + raise NotImplementedError, "#{self.class} must define #position_siblings to be moved" + end + + def swap_position_with(neighbor) + return unless neighbor + + transaction do + own_position = position + update!(position: neighbor.position) + neighbor.update!(position: own_position) + end end def append_to_siblings diff --git a/db/migrate/20261031202333_require_positions_for_contents_and_modules.rb b/db/migrate/20261031202333_require_positions_for_contents_and_modules.rb new file mode 100644 index 0000000..e68f709 --- /dev/null +++ b/db/migrate/20261031202333_require_positions_for_contents_and_modules.rb @@ -0,0 +1,25 @@ +class RequirePositionsForContentsAndModules < ActiveRecord::Migration[8.1] + def change + reversible do |dir| + dir.up do + # Number modules from 1 within each program and level: current position first (NULLs last), then id. + execute <<~SQL + UPDATE content_modules SET position = ( + SELECT ranked.row_number FROM ( + SELECT id, ROW_NUMBER() OVER ( + PARTITION BY program_id, level + ORDER BY position IS NULL, position, id + ) AS row_number + FROM content_modules + ) ranked + WHERE ranked.id = content_modules.id + ) + SQL + end + end + + change_column_null :content_modules, :position, false + add_index :content_modules, [ :program_id, :level, :position ] + change_column_null :contents, :position, false + end +end diff --git a/db/schema.rb b/db/schema.rb index fc75edd..24414fb 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[8.1].define(version: 2026_10_31_181544) do +ActiveRecord::Schema[8.1].define(version: 2026_10_31_202333) do create_table "classroom_modules", force: :cascade do |t| t.integer "classroom_program_id", null: false t.integer "content_module_id", null: false @@ -49,9 +49,10 @@ t.datetime "created_at", null: false t.string "level", null: false t.string "name", null: false - t.integer "position" + t.integer "position", null: false t.integer "program_id", null: false t.datetime "updated_at", null: false + t.index ["program_id", "level", "position"], name: "index_content_modules_on_program_id_and_level_and_position" t.index ["program_id"], name: "index_content_modules_on_program_id" end @@ -60,7 +61,7 @@ t.integer "contentable_id", null: false t.string "contentable_type", null: false t.datetime "created_at", null: false - t.integer "position" + t.integer "position", null: false t.datetime "updated_at", null: false t.index ["content_module_id", "contentable_type", "contentable_id"], name: "index_contents_on_module_and_contentable", unique: true t.index ["content_module_id", "position"], name: "index_contents_on_content_module_id_and_position" diff --git a/test/models/content_module_test.rb b/test/models/content_module_test.rb index 04ebd7d..7d8f697 100644 --- a/test/models/content_module_test.rb +++ b/test/models/content_module_test.rb @@ -88,6 +88,17 @@ class ContentModuleTest < ActiveSupport::TestCase assert_equal 1, first.reload.position end + test "moving ignores modules in other levels and programs" do + only = create_module("Only") + create_module("Moderate", level: "moderate") + create_module("Other", program: programs(:two)) + + only.move_up + only.move_down + + assert_equal 1, only.reload.position + end + test "cannot be destroyed when classroom modules exist" do content_module = ContentModule.create!(program: @program, level: "basic", name: "Assigned Module") classroom_program = classroom_programs(:one) diff --git a/test/models/content_test.rb b/test/models/content_test.rb index 795c55e..9f972e9 100644 --- a/test/models/content_test.rb +++ b/test/models/content_test.rb @@ -31,6 +31,62 @@ class ContentTest < ActiveSupport::TestCase assert_equal 1, content.position end + test "move_up swaps position with the previous content" do + contents(:two).move_up + + assert_equal [ contents(:two), contents(:one) ], content_modules(:one).contents.reload.to_a + assert_equal [ 1, 2 ], content_modules(:one).contents.map(&:position) + end + + test "move_down swaps position with the next content" do + contents(:one).move_down + + assert_equal [ contents(:two), contents(:one) ], content_modules(:one).contents.reload.to_a + end + + test "move_up does nothing for the first content" do + contents(:one).move_up + + assert_equal [ 1, 2 ], [ contents(:one).reload.position, contents(:two).reload.position ] + end + + test "move_down does nothing for the last content" do + contents(:two).move_down + + assert_equal [ 1, 2 ], [ contents(:one).reload.position, contents(:two).reload.position ] + end + + test "moving ignores contents in other modules" do + other = create_module.contents.create!(contentable: games(:one)) + order_before = content_modules(:one).contents.map(&:id) + + other.move_up + other.move_down + + assert_equal 1, other.reload.position + assert_equal order_before, content_modules(:one).contents.reload.map(&:id) + end + + test "move works when positions have gaps" do + contents(:two).update!(position: 5) + + contents(:two).move_up + + assert_equal [ contents(:two), contents(:one) ], content_modules(:one).contents.reload.to_a + end + + test "move accepts up and down" do + contents(:one).move("down") + assert_equal [ contents(:two), contents(:one) ], content_modules(:one).contents.reload.to_a + + contents(:one).move("up") + assert_equal [ contents(:one), contents(:two) ], content_modules(:one).contents.reload.to_a + end + + test "move rejects an unknown direction" do + assert_raises(ArgumentError) { contents(:one).move("sideways") } + end + test "cannot add the same game to a module twice" do content_module = create_module content_module.contents.create!(contentable: games(:one)) From ba954709a91e6430ad347bab00364bb4f12cc6ae Mon Sep 17 00:00:00 2001 From: Sean Dickinson Date: Sun, 1 Nov 2026 14:49:29 -0500 Subject: [PATCH 2/2] fix: update tests --- app/models/concerns/ordered.rb | 7 ++- ...uire_positions_for_contents_and_modules.rb | 18 ------ test/models/content_test.rb | 61 ++++++++++--------- 3 files changed, 37 insertions(+), 49 deletions(-) diff --git a/app/models/concerns/ordered.rb b/app/models/concerns/ordered.rb index fc4d227..9a9653e 100644 --- a/app/models/concerns/ordered.rb +++ b/app/models/concerns/ordered.rb @@ -3,7 +3,7 @@ module Ordered extend ActiveSupport::Concern - DIRECTIONS = %w[up down].freeze + DIRECTIONS = %i[up down].freeze included do scope :ordered, -> { order(:position) } @@ -11,10 +11,11 @@ module Ordered before_create :append_to_siblings end + # @param [:up, :down] direction def move(direction) case direction - when "up" then move_up - when "down" then move_down + when :up then move_up + when :down then move_down else raise ArgumentError, "Unknown direction: #{direction.inspect}" end end diff --git a/db/migrate/20261031202333_require_positions_for_contents_and_modules.rb b/db/migrate/20261031202333_require_positions_for_contents_and_modules.rb index e68f709..24c3d4d 100644 --- a/db/migrate/20261031202333_require_positions_for_contents_and_modules.rb +++ b/db/migrate/20261031202333_require_positions_for_contents_and_modules.rb @@ -1,23 +1,5 @@ class RequirePositionsForContentsAndModules < ActiveRecord::Migration[8.1] def change - reversible do |dir| - dir.up do - # Number modules from 1 within each program and level: current position first (NULLs last), then id. - execute <<~SQL - UPDATE content_modules SET position = ( - SELECT ranked.row_number FROM ( - SELECT id, ROW_NUMBER() OVER ( - PARTITION BY program_id, level - ORDER BY position IS NULL, position, id - ) AS row_number - FROM content_modules - ) ranked - WHERE ranked.id = content_modules.id - ) - SQL - end - end - change_column_null :content_modules, :position, false add_index :content_modules, [ :program_id, :level, :position ] change_column_null :contents, :position, false diff --git a/test/models/content_test.rb b/test/models/content_test.rb index 9f972e9..6686f22 100644 --- a/test/models/content_test.rb +++ b/test/models/content_test.rb @@ -9,13 +9,6 @@ class ContentTest < ActiveSupport::TestCase assert_equal "Game", content.contentable_type end - test "belongs to a content module" do - content_module = create_module - content = content_module.contents.create!(contentable: games(:one)) - - assert_equal content_module, content.content_module - end - test "appends new content after the highest position in its module" do content_module = create_module existing = content_module.contents.create!(contentable: games(:one)) @@ -32,59 +25,71 @@ class ContentTest < ActiveSupport::TestCase end test "move_up swaps position with the previous content" do - contents(:two).move_up + contents(:one).update!(position: 1) + contents(:two).update!(position: 2) + + assert_changes -> { contents(:two).position }, from: 2, to: 1 do + contents(:two).move_up + end assert_equal [ contents(:two), contents(:one) ], content_modules(:one).contents.reload.to_a - assert_equal [ 1, 2 ], content_modules(:one).contents.map(&:position) end test "move_down swaps position with the next content" do - contents(:one).move_down + contents(:one).update!(position: 1) + contents(:two).update!(position: 2) + + assert_changes -> { contents(:one).position }, from: 1, to: 2 do + contents(:one).move_down + end assert_equal [ contents(:two), contents(:one) ], content_modules(:one).contents.reload.to_a end test "move_up does nothing for the first content" do - contents(:one).move_up + contents(:one).update!(position: 1) + contents(:two).update!(position: 2) + + assert_no_difference -> { contents(:one).position } do + contents(:one).move_up + end assert_equal [ 1, 2 ], [ contents(:one).reload.position, contents(:two).reload.position ] end test "move_down does nothing for the last content" do + contents(:one).update!(position: 1) + contents(:two).update!(position: 2) + contents(:two).move_down assert_equal [ 1, 2 ], [ contents(:one).reload.position, contents(:two).reload.position ] end test "moving ignores contents in other modules" do + contents(:one).update!(position: 1) + contents(:two).update!(position: 2) other = create_module.contents.create!(contentable: games(:one)) - order_before = content_modules(:one).contents.map(&:id) - - other.move_up - other.move_down - - assert_equal 1, other.reload.position - assert_equal order_before, content_modules(:one).contents.reload.map(&:id) - end - - test "move works when positions have gaps" do - contents(:two).update!(position: 5) - contents(:two).move_up - - assert_equal [ contents(:two), contents(:one) ], content_modules(:one).contents.reload.to_a + assert_no_changes -> { content_modules(:one).contents.map(&:id) } do + other.move_up + other.move_down + end end test "move accepts up and down" do - contents(:one).move("down") + contents(:one).update!(position: 1) + contents(:two).update!(position: 2) + + contents(:one).move(:down) assert_equal [ contents(:two), contents(:one) ], content_modules(:one).contents.reload.to_a - contents(:one).move("up") + contents(:one).move(:up) assert_equal [ contents(:one), contents(:two) ], content_modules(:one).contents.reload.to_a end test "move rejects an unknown direction" do - assert_raises(ArgumentError) { contents(:one).move("sideways") } + assert_raises(ArgumentError) { contents(:one).move(:sideways) } end test "cannot add the same game to a module twice" do