cirandas.net

ref: master

app/models/tag.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Tag = ActsAsTaggableOn::Tag
class Tag

  attr_accessible :name, :parent_id, :pending

  has_many :children, class_name: 'Tag', foreign_key: 'parent_id', dependent: :destroy

  @@original_find = self.method(:find)
  # Rename the find method to find_with_pendings that includes all tags in the search regardless if its pending or not
  def self.find_with_pendings(*args)
    @@original_find.call(*args)
  end

  # Redefine the find method to exclude the pending tags from the search not allowing to tag something with an unapproved tag
  def self.find(*args)
    self.where(pending: false).find_with_pendings(*args)
  end

  # Return all the tags that were suggested but not yet approved
  def self.find_pendings
    self.where(pending: true)
  end

  # All the tags that can be a new parent for this tag, that is all but itself and its descendents to avoid loops
  def parent_candidates
    ActsAsTaggableOn::Tag.all - descendents - [self]
  end

  # All tags that have this tag as its one of its ancestors
  def descendents
    children.to_a.sum([], &:descendents) + children
  end

end