<commit_before>module CollectiveIdea #:nodoc:
  module Acts #:nodoc:
    module NestedSet #:nodoc:
      class Move
        attr_reader :target, :position, :instance

        delegate :left, :right, :quoted_left_column_name, :quoted_right_column_name, :quoted_parent_column_name, :parent_column_name, to: :instance

        def initialize(target, position, instance)
          @target = target
          @position = position
          @instance = instance
        end

        def move
          if not_root && !instance.move_possible?(target)
            raise ActiveRecord::ActiveRecordError, "Impossible move, target node cannot be inside moved tree."
          end

          bound, other_bound = get_boundaries

          # there would be no change
          return if bound == right || bound == left

          # we have defined the boundaries of two non-overlapping intervals,
          # so sorting puts both the intervals and their boundaries in order
          a, b, c, d = [left, right, bound, other_bound].sort

          # select the rows in the model between a and d, and apply a lock
          instance.class.base_class.select('id').lock(true).right_of(a).left_of_right_side(d)

          instance.nested_set_scope.where(*where_statement(a,d)).update_all(conditions(a,b,c,d))
        end

        private

        def where_statement(a,d)
          ["(#{quoted_left_column_name} BETWEEN :a AND :d) OR (#{quoted_right_column_name} BETWEEN :a AND :d)", {:a => a, :d => d}]
        end

        def conditions(a,b,c,d)
          [
           "#{quoted_left_column_name} = CASE " +
           "WHEN #{quoted_left_column_name} BETWEEN :a AND :b " +
           "THEN #{quoted_left_column_name} + :d - :b " +
           "WHEN #{quoted_left_column_name} BETWEEN :c AND :d " +
           "THEN #{quoted_left_column_name} + :a - :c " +
           "ELSE #{quoted_left_column_name} END, " +
           "#{quoted_right_column_name} = CASE " +
           "WHEN #{quoted_right_column_name} BETWEEN :a AND :b " +
           "THEN #{quoted_right_column_name} + :d - :b " +
           "WHEN #{quoted_right_column_name} BETWEEN :c AND :d " +
           "THEN #{quoted_right_column_name} + :a - :c " +
           "ELSE #{quoted_right_column_name} END, " +
           "#{quoted_parent_column_name} = CASE " +
           "WHEN #{instance.class.base_class.primary_key} = :id THEN :new_parent " +
           "ELSE #{quoted_parent_column_name} END",
           {:a => a, :b => b, :c => c, :d => d, :id => instance.id, :new_parent => new_parent}
          ]
        end

        def not_root
          position != :root
        end

        def new_parent
          case position
          when :child
            target.id
          else
            target[parent_column_name]
          end
        end

        def get_boundaries
          if (bound = target_bound) > right
            bound -= 1
            other_bound = right + 1
          else
            other_bound = left - 1
          end
          [bound, other_bound]
        end

        def target_bound
          case position<commit_msg>refactor complex sql conditions<commit_after>module CollectiveIdea #:nodoc:
  module Acts #:nodoc:
    module NestedSet #:nodoc:
      class Move
        attr_reader :target, :position, :instance

        delegate :left, :right, :quoted_left_column_name, :quoted_right_column_name, :quoted_parent_column_name, :parent_column_name, to: :instance

        def initialize(target, position, instance)
          @target = target
          @position = position
          @instance = instance
        end

        def move
          if not_root && !instance.move_possible?(target)
            raise ActiveRecord::ActiveRecordError, "Impossible move, target node cannot be inside moved tree."
          end

          bound, other_bound = get_boundaries

          # there would be no change
          return if bound == right || bound == left

          # we have defined the boundaries of two non-overlapping intervals,
          # so sorting puts both the intervals and their boundaries in order
          a, b, c, d = [left, right, bound, other_bound].sort

          # select the rows in the model between a and d, and apply a lock
          instance.class.base_class.select('id').lock(true).right_of(a).left_of_right_side(d)

          instance.nested_set_scope.where(*where_statement(a,d)).update_all(conditions(a,b,c,d))
        end

        private

        def where_statement(a,d)
          ["(#{quoted_left_column_name} BETWEEN :a AND :d) OR (#{quoted_right_column_name} BETWEEN :a AND :d)", {:a => a, :d => d}]
        end

        def conditions(a,b,c,d)
          [
           case_condition_for_direction(:quoted_left_column_name) +
           case_condition_for_direction(:quoted_right_column_name) +
           case_condition_for_parent,
           {:a => a, :b => b, :c => c, :d => d, :id => instance.id, :new_parent => new_parent}
          ]
        end

        def case_condition_for_direction(column_name)
          column = send(column_name)
          "#{column} = CASE " +
            "WHEN #{column} BETWEEN :a AND :b " +
            "THEN #{column} + :d - :b " +
            "WHEN #{column} BETWEEN :c AND :d " +
            "THEN #{column} + :a - :c " +
            "ELSE #{column} END, "
        end

        def case_condition_for_parent
          "#{quoted_parent_column_name} = CASE " +
            "WHEN #{instance.class.base_class.primary_key} = :id THEN :new_parent " +
            "ELSE #{quoted_parent_column_name} END"
        end

        def not_root
          position != :root
        end

        def new_parent
          case position
          when :child
            target.id
          else
            target[parent_column_name]
          end
        end

        def get_boundaries
          if (bound = target_bound) > right
            bound -= 1
            other_bound = right + 1
          else
            other_bound = left - 1
          end
          [bound, other_bound]
        end

        def target_bound
          case position
{'fraction_non_alphanumeric': 0.09942386831275721, 'fraction_numerical': 0.0009876543209876543, 'mean_word_length': 1.9740577581987273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4415477', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1803, 'n_words': 478}