Module: MegaMenus::MenuClassMethods

Defined in:
lib/mega_menus/editor.rb

Instance Method Summary collapse

Instance Method Details

#add_child(menu_id, title, link) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/mega_menus/editor.rb', line 101

def add_child(menu_id, title, link)
  position=assign_position(menu_id)
  child = self.new( "title" => title,
                    "link" => link,
                    "parent_id" => menu_id,
                    "position" => position,
                    "published" => FALSE)
  child.save!
end

#assign_position(parent_id) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/mega_menus/editor.rb', line 111

def assign_position(parent_id)
  c=children(parent_id)
  if(!c.empty?)
    position= (c.max {|c1,c2| c1.position <=> c2.position}).position + 1  
  else
    position=1
  end
  return position
end

#check_menusObject



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
# File 'lib/mega_menus/editor.rb', line 73

def check_menus
  #title, id, parent_id, link
  cn=self.column_names
  if(!cn.include?("id"))
    return FALSE
  end
  if(!cn.include?("title"))
    return FALSE
  end
  if(!cn.include?("parent_id"))
    return FALSE
  end
  if(!cn.include?("link"))
    return FALSE
  end
  if(!cn.include?("position"))
    return FALSE
  end
  if(!cn.include?("absolute_position"))
    return FALSE
  end
  if(!cn.include?("depth"))
    return FALSE
  end
  #model correct
  return TRUE
end

#children(menu_id) ⇒ Object



121
122
123
# File 'lib/mega_menus/editor.rb', line 121

def children(menu_id)
  self.find(:all, :order=> "position" ,:conditions => { :parent_id=>menu_id})
end

#delete_children(menu_id) ⇒ Object



246
247
248
249
250
251
# File 'lib/mega_menus/editor.rb', line 246

def delete_children(menu_id)
  #need to implement all_children first
  #self.find(menu_id).all_children.each do |c|
  #  self.delete(c.id)
  #end        
end

#delete_item(menu_id) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mega_menus/editor.rb', line 194

def delete_item(menu_id)
  menu=self.find(menu_id)
  if(children(menu_id).empty?)
    siblings(menu_id).each do |s|
      if(s.position>menu.position)
        s.decrement(:position)
        s.save! 
      end
    end
  else
    siblings(menu.id).each do |s|
      if(s.position>menu.position)
        s.position+=children(menu.id).count - 1
        s.save! 
      end
    end
  end
  #actual move
  children(menu.id).each do |c|
    c.position+=menu.position-1
    c.parent_id=menu.parent_id
    c.save!
  end
  self.delete(menu_id)
end

#determine_abs_position_and_depthObject



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/mega_menus/editor.rb', line 220

def determine_abs_position_and_depth
  roots=self.find(:all,:order=>"position",:conditions => {:parent_id=>1} )
  ap=0
  depth=1
  roots.each do |r|
    ap+=1
    r.absolute_position=ap
    r.depth=depth
    r.save!
    ap,depth=recursive_dapad(r,ap,depth)
  end
end

#edit(menu_id, new_parent_id, title, link) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mega_menus/editor.rb', line 173

def edit(menu_id, new_parent_id, title, link)
  menu=self.find(menu_id)
  menu.update_attributes!(:title=> title,:link=> link)
  if(menu.parent_id != new_parent_id)
    position=assign_position(new_parent_id)
    newmenu=self.new(:title=>title, :link=>link, :parent_id=>new_parent_id, :position=>position)
    newmenu.save!
    children(menu.id).each do |c|
      c.parent_id=newmenu.id
      c.save!
    end
    siblings(menu).each do |s|
      if(s.position>menu.position)
        s.decrement(:position)
        s.save!
      end
    end
    self.delete(menu_id)
    end
end

#get_menu(parent_id, position) ⇒ Object

sorry..for the spin word…:)



145
146
147
# File 'lib/mega_menus/editor.rb', line 145

def get_menu(parent_id, position)  
  self.find(:first,:conditions => { :parent_id=>parent_id, :position => position})
end

#parent_menu(menu_id) ⇒ Object



125
126
127
128
# File 'lib/mega_menus/editor.rb', line 125

def parent_menu(menu_id)
  m=self.find(menu_id)
  self.find(:first,:conditions => { :id=>m.parent_id})
end

#position_down(id) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/mega_menus/editor.rb', line 164

def position_down(id)
  menu=self.find(id)
  if(!siblings(id).empty?)
    if(menu.position< (siblings(id).max {|c1,c2| c1.position <=> c2.position}).position)
      switch_menu_positions(menu.parent_id, menu.position, menu.position+1)
    end
  end
end

#position_up(id) ⇒ Object



157
158
159
160
161
162
# File 'lib/mega_menus/editor.rb', line 157

def position_up(id)
  menu=self.find(id)
  if(menu.position>1)
    switch_menu_positions(menu.parent_id, menu.position, menu.position-1)
  end
end

#recursive_dapad(rp, ap, depth) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/mega_menus/editor.rb', line 233

def recursive_dapad(rp,ap,depth)
  depth+=1
  children(rp.id).each do |m|
    ap+=1
    m.absolute_position=ap
    m.depth=depth
    m.save!
    ap,depth=recursive_dapad(m,ap,depth)
  end
  depth-=1
  return ap,depth
end

#siblings(menu_id) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/mega_menus/editor.rb', line 130

def siblings(menu_id)
  p=parent_menu(menu_id)
  ac=children(p.id)
  i=0
  ac.each do |c|
    if(c.id==menu_id)
      ac.delete_at(i) 
      break         
    end
    i=i+1
  end
  return ac
end

#switch_menu_positions(parent_id, position1, position2) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/mega_menus/editor.rb', line 149

def switch_menu_positions(parent_id, position1, position2)
  menu1=get_menu(parent_id,position1)
  menu2=get_menu(parent_id,position2)
  menu1.position,menu2.position =menu2.position,menu1.position
  menu1.save!
  menu2.save!
end