Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion app/models/concerns/ordered.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
# 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
# ordered among (for example, the contents of one module).
#
# @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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class RequirePositionsForContentsAndModules < ActiveRecord::Migration[8.1]
Comment thread
sean-dickinson marked this conversation as resolved.
Resolved
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
7 changes: 4 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/models/content_module_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
75 changes: 68 additions & 7 deletions test/models/content_test.rb
Comment thread
sean-dickinson marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
Loading