diff --git a/app/models/concerns/ordered.rb b/app/models/concerns/ordered.rb index c846074..9a9653e 100644 --- a/app/models/concerns/ordered.rb +++ b/app/models/concerns/ordered.rb @@ -1,13 +1,33 @@ # 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 = %i[up down].freeze + included do scope :ordered, -> { order(:position) } 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 + 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 +35,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..24c3d4d --- /dev/null +++ b/db/migrate/20261031202333_require_positions_for_contents_and_modules.rb @@ -0,0 +1,7 @@ +class RequirePositionsForContentsAndModules < ActiveRecord::Migration[8.1] + def change + 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..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)) @@ -31,6 +24,74 @@ class ContentTest < ActiveSupport::TestCase assert_equal 1, content.position end + test "move_up swaps position with the previous content" do + 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 + end + + test "move_down swaps position with the next content" do + 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).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)) + + 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).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) + 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))