Module: ActsAsDAG::InstanceMethods

Defined in:
lib/acts_as_dag/acts_as_dag.rb

Instance Method Summary collapse

Instance Method Details

#add_child(*children) ⇒ Object

Adds a category as a child of this category (self)


165
166
167
168
169
# File 'lib/acts_as_dag/acts_as_dag.rb', line 165

def add_child(*children)
  children.flatten.each do |child|
    ActsAsDAG::HelperMethods.link(self, child)
  end
end

#add_parent(*parents) ⇒ Object

Adds a category as a parent of this category (self)


158
159
160
161
162
# File 'lib/acts_as_dag/acts_as_dag.rb', line 158

def add_parent(*parents)
  parents.flatten.each do |parent|
    ActsAsDAG::HelperMethods.link(parent, self)
  end
end

#ancestor_of?(category, options = {}) ⇒ Boolean

Returns true if the category's descendants include self

Returns:

  • (Boolean)

201
202
203
# File 'lib/acts_as_dag/acts_as_dag.rb', line 201

def ancestor_of?(category, options = {})
  category.ancestors.exists?(id)
end

#child_of?(category, options = {}) ⇒ Boolean

Returns true if the category's children include self

Returns:

  • (Boolean)

186
187
188
# File 'lib/acts_as_dag/acts_as_dag.rb', line 186

def child_of?(category, options = {})
  category.children.exists?(id)
end

#children=(children) ⇒ Object

NOTE: Children that are removed will not trigger the destroy callback on their link, so we need to remove them manually


149
150
151
152
153
154
# File 'lib/acts_as_dag/acts_as_dag.rb', line 149

def children=(children)
  (self.children - children).each do |child_to_remove|
    remove_child(child_to_remove)
  end
  super
end

#descendant_classObject

Returns the class used for descendants


211
212
213
# File 'lib/acts_as_dag/acts_as_dag.rb', line 211

def descendant_class
  self.class.descendant_class
end

#descendant_of?(category, options = {}) ⇒ Boolean

Returns true if the category's descendants include self

Returns:

  • (Boolean)

196
197
198
# File 'lib/acts_as_dag/acts_as_dag.rb', line 196

def descendant_of?(category, options = {})
  category.descendants.exists?(id)
end

#leaf?Boolean

Returns:

  • (Boolean)

130
131
132
# File 'lib/acts_as_dag/acts_as_dag.rb', line 130

def leaf?
  children.empty?
end

#lineageObject

Returns an array of ancestors and descendants


216
217
218
219
220
221
222
223
# File 'lib/acts_as_dag/acts_as_dag.rb', line 216

def lineage
  lineage_links = self.class.descendant_table_entries
                              .select("(CASE ancestor_id WHEN #{id} THEN descendant_id ELSE ancestor_id END) AS id, ancestor_id, descendant_id, distance")
                              .where('ancestor_id = :id OR descendant_id = :id', :id => id)
                              .where('ancestor_id != descendant_id')                        # Don't include self

  self.class.joins("JOIN (#{lineage_links.to_sql}) lineage_links ON #{self.class.table_name}.id = lineage_links.id").order("CASE ancestor_id WHEN #{id} THEN distance ELSE -distance END") # Ensure the links are orders furthest ancestor to furthest descendant
end

Returns the class used for links


206
207
208
# File 'lib/acts_as_dag/acts_as_dag.rb', line 206

def link_class
  self.class.link_class
end

#make_rootObject


134
135
136
137
138
# File 'lib/acts_as_dag/acts_as_dag.rb', line 134

def make_root
  ancestor_links.delete_all
  parent_links.delete_all
  initialize_dag
end

#parent_of?(category, options = {}) ⇒ Boolean

Returns true if the category's parents include self

Returns:

  • (Boolean)

191
192
193
# File 'lib/acts_as_dag/acts_as_dag.rb', line 191

def parent_of?(category, options = {})
  category.parents.exists?(id)
end

#parents=(parents) ⇒ Object

NOTE: Parents that are removed will not trigger the destroy callback on their link, so we need to remove them manually


141
142
143
144
145
146
# File 'lib/acts_as_dag/acts_as_dag.rb', line 141

def parents=(parents)
  (self.parents - parents).each do |parent_to_remove|
    remove_parent(parent_to_remove)
  end
  super
end

#remove_child(child) ⇒ Object

Removes a category as a child of this category (self) Returns the child


173
174
175
176
# File 'lib/acts_as_dag/acts_as_dag.rb', line 173

def remove_child(child)
  ActsAsDAG::HelperMethods.unlink(self, child)
  return child
end

#remove_parent(parent) ⇒ Object

Removes a category as a parent of this category (self) Returns the parent


180
181
182
183
# File 'lib/acts_as_dag/acts_as_dag.rb', line 180

def remove_parent(parent)
  ActsAsDAG::HelperMethods.unlink(parent, self)
  return parent
end

#root?Boolean

Returns true if this record is a root node

Returns:

  • (Boolean)

126
127
128
# File 'lib/acts_as_dag/acts_as_dag.rb', line 126

def root?
  parents.empty?
end