Class: Menu

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

Overview

this is needed for now to make mass assignment security compatible with the translation of globalize3 Globalize::ActiveRecord::Translation.class_eval do

attr_accessible :locale

end

Instance Method Summary collapse

Instance Method Details

#childrenObject



32
33
34
# File 'app/models/menu.rb', line 32

def children
  Menu.with_parent_id(id)
end

#has_children?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/models/menu.rb', line 28

def has_children?
  Menu.with_parent_id(id).count >= 1
end

#has_parent?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/menu.rb', line 36

def has_parent?
  !parent_id.nil?
end

#has_siblings?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/menu.rb', line 44

def has_siblings?
  Menu.with_parent_id(parent_id).count >= 1
end

#orphan?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/menu.rb', line 24

def orphan?
  parent_id == nil
end

#parentObject



40
41
42
# File 'app/models/menu.rb', line 40

def parent
  Menu.find(parent_id)
end

#parent_menu_nameObject

autocomplete related instance methods



61
62
63
# File 'app/models/menu.rb', line 61

def parent_menu_name
  parent.try(:name) unless self.orphan?
end

#parent_menu_name=(name) ⇒ Object



65
66
67
68
69
70
71
# File 'app/models/menu.rb', line 65

def parent_menu_name=(name)
  if name.present? && Menu.find_by_name(name)
    self.parent_id = Menu.find_by_name(name).id
  else
    self.parent_id = nil
  end
end

#self_and_siblingsObject



52
53
54
55
56
57
58
# File 'app/models/menu.rb', line 52

def self_and_siblings
  if has_parent?
    parent.children
  else
    Menu.orphans
  end
end

#siblingsObject



48
49
50
# File 'app/models/menu.rb', line 48

def siblings
  self_and_siblings - [self]
end

#to_paramObject



14
15
16
# File 'app/models/menu.rb', line 14

def to_param
  name ? "#{id}-#{name.to_url}" : id
end