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)


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

def child?
  !root?
end

#leaf?Boolean

Returns true if this is the end of a branch.

Returns:

  • (Boolean)


139
140
141
# File 'lib/awesome_nested_set/model.rb', line 139

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

#left(target = self) ⇒ Object

Value of the left column



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

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.



146
147
148
149
150
# File 'lib/awesome_nested_set/model.rb', line 146

def nested_set_scope(options = {})
  add_scope_conditions_to_options(options)

  self.class.base_class.default_scoped.nested_set_scope options
end

#nested_set_scope_without_default_scope(options = {}) ⇒ 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.



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

def nested_set_scope_without_default_scope(options = {})
  add_scope_conditions_to_options(options)

  self.class.base_class.unscoped.nested_set_scope options
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



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

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

#primary_id(target = self) ⇒ Object



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

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

#right(target = self) ⇒ Object

Value of the right column



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

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

#root?Boolean

Returns true if this is a root node.

Returns:

  • (Boolean)


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

def root?
  parent_id.nil?
end

#to_textObject



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

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