Module: MegaMenus::MenuClassMethods
- Defined in:
- lib/mega_menus/editor.rb
Instance Method Summary collapse
- #add_child(menu_id, title, link) ⇒ Object
- #assign_position(parent_id) ⇒ Object
- #check_menus ⇒ Object
- #children(menu_id) ⇒ Object
- #delete_children(menu_id) ⇒ Object
- #delete_item(menu_id) ⇒ Object
- #determine_abs_position_and_depth ⇒ Object
- #edit(menu_id, new_parent_id, title, link) ⇒ Object
-
#get_menu(parent_id, position) ⇒ Object
sorry..for the spin word…:).
- #parent_menu(menu_id) ⇒ Object
- #position_down(id) ⇒ Object
- #position_up(id) ⇒ Object
- #recursive_dapad(rp, ap, depth) ⇒ Object
- #siblings(menu_id) ⇒ Object
- #switch_menu_positions(parent_id, position1, position2) ⇒ Object
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(, title, link) position=assign_position() child = self.new( "title" => title, "link" => link, "parent_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_menus ⇒ 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 |
# File 'lib/mega_menus/editor.rb', line 73 def #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() self.find(:all, :order=> "position" ,:conditions => { :parent_id=>}) end |
#delete_children(menu_id) ⇒ Object
246 247 248 249 250 251 |
# File 'lib/mega_menus/editor.rb', line 246 def delete_children() #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() =self.find() if(children().empty?) siblings().each do |s| if(s.position>.position) s.decrement(:position) s.save! end end else siblings(.id).each do |s| if(s.position>.position) s.position+=children(.id).count - 1 s.save! end end end #actual move children(.id).each do |c| c.position+=.position-1 c.parent_id=.parent_id c.save! end self.delete() end |
#determine_abs_position_and_depth ⇒ Object
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(, new_parent_id, title, link) =self.find() .update_attributes!(:title=> title,:link=> link) if(.parent_id != new_parent_id) position=assign_position(new_parent_id) =self.new(:title=>title, :link=>link, :parent_id=>new_parent_id, :position=>position) .save! children(.id).each do |c| c.parent_id=.id c.save! end siblings().each do |s| if(s.position>.position) s.decrement(:position) s.save! end end self.delete() 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 (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 () m=self.find() 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) =self.find(id) if(!siblings(id).empty?) if(.position< (siblings(id).max {|c1,c2| c1.position <=> c2.position}).position) (.parent_id, .position, .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) =self.find(id) if(.position>1) (.parent_id, .position, .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() p=() ac=children(p.id) i=0 ac.each do |c| if(c.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 (parent_id, position1, position2) =(parent_id,position1) =(parent_id,position2) .position,.position =.position,.position .save! .save! end |