module CollectiveIdea::Acts::NestedSet::Model::Validatable

Public Instance Methods

all_roots_valid?() click to toggle source

Wrapper for each_root_valid? that can deal with scope.

# File lib/awesome_nested_set/model/validatable.rb, line 29
def all_roots_valid?
  if acts_as_nested_set_options[:scope]
    all_roots_valid_by_scope?(roots)
  else
    each_root_valid?(roots)
  end
end
all_roots_valid_by_scope?(roots_to_validate) click to toggle source
# File lib/awesome_nested_set/model/validatable.rb, line 37
def all_roots_valid_by_scope?(roots_to_validate)
  roots_grouped_by_scope(roots_to_validate).all? do |scope, grouped_roots|
    each_root_valid?(grouped_roots)
  end
end
each_root_valid?(roots_to_validate) click to toggle source
# File lib/awesome_nested_set/model/validatable.rb, line 43
def each_root_valid?(roots_to_validate)
  left = right = 0
  roots_to_validate.all? do |root|
    (root.left > left && root.right > right).tap do
      left = root.left
      right = root.right
    end
  end
end
left_and_rights_valid?() click to toggle source
# File lib/awesome_nested_set/model/validatable.rb, line 13
def left_and_rights_valid?
  SetValidator.new(self).valid?
end
no_duplicates_for_columns?() click to toggle source
# File lib/awesome_nested_set/model/validatable.rb, line 17
def no_duplicates_for_columns?
  [quoted_left_column_full_name, quoted_right_column_full_name].all? do |column|
    # No duplicates
    select("#{scope_string}#{column}, COUNT(#{column}) as _count").
      group("#{scope_string}#{column}", quoted_primary_key_column_full_name).
      having("COUNT(#{column}) > 1").
      order(quoted_primary_key_column_full_name).
      first.nil?
  end
end
valid?() click to toggle source
# File lib/awesome_nested_set/model/validatable.rb, line 9
def valid?
  left_and_rights_valid? && no_duplicates_for_columns? && all_roots_valid?
end

Private Instance Methods

roots_grouped_by_scope(roots_to_group) click to toggle source
# File lib/awesome_nested_set/model/validatable.rb, line 54
def roots_grouped_by_scope(roots_to_group)
  roots_to_group.group_by {|record|
    scope_column_names.collect {|col| record.send(col) }
  }
end
scope_string() click to toggle source
# File lib/awesome_nested_set/model/validatable.rb, line 60
def scope_string
  Array(acts_as_nested_set_options[:scope]).map do |c|
    connection.quote_column_name(c)
  end.push(nil).join(", ")
end