Sha256: b10ff52ca460b96f87c7a10bf18aaee4cc3eb982694aa4afdb7b0294ae4cc885

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

module CollectiveIdea #:nodoc:
  module Acts #:nodoc:
    module NestedSet #:nodoc:
      class SetValidator

        def initialize(model)
          @model = model
          @scope = model.all
          @parent = arel_table.alias('parent')
        end

        def valid?
          query.count == 0
        end

        private

        attr_reader :model, :parent
        attr_accessor :scope

        delegate :parent_column_name, :primary_key, :left_column_name, :right_column_name, :arel_table,
          :quoted_table_name, :quoted_parent_column_full_name, :quoted_left_column_full_name, :quoted_right_column_full_name, :quoted_left_column_name, :quoted_right_column_name,
        :to => :model

        def query
          join_scope
          filter_scope
        end

        def join_scope
          join_arel = arel_table.join(parent, Arel::Nodes::OuterJoin).on(parent[primary_key].eq(arel_table[parent_column_name]))
          self.scope = scope.joins(join_arel.join_sql)
        end

        def filter_scope
          self.scope = scope.where(
                                   bound_is_null(left_column_name).
                                   or(bound_is_null(right_column_name)).
                                   or(left_bound_greater_than_right).
                                   or(parent_not_null.and(bounds_outside_parent))
                                   )
        end

        def bound_is_null(column_name)
          arel_table[column_name].eq(nil)
        end

        def left_bound_greater_than_right
          arel_table[left_column_name].gteq(arel_table[right_column_name])
        end

        def parent_not_null
          arel_table[parent_column_name].not_eq(nil)
        end

        def bounds_outside_parent
          arel_table[left_column_name].lteq(parent[left_column_name]).or(arel_table[right_column_name].gteq(parent[right_column_name]))
        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
awesome_nested_set-3.0.0.rc.3 lib/awesome_nested_set/set_validator.rb
awesome_nested_set-3.0.0.rc.2 lib/awesome_nested_set/set_validator.rb
awesome_nested_set-3.0.0.rc.1 lib/awesome_nested_set/set_validator.rb
loyal_awesome_nested_set-0.0.1 lib/awesome_nested_set/set_validator.rb