Class: Goldberg::MenuItem

Inherits:
ActiveRecord::Base show all
Includes:
Model
Defined in:
lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu_item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

included

Methods inherited from ActiveRecord::Base

#associated_valid?, #no_errors_in_associated?, #save_associated, #save_associated!, #save_with_unsaved_flag, #to_label, #unsaved=, #unsaved?

Instance Attribute Details

#content_pageObject

Returns the value of attribute content_page.



8
9
10
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu_item.rb', line 8

def content_page
  @content_page
end

#controller_actionObject

Returns the value of attribute controller_action.



8
9
10
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu_item.rb', line 8

def controller_action
  @controller_action
end

Class Method Details

.items_for_permissions(permission_ids = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu_item.rb', line 73

def MenuItem.items_for_permissions(permission_ids = nil)
  # Hash for faster & easier lookups
  if permission_ids
    perms = {}
    for id in permission_ids do
      perms[id] = true
    end
  end

  # List of items to return
  items = []

  menu_items = self.find(:all,
                         :order => 'parent_id, seq, id')
  for item in menu_items do
    if item.controller_action_id.to_i > 0
      item.controller_action = 
        ControllerAction.find(item.controller_action_id)
      if perms
        if perms.has_key?(item.controller_action.effective_permission.id)
          items << item
        end
      else
        items << item
      end
    elsif item.content_page_id.to_i > 0
      item.content_page = 
        ContentPage.find(item.content_page_id)
      if perms
        if perms.has_key?(item.content_page.permission_id)
          items << item
        end
      else
        items << item
      end
    end
  end

  return items
end

.next_seq(parent_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu_item.rb', line 58

def MenuItem.next_seq(parent_id)
  if parent_id and parent_id.to_i > 0
    next_seq = MenuItem.find_by_sql("select coalesce(max(seq) + 1, 1) as seq from #{prefix}menu_items where parent_id = #{parent_id}")
  else
    next_seq = MenuItem.find_by_sql("select coalesce(max(seq) + 1, 1) as seq from #{prefix}menu_items where parent_id is null")
  end

  if next_seq
    return next_seq[0].seq
  else
    return 1
  end
end

.repack(repack_id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu_item.rb', line 38

def MenuItem.repack(repack_id)
  if repack_id
    items = MenuItem.find(:all,
                          :conditions => "parent_id = #{repack_id}",
                          :order => 'seq')
  else
    items = MenuItem.find(:all,
                          :conditions => "parent_id is null",
                          :order => 'seq')
  end

  seq = 1
  for item in items do
    item.seq = seq
    item.save!
    seq += 1
  end
end

Instance Method Details

#aboveObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu_item.rb', line 10

def above
  if self.parent_id
    conditions = 
      ["parent_id = ? and seq = ?", self.parent_id, self.seq - 1]
  else
    conditions = 
      ["parent_id is null and seq = ?", self.seq - 1]
  end
  
  return MenuItem.find(:first,
                       :conditions => conditions)
end

#belowObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/menu_item.rb', line 24

def below
  if self.parent_id
    conditions = 
      ["parent_id = ? and seq = ?", self.parent_id, self.seq + 1]
  else
    conditions = 
      ["parent_id is null and seq = ?", self.seq + 1]
  end
  
  return MenuItem.find(:first,
                       :conditions => conditions)
end