Class: Caboose::MediaCategory

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/media_category.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.flat_tree(site_id, prefix = '-') ⇒ Object


30
31
32
33
34
35
36
# File 'app/models/caboose/media_category.rb', line 30

def self.flat_tree(site_id, prefix = '-')
  arr = []
  self.tree(site_id).each do |cat|
    arr += self.flat_tree_helper(cat, prefix, '')
  end    
  return arr
end

.flat_tree_helper(cat, prefix, str) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/caboose/media_category.rb', line 38

def self.flat_tree_helper(cat, prefix, str)        
  cat.name = "#{str}#{cat.name}"
  if cat.name == "-  Products"
    return []
  else
    arr = [{
      :id          => cat.id,
      :parent_id   => cat.parent_id,
      :site_id     => cat.site_id,      
      :name        => cat.name,
      :media_count => cat.media.count      
    }]
    cat.children.each do |cat2|
      arr += self.flat_tree_helper(cat2, prefix, "#{str}#{prefix}")
    end
    return arr
  end
end

.top_category(site_id) ⇒ Object


9
10
11
12
13
# File 'app/models/caboose/media_category.rb', line 9

def self.top_category(site_id)
  cat = self.where(:site_id => site_id, :parent_id => nil).first
  cat = self.create(:site_id => site_id, :parent_id => nil, :name => 'Media') if cat.nil?
  return cat    
end

.tree(site_id) ⇒ Object


26
27
28
# File 'app/models/caboose/media_category.rb', line 26

def self.tree(site_id)
  return self.where(:parent_id => nil, :site_id => site_id).reorder("name").all
end

.tree_hash(site_id) ⇒ Object


57
58
59
60
61
# File 'app/models/caboose/media_category.rb', line 57

def self.tree_hash(site_id)
  top_cat = self.where(:parent_id => nil, :site_id => site_id).first
  top_cat = Caboose::MediaCategory.create(:parent_id => nil, :site_id => site_id, :name => 'Media') if top_cat.nil?
  return self.tree_hash_helper(top_cat)
end

.tree_hash_helper(cat) ⇒ Object


63
64
65
66
67
68
69
70
# File 'app/models/caboose/media_category.rb', line 63

def self.tree_hash_helper(cat)
  return {
    :id => cat.id,
    :name => cat.name,
    :media_count => cat.media.count,
    :children => (cat.name == 'Products' ? [] : cat.children.collect{ |kid| self.tree_hash_helper(kid) })
  }
end

Instance Method Details

#api_hashObject


15
16
17
18
19
20
21
22
23
24
# File 'app/models/caboose/media_category.rb', line 15

def api_hash
  {
    :id        => self.id,
    :parent_id => self.parent_id,
    :site_id   => self.site_id,      
    :name      => self.name,
    :children  => self.children.collect { |child| child.api_hash },
    :media     => self.media.collect { |m| m.api_hash }      
  }
end

#is_ancestor_of?(cat) ⇒ Boolean

Returns:

  • (Boolean)

72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/caboose/media_category.rb', line 72

def is_ancestor_of?(cat)    
  if cat.is_a?(Integer) || cat.is_a?(String)
    cat_id = cat.to_i
    return false if cat_id == -1
    cat = Caboose::MediaCategory.find(cat_id)
  end
  return false if cat.parent_id.nil?
  return false if cat.parent.nil?
  return true  if cat.parent.id == self.id
  return is_ancestor_of?(cat.parent)      
end

#is_child_of?(cat) ⇒ Boolean

Returns:

  • (Boolean)

84
85
86
87
# File 'app/models/caboose/media_category.rb', line 84

def is_child_of?(cat)
  cat = Caboose::MediaCategory.find(cat) if cat.is_a?(Integer)
  return cat.is_ancestor_of?(self)      
end