Module: Hyrarchy::AwesomeNestedSetCompatibility::ClassMethods

Included in:
ClassMethods
Defined in:
lib/hyrarchy/awesome_nested_set_compatibility.rb

Instance Method Summary collapse

Instance Method Details

#all_roots_valid?Boolean

Returns true if all roots have no ancestors.

Returns:

  • (Boolean)


47
48
49
# File 'lib/hyrarchy/awesome_nested_set_compatibility.rb', line 47

def all_roots_valid?
  each_root_valid?(roots)
end

#each_root_valid?(roots_to_validate) ⇒ Boolean

Returns true if all of the nodes in roots_to_validate have no ancestors.

Returns:

  • (Boolean)


53
54
55
# File 'lib/hyrarchy/awesome_nested_set_compatibility.rb', line 53

def each_root_valid?(roots_to_validate)
  roots_to_validate.all? {|r| r.root?}
end

#left_and_rights_valid?Boolean

Returns true if the model’s left and right values match the parent_id attributes.

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hyrarchy/awesome_nested_set_compatibility.rb', line 17

def left_and_rights_valid?
  # Load all nodes and index them by ID so we can leave the database
  # alone.
  nodes = connection.select_all("SELECT id, lft_numer, lft_denom, parent_id FROM #{quoted_table_name}")
  nodes_by_id = {}
  nodes.each do |node|
    node['id']           = node['id'].to_i
    node['encoded_path'] = Hyrarchy::EncodedPath(node['lft_numer'].to_i, node['lft_denom'].to_i)
    node['parent_id']    = node['parent_id'] ? node['parent_id'].to_i : nil
    nodes_by_id[node['id']] = node
  end
  # Check to see if the structure defined by the nodes' encoded paths
  # matches the structure defined by their parent_id attributes.
  nodes.all? do |node|
    if node['parent_id'].nil?
      node['encoded_path'].parent == nil rescue false
    else
      parent = nodes_by_id[node['parent_id']]
      parent && node['encoded_path'].parent == parent['encoded_path']
    end
  end
end

#no_duplicates_for_columns?Boolean

Always returns true. This method exists solely for compatibility with awesome_nested_set; the test it performs doesn’t apply to Hyrarchy.

Returns:

  • (Boolean)


42
43
44
# File 'lib/hyrarchy/awesome_nested_set_compatibility.rb', line 42

def no_duplicates_for_columns?
  true
end

#rebuild!Object

Rebuilds the model’s hierarchy attributes based on the parent_id attributes.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hyrarchy/awesome_nested_set_compatibility.rb', line 59

def rebuild!
  return true if (valid? rescue false)
  
  update_all("lft = id, rgt = id, lft_numer = id, lft_denom = id")
  paths_by_id = {}
  order_by = columns_hash['created_at'] ? :created_at : :id
  
  nodes = roots :order => order_by
  until nodes.empty? do
    nodes.each do |node|
      parent_path = paths_by_id[node.parent_id] || Hyrarchy::EncodedPath::ROOT
      node.send(:encoded_path=, next_child_encoded_path(parent_path))
      node.send(:create_or_update_without_callbacks) || raise(RecordNotSaved)
      paths_by_id[node.id] = node.send(:encoded_path)
    end
    node_ids = nodes.collect {|n| n.id}
    nodes = find(:all, :conditions => { :parent_id => node_ids }, :order => order_by)
  end
end

#rootObject

Returns the first root node.



5
6
7
# File 'lib/hyrarchy/awesome_nested_set_compatibility.rb', line 5

def root
  roots.first
end

#valid?Boolean

Returns true if the model’s left and right values are valid, and all root nodes have no ancestors.

Returns:

  • (Boolean)


11
12
13
# File 'lib/hyrarchy/awesome_nested_set_compatibility.rb', line 11

def valid?
  left_and_rights_valid? && all_roots_valid?
end