Class: Category

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

Instance Method Summary collapse

Instance Method Details

#category_pictureObject



27
28
29
30
31
32
33
# File 'app/models/category.rb', line 27

def category_picture
  unless self.attachments.empty?
    self.attachments.first.public_filename(:categories_icon)
  else
    'folder'
  end
end

#descendantsObject



45
46
47
# File 'app/models/category.rb', line 45

def descendants
  (children + children.map(&:descendants)).flatten
end

#kindObject



15
16
17
# File 'app/models/category.rb', line 15

def kind
  read_attribute(:type)
end

#kind=(kind) ⇒ Object



19
20
21
# File 'app/models/category.rb', line 19

def kind=(kind)
  write_attribute(:type, kind)
end

#levelObject



11
12
13
# File 'app/models/category.rb', line 11

def level
  return self.ancestors.length
end

#to_jstreeObject



35
36
37
38
39
40
41
42
43
# File 'app/models/category.rb', line 35

def to_jstree
  hash = {}
  hash[:attr] = { :id => "#{self.class.to_s.underscore}_#{id}", :type => 'folder' }
  hash[:data] = { :title => name, :icon => category_picture, :attr => { :title => total_elements_count } }
  unless children.empty?
   hash[:children] = children.all(:order => 'position ASC').collect(&:to_jstree)
  end
  hash
end

#total_elements_countObject



23
24
25
# File 'app/models/category.rb', line 23

def total_elements_count
  ([elements.count('id')] + children.all(:select => 'id,type').map(&:total_elements_count)).sum
end