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
29 def all_roots_valid?
30   if acts_as_nested_set_options[:scope]
31     all_roots_valid_by_scope?(roots)
32   else
33     each_root_valid?(roots)
34   end
35 end
all_roots_valid_by_scope?(roots_to_validate) click to toggle source
   # File lib/awesome_nested_set/model/validatable.rb
37 def all_roots_valid_by_scope?(roots_to_validate)
38   roots_grouped_by_scope(roots_to_validate).all? do |scope, grouped_roots|
39     each_root_valid?(grouped_roots)
40   end
41 end
each_root_valid?(roots_to_validate) click to toggle source
   # File lib/awesome_nested_set/model/validatable.rb
43 def each_root_valid?(roots_to_validate)
44   left_column = acts_as_nested_set_options[:left_column]
45   reordered_roots = roots_reordered_by_column(roots_to_validate, left_column)
46   left = right = 0
47   reordered_roots.all? do |root|
48     (root.left > left && root.right > right).tap do
49       left = root.left
50       right = root.right
51     end
52   end
53 end
left_and_rights_valid?() click to toggle source
   # File lib/awesome_nested_set/model/validatable.rb
13 def left_and_rights_valid?
14   SetValidator.new(self).valid?
15 end
no_duplicates_for_columns?() click to toggle source
   # File lib/awesome_nested_set/model/validatable.rb
17 def no_duplicates_for_columns?
18   [quoted_left_column_full_name, quoted_right_column_full_name].all? do |column|
19     # No duplicates
20     select("#{scope_string}#{column}, COUNT(#{column}) as _count").
21       group("#{scope_string}#{column}", quoted_primary_key_column_full_name).
22       having("COUNT(#{column}) > 1").
23       order(primary_column_name => :asc).
24       first.nil?
25   end
26 end
valid?() click to toggle source
   # File lib/awesome_nested_set/model/validatable.rb
 9 def valid?
10   left_and_rights_valid? && no_duplicates_for_columns? && all_roots_valid?
11 end

Private Instance Methods

roots_grouped_by_scope(roots_to_group) click to toggle source
   # File lib/awesome_nested_set/model/validatable.rb
56 def roots_grouped_by_scope(roots_to_group)
57   roots_to_group.group_by {|record|
58     scope_column_names.collect {|col| record.send(col) }
59   }
60 end
roots_reordered_by_column(roots_to_reorder, column) click to toggle source
   # File lib/awesome_nested_set/model/validatable.rb
62 def roots_reordered_by_column(roots_to_reorder, column)
63   if roots_to_reorder.respond_to?(:reorder) # ActiveRecord's relation
64     roots_to_reorder.reorder(column)
65   elsif roots_to_reorder.respond_to?(:sort) # Array
66     roots_to_reorder.sort { |a, b| a.send(column) <=> b.send(column) }
67   else
68     roots_to_reorder
69   end
70 end
scope_string() click to toggle source
   # File lib/awesome_nested_set/model/validatable.rb
72 def scope_string
73   Array(acts_as_nested_set_options[:scope]).map do |c|
74     connection.quote_column_name(c)
75   end.push(nil).join(", ")
76 end