lock_nested_set if lock
self.lft = target_lft
self.rgt = lft + 1
- self.class.where("lft >= ? OR rgt >= ?", lft, lft).update_all([
- "lft = CASE WHEN lft >= :lft THEN lft + 2 ELSE lft END, " +
- "rgt = CASE WHEN rgt >= :lft THEN rgt + 2 ELSE rgt END",
- {:lft => lft}
- ])
+ self.class.where("lft >= ? OR rgt >= ?", lft, lft).update_all(
+ [
+ "lft = CASE WHEN lft >= :lft THEN lft + 2 ELSE lft END, " +
+ "rgt = CASE WHEN rgt >= :lft THEN rgt + 2 ELSE rgt END",
+ {:lft => lft}
+ ]
+ )
end
def move_in_nested_set
# Moving to the right
d = c - (b - a + 1)
scope = self.class.where(["lft BETWEEN :a AND :c - 1 OR rgt BETWEEN :a AND :c - 1", {:a => a, :c => c}])
- scope.update_all([
- "lft = CASE WHEN lft BETWEEN :a AND :b THEN lft + (:d - :a) WHEN lft BETWEEN :b + 1 AND :c - 1 THEN lft - (:b - :a + 1) ELSE lft END, " +
- "rgt = CASE WHEN rgt BETWEEN :a AND :b THEN rgt + (:d - :a) WHEN rgt BETWEEN :b + 1 AND :c - 1 THEN rgt - (:b - :a + 1) ELSE rgt END",
- {:a => a, :b => b, :c => c, :d => d}
- ])
+ scope.update_all(
+ [
+ "lft = CASE WHEN lft BETWEEN :a AND :b THEN lft + (:d - :a) WHEN lft BETWEEN :b + 1 AND :c - 1 THEN lft - (:b - :a + 1) ELSE lft END, " +
+ "rgt = CASE WHEN rgt BETWEEN :a AND :b THEN rgt + (:d - :a) WHEN rgt BETWEEN :b + 1 AND :c - 1 THEN rgt - (:b - :a + 1) ELSE rgt END",
+ {:a => a, :b => b, :c => c, :d => d}
+ ]
+ )
elsif c < a
# Moving to the left
scope = self.class.where("lft BETWEEN :c AND :b OR rgt BETWEEN :c AND :b", {:a => a, :b => b, :c => c})
- scope.update_all([
- "lft = CASE WHEN lft BETWEEN :a AND :b THEN lft - (:a - :c) WHEN lft BETWEEN :c AND :a - 1 THEN lft + (:b - :a + 1) ELSE lft END, " +
- "rgt = CASE WHEN rgt BETWEEN :a AND :b THEN rgt - (:a - :c) WHEN rgt BETWEEN :c AND :a - 1 THEN rgt + (:b - :a + 1) ELSE rgt END",
- {:a => a, :b => b, :c => c, :d => d}
- ])
+ scope.update_all(
+ [
+ "lft = CASE WHEN lft BETWEEN :a AND :b THEN lft - (:a - :c) WHEN lft BETWEEN :c AND :a - 1 THEN lft + (:b - :a + 1) ELSE lft END, " +
+ "rgt = CASE WHEN rgt BETWEEN :a AND :b THEN rgt - (:a - :c) WHEN rgt BETWEEN :c AND :a - 1 THEN rgt + (:b - :a + 1) ELSE rgt END",
+ {:a => a, :b => b, :c => c, :d => d}
+ ]
+ )
end
reload_nested_set_values
end
end
children.each {|c| c.send :destroy_without_nested_set_update}
unless @without_nested_set_update
- self.class.where("lft > ? OR rgt > ?", lft, lft).update_all([
- "lft = CASE WHEN lft > :lft THEN lft - :shift ELSE lft END, " +
- "rgt = CASE WHEN rgt > :lft THEN rgt - :shift ELSE rgt END",
- {:lft => lft, :shift => rgt - lft + 1}
- ])
+ self.class.where("lft > ? OR rgt > ?", lft, lft).update_all(
+ [
+ "lft = CASE WHEN lft > :lft THEN lft - :shift ELSE lft END, " +
+ "rgt = CASE WHEN rgt > :lft THEN rgt - :shift ELSE rgt END",
+ {:lft => lft, :shift => rgt - lft + 1}
+ ]
+ )
end
end