From 4273e9decf7249c9a729624f01134f953fadc450 Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Wed, 9 Dec 2020 14:11:52 +0000 Subject: [PATCH] shorten long line of lib/redmine/nested_set/project_nested_set.rb git-svn-id: http://svn.redmine.org/redmine/trunk@20592 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- lib/redmine/nested_set/project_nested_set.rb | 42 +++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/redmine/nested_set/project_nested_set.rb b/lib/redmine/nested_set/project_nested_set.rb index 55b952f5d..1cef4ea14 100644 --- a/lib/redmine/nested_set/project_nested_set.rb +++ b/lib/redmine/nested_set/project_nested_set.rb @@ -25,7 +25,11 @@ module Redmine belongs_to :parent, :class_name => self.name before_create :add_to_nested_set - before_update :move_in_nested_set, :if => lambda {|project| project.parent_id_changed? || project.name_changed?} + before_update( + :move_in_nested_set, + :if => + lambda {|project| project.parent_id_changed? || project.name_changed?} + ) before_destroy :destroy_children end base.extend ClassMethods @@ -35,12 +39,16 @@ module Redmine private def target_lft - siblings_rgt = self.class.where(:parent_id => parent_id).where("name < ?", name).maximum(:rgt) + siblings_rgt = + self.class.where(:parent_id => parent_id).where("name < ?", name).maximum(:rgt) if siblings_rgt siblings_rgt + 1 elsif parent_id parent_lft = self.class.where(:id => parent_id).pluck(:lft).first - raise "Project id=#{id} with parent_id=#{parent_id}: parent missing or without 'lft' value" unless parent_lft + unless parent_lft + raise "Project id=#{id} with parent_id=#{parent_id}: parent missing or without 'lft' value" + end + parent_lft + 1 else 1 @@ -53,7 +61,7 @@ module Redmine 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, " + + "lft = CASE WHEN lft >= :lft THEN lft + 2 ELSE lft END, " \ "rgt = CASE WHEN rgt >= :lft THEN rgt + 2 ELSE rgt END", {:lft => lft} ] @@ -70,21 +78,33 @@ module Redmine if c > a # 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 = + 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", + "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 = + 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", + "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} ] ) @@ -102,7 +122,7 @@ module Redmine 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, " + + "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} ] -- 2.39.5