Skip to content
Open
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
12 changes: 6 additions & 6 deletions lib/positioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def positioning_columns
@positioning_columns ||= {}
end

def positioned(on: [], column: :position)
def positioned(on: [], column: :position, touch: false)
unless base_class?
raise Error.new "can't be called on an abstract class or STI subclass."
end
Expand All @@ -43,17 +43,17 @@ def positioned(on: [], column: :position)
end
end

define_method(:"prior_#{column}") { Mechanisms.new(self, column).prior }
define_method(:"subsequent_#{column}") { Mechanisms.new(self, column).subsequent }
define_method(:"prior_#{column}") { Mechanisms.new(self, column, touch).prior }
define_method(:"subsequent_#{column}") { Mechanisms.new(self, column, touch).subsequent }

redefine_method(:"#{column}=") do |position|
send :"#{column}_will_change!"
super(position)
end

before_create { Mechanisms.new(self, column).create_position }
before_update { Mechanisms.new(self, column).update_position }
before_destroy { Mechanisms.new(self, column).destroy_position }
before_create { Mechanisms.new(self, column, touch).create_position }
before_update { Mechanisms.new(self, column, touch).update_position }
before_destroy { Mechanisms.new(self, column, touch).destroy_position }

define_singleton_method(:"heal_#{column}_column!") do |order = column|
Healer.new(self, column, order).heal
Expand Down
15 changes: 12 additions & 3 deletions lib/positioning/mechanisms.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Positioning
class Mechanisms
def initialize(positioned, column)
def initialize(positioned, column, touch)
@positioned = positioned
@column = column.to_sym
@touch = touch
end

def prior
Expand Down Expand Up @@ -106,13 +107,21 @@ def move_out_of_the_way
end

def expand(scope, range)
scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1"
records = scope.where(@column => range)
records.update_all "#{quoted_column} = #{quoted_column} * -1"
scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 + 1"
if @touch
records.find_each { it.touch }
end
end

def contract(scope, range)
scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1"
records = scope.where(@column => range)
records.update_all "#{quoted_column} = #{quoted_column} * -1"
scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 - 1"
if @touch
records.find_each { it.touch }
end
end

def solidify_position
Expand Down