Sha256: 74bc83754eaf3d414add30e23bb00b2dfa0f6f635cdbe7e1cf795661f9d19026

Contents?: true

Size: 1.91 KB

Versions: 16

Compression:

Stored size: 1.91 KB

Contents

module Mongoid::Acts::NestedSet

  module Validation

    # Warning: Very expensive!  Do not use unless you know what you are doing.
    # This method is only useful for determining if the entire tree is valid
    def valid?
      left_and_rights_valid? && no_duplicates_for_fields? && all_roots_valid?
    end


    # Warning: Very expensive!  Do not use unless you know what you are doing.
    def left_and_rights_valid?
      all.detect { |node|
        node.send(left_field_name).nil? ||
          node.send(right_field_name).nil? ||
          node.send(left_field_name) >= node.send(right_field_name) ||
          !node.parent.nil? && (
            node.send(left_field_name) <= node.parent.send(left_field_name) ||
            node.send(right_field_name) >= node.parent.send(right_field_name)
        )
      }.nil?
    end


    # Warning: Very expensive!  Do not use unless you know what you are doing.
    def no_duplicates_for_fields?
      roots.group_by{|record| scope_field_names.collect{|field| record.send(field.to_sym)}}.all? do |scope, grouped_roots|
        [left_field_name, right_field_name].all? do |field|
          grouped_roots.first.nested_set_scope.only(field).group_by {|doc| doc.send(field)}.all? {|k, v| v.size == 1}
        end
      end
    end


    # Wrapper for each_root_valid? that can deal with scope
    # Warning: Very expensive!  Do not use unless you know what you are doing.
    def all_roots_valid?
      if acts_as_nested_set_options[:scope]
        roots.group_by{|record| scope_field_names.collect{|field| record.send(field.to_sym)}}.all? do |scope, grouped_roots|
          each_root_valid?(grouped_roots)
        end
      else
        each_root_valid?(roots)
      end
    end


    def each_root_valid?(roots_to_validate)
      right = 0
      roots_to_validate.all? do |root|
        (root.left > right && root.right > right).tap do
          right = root.right
        end
      end
    end

  end
end

Version data entries

16 entries across 15 versions & 3 rubygems

Version Path
glebtv-mongoid_nested_set-0.8.0 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.7.0 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.6.0 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.5.3 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.5.2 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.5.1 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.5.0 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.4.3 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.4.2 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.4.1 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.4.0 lib/mongoid_nested_set/validation.rb
glebtv-mongoid_nested_set-0.3.0 lib/mongoid_nested_set/validation.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/bundler/gems/mongoid_nested_set-d482b2642889/lib/mongoid_nested_set/validation.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/mongoid_nested_set-0.2.1/lib/mongoid_nested_set/validation.rb
mongoid_nested_set-0.2.1 lib/mongoid_nested_set/validation.rb
mongoid_nested_set-0.2.0 lib/mongoid_nested_set/validation.rb