-
Notifications
You must be signed in to change notification settings - Fork 2
/
cat.rb
48 lines (37 loc) · 1.11 KB
/
cat.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Cat < ActiveRecord::Base
has_many :accounts, :through => :cattings
has_many :contacts, :through => :cattings
has_many :leads, :through => :cattings
has_many :opportunities, :through => :cattings
has_many :campaigns, :through => :cattings
has_many :cattings
acts_as_nested_set
acts_as_paranoid
simple_column_search :name, :description, :escape => lambda { |query| query.gsub(/[^\w\s\-\.']/, "").strip }
named_scope :find_for_model, lambda { |model|
{ :conditions => { :cat_type => model } }
}
def self.root_nodes
find(:all, :conditions => 'parent_id IS NULL')
end
def self.find_children(start_id = nil)
start_id.to_i == 0 ? root_nodes : find(start_id).direct_children
end
def leaf
unknown? || children_count == 0
end
def to_json_with_leaf(options = {})
self.to_json_without_leaf(options.merge(:methods => :leaf))
end
alias_method_chain :to_json, :leaf
def ancestors_name
if parent
parent.ancestors_name + parent.name + '::'
else
""
end
end
def long_name
ancestors_name + name
end
end