Module: CollectiveIdea::Acts::NestedSet::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/awesome_nested_set/model.rb,
lib/awesome_nested_set/model/movable.rb,
lib/awesome_nested_set/model/prunable.rb,
lib/awesome_nested_set/model/relatable.rb,
lib/awesome_nested_set/model/rebuildable.rb,
lib/awesome_nested_set/model/validatable.rb,
lib/awesome_nested_set/model/transactable.rb

Defined Under Namespace

Modules: ClassMethods, Movable, Prunable, Rebuildable, Relatable, Transactable, Validatable

Instance Method Summary collapse

Instance Method Details

#child?Boolean

Returns true is this is a child node

Returns:

  • (Boolean)


129
130
131
# File 'lib/awesome_nested_set/model.rb', line 129

def child?
  !root?
end

#leaf?Boolean

Returns true if this is the end of a branch.

Returns:

  • (Boolean)


134
135
136
# File 'lib/awesome_nested_set/model.rb', line 134

def leaf?
  persisted? && right.to_i - left.to_i == 1
end

#left(target = self) ⇒ Object

Value of the left column



114
115
116
# File 'lib/awesome_nested_set/model.rb', line 114

def left(target = self)
  target[left_column_name]
end

#nested_set_scope(options = {}) ⇒ Object

All nested set queries should use this nested_set_scope, which performs finds on the base ActiveRecord class, using the :scope declared in the acts_as_nested_set declaration.



141
142
143
144
145
146
147
148
149
# File 'lib/awesome_nested_set/model.rb', line 141

def nested_set_scope(options = {})
  if (scopes = Array(acts_as_nested_set_options[:scope])).any?
    options[:conditions] = scopes.inject({}) do |conditions,attr|
      conditions.merge attr => self[attr]
    end
  end

  self.class.base_class.nested_set_scope options
end

#nested_set_scope_without_default_scope(*args) ⇒ Object

Separate an other ‘nested_set_scope` for unscoped model because normal query still need activerecord `default_scope` Only activerecord callbacks need unscoped model to handle the nested set records And class level `nested_set_scope` seems just for query `root` `child` .. etc I think we don’t have to provide unscoped ‘nested_set_scope` in class level.



156
157
158
159
160
# File 'lib/awesome_nested_set/model.rb', line 156

def nested_set_scope_without_default_scope(*args)
  self.class.base_class.unscoped do
    nested_set_scope(*args)
  end
end

#parent_id(target = self) ⇒ Object

Any instance method that returns a collection makes use of Rails 2.1’s named_scope (which is bundled for Rails 2.0), so it can be treated as a finder.

category.self_and_descendants.count
category.ancestors.find(:all, :conditions => "name like '%foo%'")

Value of the parent column



105
106
107
# File 'lib/awesome_nested_set/model.rb', line 105

def parent_id(target = self)
  target[parent_column_name]
end

#primary_id(target = self) ⇒ Object



109
110
111
# File 'lib/awesome_nested_set/model.rb', line 109

def primary_id(target = self)
  target[primary_column_name]
end

#right(target = self) ⇒ Object

Value of the right column



119
120
121
# File 'lib/awesome_nested_set/model.rb', line 119

def right(target = self)
  target[right_column_name]
end

#root?Boolean

Returns true if this is a root node.

Returns:

  • (Boolean)


124
125
126
# File 'lib/awesome_nested_set/model.rb', line 124

def root?
  parent_id.nil?
end

#to_textObject



162
163
164
165
166
# File 'lib/awesome_nested_set/model.rb', line 162

def to_text
  self_and_descendants.map do |node|
    "#{'*'*(node.level+1)} #{node.primary_id} #{node.to_s} (#{node.parent_id}, #{node.left}, #{node.right})"
  end.join("\n")
end