Class: Sharkey::Category

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/sharkey/models.rb

Overview

Category for Links.

While a Link can have several Tags, it can only have a single Category.

Think of it as a folder on your Bookmarks browser.

A category can have one parent and many children.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.orphansObject



254
255
256
# File 'lib/sharkey/models.rb', line 254

def self.orphans
  all.select { |me| me.parent.nil? }
end

Instance Method Details

#add_child(child) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/sharkey/models.rb', line 220

def add_child child
  return if (child.nil?) or (child == self) or (has_child? child)
  return if (child.parent) and (child.parent == self)

  self.childs << child
  child.parent = self

  self.save
  self
end

#has_child?(child) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/sharkey/models.rb', line 231

def has_child? child
  self.childs.member? child
end

#remove_child(child) ⇒ Object

Note:

Does not remove any Categories!

Removes the parent/children relationship



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/sharkey/models.rb', line 237

def remove_child child
  throw 'Removing self as child' if child == self

  if self.categoryChilds
    self.categoryChilds.all(target_id: child.id).destroy
  end

  if child.categoryParent
    if child.categoryParent.source_id == self.id
      child.categoryParent.destroy
    end
  end

  self.reload
  self
end