mtoros-mega_menus 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ Manifest
2
+ README.rdoc
3
+ init.rb
4
+ Rakefile
5
+ lib/mega_menus.rb
6
+ lib/mega_menus/editor.rb
7
+ lib/mega_menus/view_helpers.rb
8
+ test/test_editor.rb
9
+ rails_generators/menu/USAGE
10
+ rails_generators/menu/menu_generator.rb
11
+ rails_generators/menu/templates/controllers/menu_controller.rb
12
+ rails_generators/menu/templates/views/add_menu_form.rjs
13
+ rails_generators/menu/templates/views/add_menu.rjs
14
+ rails_generators/menu/templates/views/delete_menu.rjs
15
+ rails_generators/menu/templates/views/edit_menu_form.rjs
16
+ rails_generators/menu/templates/views/edit_menu.rjs
17
+ rails_generators/menu/templates/views/up_menu.rjs
18
+ rails_generators/menu/templates/views/down_menu.rjs
19
+ rails_generators/menu/templates/views/publish_menu.rjs
20
+ rails_generators/menu/templates/views/_menu.html.erb
21
+ rails_generators/menu/templates/helpers/menu_helper.rb
22
+ rails_generators/menu/templates/models/create_menus.rb
23
+ rails_generators/menu/templates/models/menu.rb
24
+ test/test_editor.rb
@@ -0,0 +1,40 @@
1
+ = MegaMenus
2
+
3
+ == DESCRIPTION:
4
+
5
+ This gem makes working with menu structures in Rails easier.
6
+
7
+ == FEATURES/PROBLEMS:
8
+ The current version creates:
9
+ * a controller for editing the menu
10
+ * views associated to methods written in rjs
11
+ * a partial for rendering the menu(for example to be rendered from a menu)
12
+ * a helper to configure its working in normal mode and edit mode, selection of menu depths(levels) to be shown
13
+ * the builtin functions are (add, delete, edit, move up, move down)
14
+ * a model for storing the menu with fields absolute_position and depth to improve performance when the menu is in normal mode
15
+ * a generator for rails to ease the developers work for using this gem, the menu generator creates a template menu structure that the developer can customize.
16
+ * uses session[:menu_id] to identify the selected menu
17
+ * possibility to render different menu depths in different places
18
+ * added a publish button in order to hide or set visible menus (publish model field)
19
+
20
+
21
+ == STATUS
22
+ * Currently writing tests to ensure proper working for future versions.
23
+
24
+
25
+ == REQUIREMENTS:
26
+ * Rails 2.2
27
+
28
+ == NOTES:
29
+ * You can have multiple partials rendering different parts of the menu. Include in your partial the following line:
30
+ - <% mega_menus(name_of_the_menu_model, 'name_of_the_menu_controller', par, arr, parent_id) %>
31
+ - where par is the either TRUE(editor mode) or FALSE(normal mode), arr is an array containing the interval of depths to be shown(2..3 means display menus with depths 2 and 3), parent_id determines to which parent are this children related to( 5 would mean show only children whoose ancestor has id parent_id)
32
+ * If you have multiple partials you should edit the rjs files in order to assure proper updating of each partial. By default only the generated partial is updated.
33
+
34
+ * The menu is located in <ul id="ul_menu_#{m.depth}" class="ul_menu_depth_#{m.depth}">
35
+ * The selected menu is located in <li class="li_menu_class_selected">
36
+ * This two id and class values can be easly changed by editing the mega_menu partial and its helper.
37
+
38
+ == INSTALL:
39
+ * script/generate menu menu_name
40
+
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'echoe'
6
+
7
+ Echoe.new('mega_menus', '0.8.4') do |p|
8
+ p.summary = "Treeview menu Gem for Rails"
9
+ p.description = "Adds a model, controller to perform the tasks in order to have a treeview menu. To use this gem simply install it and write script/generate menu name_of_the_menu"
10
+ p.author = ['Marko Toros']
11
+ p.email = "mtoros@gmail.com"
12
+ p.url = ""
13
+ end
14
+ rescue LoadError => boom8
15
+ puts "You are missing a dependency required for meta-operations on this gem."
16
+ puts "#{boom.to_s.capitalize}."
17
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'mega_menus'
@@ -0,0 +1,22 @@
1
+ module MegaMenus
2
+ class << self
3
+ def enable
4
+ enable_actionpack
5
+ enable_activerecord
6
+ end
7
+
8
+ def enable_actionpack
9
+ # require 'mega_menus/view_helpers'
10
+ # ActionView::Base.send :include, ViewHelpers
11
+ end
12
+
13
+ def enable_activerecord
14
+ require 'mega_menus/editor'
15
+ ActiveRecord::Base.send :include, Editor
16
+ end
17
+ end
18
+ end
19
+
20
+ if defined?(Rails) and defined?(ActiveRecord) and defined?(ActionController)
21
+ MegaMenus.enable
22
+ end
@@ -0,0 +1,253 @@
1
+ module MegaMenus
2
+ module Editor
3
+ #here we want a class method!
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ def isChildOf( parent_id)
9
+ menu_id=self.id
10
+ while(menu_id != 1)
11
+ menu_id= self.class.parent_menu(menu_id).id
12
+ if(menu_id===parent_id)
13
+ return TRUE
14
+ end
15
+ end
16
+ return FALSE
17
+ end
18
+
19
+ def isRootOf( child_id)
20
+ menu_id=self.id
21
+
22
+ while(child_id!=1)
23
+ child_id= self.class.parent_menu(child_id).id
24
+ if(menu_id===child_id)
25
+ return TRUE
26
+ end
27
+ end
28
+ return FALSE
29
+ end
30
+
31
+ def setPublished
32
+ self.published=TRUE
33
+ self.save!
34
+ menu_id=self.id
35
+ allmenus=self.class.all
36
+ allmenus.each do |m|
37
+ if(m.isChildOf(menu_id))
38
+ m.published=TRUE
39
+ m.save!
40
+ end
41
+ if(m.isRootOf(menu_id))
42
+ m.published=TRUE
43
+ m.save!
44
+ end
45
+ end
46
+ end
47
+
48
+ def setNotPublished
49
+ self.published=FALSE
50
+ self.save!
51
+ parent_id=self.id
52
+ allmenus=self.class.all
53
+ allmenus.each do |m|
54
+ if(m.isChildOf(parent_id))
55
+ m.published=FALSE
56
+ m.save!
57
+ end
58
+ end
59
+ end
60
+
61
+ def children
62
+ self.class.children(self.id)
63
+ end
64
+
65
+
66
+ module ClassMethods
67
+ def acts_as_menu
68
+ self.send :extend, MenuClassMethods
69
+ end
70
+ end
71
+ end
72
+ module MenuClassMethods
73
+ def check_menus
74
+ #title, id, parent_id, link
75
+ cn=self.column_names
76
+ if(!cn.include?("id"))
77
+ return FALSE
78
+ end
79
+ if(!cn.include?("title"))
80
+ return FALSE
81
+ end
82
+ if(!cn.include?("parent_id"))
83
+ return FALSE
84
+ end
85
+ if(!cn.include?("link"))
86
+ return FALSE
87
+ end
88
+ if(!cn.include?("position"))
89
+ return FALSE
90
+ end
91
+ if(!cn.include?("absolute_position"))
92
+ return FALSE
93
+ end
94
+ if(!cn.include?("depth"))
95
+ return FALSE
96
+ end
97
+ #model correct
98
+ return TRUE
99
+ end
100
+
101
+ def add_child(menu_id, title, link)
102
+ position=assign_position(menu_id)
103
+ child = self.new( "title" => title,
104
+ "link" => link,
105
+ "parent_id" => menu_id,
106
+ "position" => position,
107
+ "published" => FALSE)
108
+ child.save!
109
+ end
110
+
111
+ def assign_position(parent_id)
112
+ c=children(parent_id)
113
+ if(!c.empty?)
114
+ position= (c.max {|c1,c2| c1.position <=> c2.position}).position + 1
115
+ else
116
+ position=1
117
+ end
118
+ return position
119
+ end
120
+
121
+ def children(menu_id)
122
+ self.find(:all, :order=> "position" ,:conditions => { :parent_id=>menu_id})
123
+ end
124
+
125
+ def parent_menu(menu_id)
126
+ m=self.find(menu_id)
127
+ self.find(:first,:conditions => { :id=>m.parent_id})
128
+ end
129
+
130
+ def siblings(menu_id)
131
+ p=parent_menu(menu_id)
132
+ ac=children(p.id)
133
+ i=0
134
+ ac.each do |c|
135
+ if(c.id==menu_id)
136
+ ac.delete_at(i)
137
+ break
138
+ end
139
+ i=i+1
140
+ end
141
+ return ac
142
+ end
143
+
144
+ #sorry..for the spin word...:)
145
+ def get_menu(parent_id, position)
146
+ self.find(:first,:conditions => { :parent_id=>parent_id, :position => position})
147
+ end
148
+
149
+ def switch_menu_positions(parent_id, position1, position2)
150
+ menu1=get_menu(parent_id,position1)
151
+ menu2=get_menu(parent_id,position2)
152
+ menu1.position,menu2.position =menu2.position,menu1.position
153
+ menu1.save!
154
+ menu2.save!
155
+ end
156
+
157
+ def position_up(id)
158
+ menu=self.find(id)
159
+ if(menu.position>1)
160
+ switch_menu_positions(menu.parent_id, menu.position, menu.position-1)
161
+ end
162
+ end
163
+
164
+ def position_down(id)
165
+ menu=self.find(id)
166
+ if(!siblings(id).empty?)
167
+ if(menu.position< (siblings(id).max {|c1,c2| c1.position <=> c2.position}).position)
168
+ switch_menu_positions(menu.parent_id, menu.position, menu.position+1)
169
+ end
170
+ end
171
+ end
172
+
173
+ def edit(menu_id, new_parent_id, title, link)
174
+ menu=self.find(menu_id)
175
+ menu.update_attributes!(:title=> title,:link=> link)
176
+ if(menu.parent_id != new_parent_id)
177
+ position=assign_position(new_parent_id)
178
+ newmenu=self.new(:title=>title, :link=>link, :parent_id=>new_parent_id, :position=>position)
179
+ newmenu.save!
180
+ children(menu.id).each do |c|
181
+ c.parent_id=newmenu.id
182
+ c.save!
183
+ end
184
+ siblings(menu).each do |s|
185
+ if(s.position>menu.position)
186
+ s.decrement(:position)
187
+ s.save!
188
+ end
189
+ end
190
+ self.delete(menu_id)
191
+ end
192
+ end
193
+
194
+ def delete_item(menu_id)
195
+ menu=self.find(menu_id)
196
+ if(children(menu_id).empty?)
197
+ siblings(menu_id).each do |s|
198
+ if(s.position>menu.position)
199
+ s.decrement(:position)
200
+ s.save!
201
+ end
202
+ end
203
+ else
204
+ siblings(menu.id).each do |s|
205
+ if(s.position>menu.position)
206
+ s.position+=children(menu.id).count - 1
207
+ s.save!
208
+ end
209
+ end
210
+ end
211
+ #actual move
212
+ children(menu.id).each do |c|
213
+ c.position+=menu.position-1
214
+ c.parent_id=menu.parent_id
215
+ c.save!
216
+ end
217
+ self.delete(menu_id)
218
+ end
219
+
220
+ def determine_abs_position_and_depth
221
+ roots=self.find(:all,:order=>"position",:conditions => {:parent_id=>1} )
222
+ ap=0
223
+ depth=1
224
+ roots.each do |r|
225
+ ap+=1
226
+ r.absolute_position=ap
227
+ r.depth=depth
228
+ r.save!
229
+ ap,depth=recursive_dapad(r,ap,depth)
230
+ end
231
+ end
232
+
233
+ def recursive_dapad(rp,ap,depth)
234
+ depth+=1
235
+ children(rp.id).each do |m|
236
+ ap+=1
237
+ m.absolute_position=ap
238
+ m.depth=depth
239
+ m.save!
240
+ ap,depth=recursive_dapad(m,ap,depth)
241
+ end
242
+ depth-=1
243
+ return ap,depth
244
+ end
245
+
246
+ def delete_children(menu_id)
247
+ #need to implement all_children first
248
+ #self.find(menu_id).all_children.each do |c|
249
+ # self.delete(c.id)
250
+ #end
251
+ end
252
+ end
253
+ end
@@ -0,0 +1,5 @@
1
+ #module MegaMenus
2
+ # module ViewHelpers
3
+ #
4
+ #end
5
+
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{mega_menus}
5
+ s.version = "0.8.4"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Marko Toros"]
9
+ s.date = %q{2008-12-08}
10
+ s.description = %q{Adds a model, controller to perform the tasks in order to have a treeview menu. To use this gem simply install it and write script/generate menu name_of_the_menu}
11
+ s.email = %q{mtoros@gmail.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/mega_menus.rb", "lib/mega_menus/editor.rb", "lib/mega_menus/view_helpers.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "init.rb", "Rakefile", "lib/mega_menus.rb", "lib/mega_menus/editor.rb", "lib/mega_menus/view_helpers.rb", "test/test_editor.rb", "rails_generators/menu/USAGE", "rails_generators/menu/menu_generator.rb", "rails_generators/menu/templates/controllers/menu_controller.rb", "rails_generators/menu/templates/views/add_menu_form.rjs", "rails_generators/menu/templates/views/add_menu.rjs", "rails_generators/menu/templates/views/delete_menu.rjs", "rails_generators/menu/templates/views/edit_menu_form.rjs", "rails_generators/menu/templates/views/edit_menu.rjs", "rails_generators/menu/templates/views/up_menu.rjs", "rails_generators/menu/templates/views/down_menu.rjs", "rails_generators/menu/templates/views/publish_menu.rjs", "rails_generators/menu/templates/views/_menu.html.erb", "rails_generators/menu/templates/helpers/menu_helper.rb", "rails_generators/menu/templates/models/create_menus.rb", "rails_generators/menu/templates/models/menu.rb", "mega_menus.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Mega_menus", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{mega_menus}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Treeview menu Gem for Rails}
21
+ s.test_files = ["test/test_editor.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_development_dependency(%q<echoe>, [">= 0"])
29
+ else
30
+ s.add_dependency(%q<echoe>, [">= 0"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<echoe>, [">= 0"])
34
+ end
35
+ end
@@ -0,0 +1 @@
1
+ Write script/generate menu <name_of_menu> in the root folder of your rails application in order to use this generator.
@@ -0,0 +1,26 @@
1
+ class MenuGenerator < Rails::Generator::NamedBase
2
+ def manifest
3
+ record do |m|
4
+ #create the menu controller
5
+ m.template "controllers/menu_controller.rb", "app/controllers/#{file_name}editor_controller.rb"
6
+ #create the views
7
+ m.directory File.join('app/views/', "#{file_name}editor")
8
+ m.template "views/add_menu_form.rjs", "app/views/#{file_name}editor/add_menu_form.rjs"
9
+ m.template "views/add_menu.rjs", "app/views/#{file_name}editor/add_menu.rjs"
10
+ m.template "views/delete_menu.rjs", "app/views/#{file_name}editor/delete_menu.rjs"
11
+ m.template "views/edit_menu_form.rjs", "app/views/#{file_name}editor/edit_menu_form.rjs"
12
+ m.template "views/edit_menu.rjs", "app/views/#{file_name}editor/edit_menu.rjs"
13
+ m.template "views/up_menu.rjs", "app/views/#{file_name}editor/up_menu.rjs"
14
+ m.template "views/down_menu.rjs", "app/views/#{file_name}editor/down_menu.rjs"
15
+ m.template "views/publish_menu.rjs", "app/views/#{file_name}editor/publish_menu.rjs"
16
+ m.template "views/_menu.html.erb", "app/views/#{file_name}editor/_#{file_name}.html.erb"
17
+ #create the helper
18
+ m.template "helpers/menu_helper.rb", "app/helpers/#{file_name}editor_helper.rb"
19
+ #create the models
20
+ m.directory 'db/migrate/'
21
+ m.template "models/create_menus.rb", "db/migrate/#{Time.now.strftime("%Y%m%d%H%M%S")}_create_#{file_name}s.rb"
22
+ m.template "models/menu.rb", "app/models/#{file_name}.rb"
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,49 @@
1
+ class <%= "#{file_name.capitalize+ "editor" + "Controller"}" %> < ApplicationController
2
+
3
+ def add_menu_form
4
+ @menu_id=params[:<%="#{file_name}"%>_id]
5
+ end
6
+
7
+ def add_menu
8
+ <%= file_name.capitalize %>.add_child(params[:<%="#{file_name}"%>_id].to_i, params[:title], params[:link])
9
+ <%= file_name.capitalize %>.determine_abs_position_and_depth
10
+
11
+ end
12
+
13
+ def publish_menu
14
+ m=<%= file_name.capitalize %>.find(params[:<%="#{file_name}"%>_id].to_i)
15
+ if(m.published)
16
+ m.setNotPublished
17
+ else
18
+ m.setPublished
19
+ end
20
+ end
21
+
22
+ def delete_menu
23
+ temp=<%= file_name.capitalize %>.find(params[:<%="#{file_name}"%>_id]).parent_id
24
+
25
+ <%= file_name.capitalize %>.delete_item(params[:<%="#{file_name}"%>_id].to_i)
26
+ <%= file_name.capitalize %>.determine_abs_position_and_depth
27
+ session[:<%="#{file_name}"%>_id]=temp
28
+ params[:<%="#{file_name}"%>_id]=temp
29
+ end
30
+
31
+ def edit_menu_form
32
+ @menu_id=params[:<%="#{file_name}"%>_id]
33
+ end
34
+
35
+ def edit_menu
36
+ <%= file_name.capitalize %>.edit(params[:<%="#{file_name}"%>_id].to_i, params[:parent_id].to_i, params[:title], params[:link])
37
+ <%= file_name.capitalize %>.determine_abs_position_and_depth
38
+ end
39
+
40
+ def up_menu
41
+ <%= file_name.capitalize %>.position_up(params[:<%="#{file_name}"%>_id].to_i)
42
+ <%= file_name.capitalize %>.determine_abs_position_and_depth
43
+ end
44
+
45
+ def down_menu
46
+ <%= file_name.capitalize %>.position_down(params[:<%="#{file_name}"%>_id].to_i)
47
+ <%= file_name.capitalize %>.determine_abs_position_and_depth
48
+ end
49
+ end
@@ -0,0 +1,178 @@
1
+ module <%= "#{file_name.capitalize}editorHelper" %>
2
+ def mega_menus(menu_model, menu_controller, admin_condition, admin_depth, admin_parent)
3
+ #Checks
4
+ if(!menu_checks(menu_model))
5
+ return nil
6
+ end
7
+ #HTML generation
8
+ html_generation(menu_model, menu_controller,admin_condition, admin_depth, admin_parent)
9
+ end
10
+
11
+ def menu_checks(menu_model)
12
+ if(!defined?(menu_model.check_menus))
13
+ concat( <%="'Are you sure you have called acts_as_menu on model #{file_name.capitalize}.'"%>)
14
+ return FALSE
15
+ end
16
+ if(!menu_model.check_menus)
17
+ concat( <%="'You have not set correctly up the model #{file_name.capitalize}.'"%>)
18
+ return FALSE
19
+ end
20
+ return TRUE
21
+ end
22
+
23
+ def html_generation(menu_model, menu_controller,admin_condition, admin_depth, admin_parent)
24
+ allmenus=menu_model.find(:all, :order => "absolute_position")
25
+ #mdp...previous menu depth
26
+ pmd=1
27
+
28
+ if(!params[:<%= "#{file_name}_id" %>].nil?)
29
+ menu_id=params[<%= ":#{file_name}_id" %>]
30
+ elsif(!session[:<%= "#{file_name}_id" %>].nil?)
31
+ menu_id=session[<%= ":#{file_name}_id" %>]
32
+ end
33
+
34
+ firstm=TRUE
35
+ allmenus.each do |m|
36
+ #write the actual menu line for each record
37
+ if(m.published==TRUE or admin_condition==TRUE)
38
+ if(m.id==1 and m.children.empty? and admin_depth.include?(m.depth+1))
39
+ if(admin_condition)
40
+ add_menu_form(menu_model, menu_controller, m.id)
41
+ end
42
+ concat( "<ul id=\"ul_<%= "#{file_name}" %>_#{m.depth+1}\" class=\"ul_<%= "#{file_name}" %>_depth_#{m.depth} ul_<%= "#{file_name}" %>\">")
43
+ if(admin_condition)
44
+ concat( "<li id=\"root_<%= "#{file_name}" %>_add_#{m.id}\" class=\"<%= "#{file_name}" %>_root_add\">")
45
+ concat( link_to_remote( "<span>Add</span>", {:url => {:controller => menu_controller, :action => '<%= "add_menu_form" %>', '<%= "#{file_name}_id" %>' => m.id,:menu_model=>menu_model, :menu_controller=>menu_controller}}, {:class => "<%= "#{file_name}" %>_links_add all_menus_links_add", :title=> "Add",:id => "add_<%= "#{file_name}" %>_link_#{m.id}"}))
46
+ concat( "</li>")
47
+ end
48
+ firstm=FALSE
49
+ end
50
+ if(m.id!=1 and m.isChildOf(admin_parent))
51
+ #check if your depth is correct
52
+ if(admin_depth.include?(m.depth))
53
+ if(m.depth > pmd or firstm)
54
+ if(admin_condition)
55
+ add_menu_form(menu_model, menu_controller, m.parent_id)
56
+ end
57
+ concat( "<ul id=\"ul_<%= "#{file_name}" %>_#{m.depth}\" class=\"ul_<%= "#{file_name}" %>_depth_#{m.depth} ul_<%= "#{file_name}" %>\">")
58
+ if(admin_condition)
59
+ concat( "<li id=\"root_add_#{m.parent_id}\" class=\"root_add\">")
60
+ concat( link_to_remote( "<span>Add</span>", {:url => {:controller => menu_controller, :action => 'add_menu_form', <%= ":#{file_name}_id" %> => m.parent_id,:menu_model=>menu_model, :menu_controller=>menu_controller}}, {:class => "<%= "#{file_name}" %>_links_add all_menus_links_add", :title=> "Add",:id => "add_<%= "#{file_name}" %>_link_#{m.id}"}))
61
+ concat( "</li>")
62
+ end
63
+ firstm=FALSE
64
+ elsif(m.depth < pmd)
65
+ pmd.downto(m.depth+1) {concat( "</ul>")}
66
+ end
67
+ pmd=m.depth
68
+
69
+ #make the selected view appear nicer
70
+ if(!m.class.exists?(menu_id.to_i))
71
+ menu_id=1
72
+ end
73
+ if(m.id==menu_id.to_i || m.isRootOf(menu_id.to_i))
74
+ concat( "<li id=\"li_<%="#{file_name}"%>_#{m.id}\" class=\"<%= "#{file_name}" %>_active\">")
75
+ concat( "<a class=\"<%= "#{file_name}" %>_active\" id =\"<%= "#{file_name}" %>_link_#{m.id}\" href=\"#{m.link}?<%= "#{file_name}" %>_id=#{m.id}\"> #{m.title} </a>")
76
+ session[<%= ":#{file_name}_id" %>]=menu_id
77
+ else
78
+ concat( "<li id=\"li_<%= "#{file_name}" %>_#{m.id}\" class=\"li_<%= "#{file_name}" %>_class\">")
79
+ concat( "<a class=\"<%= "#{file_name}" %>_link\" id =\"<%= "#{file_name}" %>_link_#{m.id}\" href=\"#{m.link}?<%= "#{file_name}" %>_id=#{m.id}\"> #{m.title} </a>")
80
+ end
81
+
82
+ if(admin_condition)
83
+ #remove the comment on the following line depending on the view you want to have
84
+ #concat( link_to_remote( "<span>Add</span>", {:url => {:controller => menu_controller, :action => 'add_menu_form', <%= "#{file_name}_id" %> => m.id,:menu_model=>menu_model, :menu_controller=>menu_controller}}, {:class => "menu_links_add", :title=> "Add",:id => "add_menu_link_#{m.id}"}))
85
+ # DELETE
86
+ concat( link_to_remote( "<span>Delete</span>", {:url => {:controller => menu_controller, :action => 'delete_menu', <%= ":#{file_name}_id" %> => m.id,:menu_model=>menu_model, :menu_controller=>menu_controller}, :confirm => "Are you sure?"}, {:class => "<%= "#{file_name}" %>_links_delete all_menus_links_delete", :title=> "Delete",:id => "delete_<%= "#{file_name}" %>_link_#{m.id}"}))
87
+ # EDIT
88
+ concat( link_to_remote( "<span>Edit</span>", {:url => {:controller => menu_controller, :action => 'edit_menu_form', <%= ":#{file_name}_id" %> => m.id,:menu_model=>menu_model, :menu_controller=>menu_controller}}, {:class => "<%= "#{file_name}" %>_links_edit all_menus_links_edit",:title=> "Edit", :id => "edit_<%= "#{file_name}" %>_link_#{m.id}"}))
89
+ # UP
90
+ concat( link_to_remote( "<span>Up</span>", {:url => {:controller => menu_controller, :action => 'up_menu', <%= ":#{file_name}_id" %> => m.id,:menu_model=>menu_model, :menu_controller=>menu_controller}}, {:class => "<%= "#{file_name}" %>_links_up all_menus_links_up", :title=> "Up",:id => "up_<%= "#{file_name}" %>_link_#{m.id}"}))
91
+ # DOWN
92
+ concat( link_to_remote( "<span>Down</span>", {:url => {:controller => menu_controller, :action => 'down_menu', <%= ":#{file_name}_id" %> => m.id,:menu_model=>menu_model, :menu_controller=>menu_controller}}, {:class => "<%= "#{file_name}" %>_links_down all_menus_links_down", :title=> "Down", :id => "down_<%= "#{file_name}" %>_link_#{m.id}"}))
93
+ # PUBLISH
94
+ if(m.published==TRUE)
95
+ mp="<%= "#{file_name}" %>_links_published all_menus_links_published"
96
+ mpt="published"
97
+ else
98
+ mp="<%= "#{file_name}" %>_links_not_published all_menus_links_not_published"
99
+ mpt="not published"
100
+ end
101
+ concat( link_to_remote( "<span>Publish</span>", {:url => {:controller => menu_controller, :action => 'publish_menu', <%= ":#{file_name}_id" %> => m.id,:menu_model=>menu_model, :menu_controller=>menu_controller}}, {:class => mp, :title=> mpt, :id => "publish_menu_link_#{m.id}"}))
102
+ # EDIT FORM
103
+ edit_menu_form(menu_model, menu_controller,m)
104
+ end
105
+
106
+ concat( "</li>")
107
+ if(admin_condition and m.children.empty? and admin_depth.include?(m.depth+1))
108
+ concat( "<ul id=\"ul_menu_#{m.depth+1}\" class=\"ul_<%= "#{file_name}" %>_depth_#{m.depth+1} ul_<%= "#{file_name}" %>\">")
109
+ concat( "<li id=\"root_<%= "#{file_name}" %>_add_#{m.id}\" class=\"root_<%= "#{file_name}" %>_add\">")
110
+ concat( link_to_remote( "<span>Add</span>", {:url => {:controller => menu_controller, :action => 'add_menu_form', <%= ":#{file_name}_id" %> => m.id,:menu_model=>menu_model, :menu_controller=>menu_controller}}, {:class => "menu_<%= "#{file_name}" %>_add all_menus_links_add", :title=> "Add",:id => "add_<%= "#{file_name}" %>_link_#{m.id}"}))
111
+ add_menu_form(menu_model, menu_controller, m.id)
112
+ concat( "</li>")
113
+ concat( "</ul>")
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ if(firstm==TRUE and <%= "#{file_name.capitalize}" %>.exists?(admin_parent) and admin_parent!=1)
120
+ concat( "<ul id=\"ul_<%= "#{file_name}" %>_#{admin_depth.first}\" class=\"ul_<%= "#{file_name}" %>_depth_#{admin_depth.first} ul_<%= "#{file_name}" %>\">")
121
+ if(admin_condition)
122
+ concat( "<li id=\"root_<%= "#{file_name}" %>_add_#{admin_parent}\" class=\"root_<%= "#{file_name}" %>_add\">")
123
+ concat( link_to_remote( "<span>Add</span>", {:url => {:controller => menu_controller, :action => 'add_menu_form', <%= ":#{file_name}_id" %> => admin_parent,:menu_model=>menu_model, :menu_controller=>menu_controller}}, {:class => "<%= "#{file_name}" %>_links_add all_menus_links_add", :title=> "Add",:id => "add_<%="#{file_name}"%>_link_#{admin_parent}"}))
124
+ add_menu_form(menu_model, menu_controller, admin_parent)
125
+ concat( "</li>")
126
+ end
127
+ pmd=admin_depth.first
128
+ elsif(firstm==TRUE)
129
+ concat( "<ul id=\"ul_<%= "#{file_name}" %>_#{admin_depth.first}\" class=\"ul_<%= "#{file_name}" %>_depth_#{admin_depth.first} ul_menu\">")
130
+ pmd=admin_depth.first
131
+ end
132
+ pmd.downto(admin_depth.first) {concat( "</ul>")}
133
+
134
+ return nil
135
+ end
136
+
137
+ def add_menu_form(menu_model, menu_controller, menu_id)
138
+ form_remote_tag( :url => {:controller=>menu_controller, :action=> 'add_menu'}, :html => {:id => "add_<%= "#{file_name}" %>_form_#{menu_id}", :class => "add_<%= "#{file_name}" %>_form", :style=>"display: none"}) do
139
+ concat( "Title:" )
140
+ concat( text_field_tag( "title", "", :id=>"a_tft_title_#{menu_id}"))
141
+ concat( "Link:")
142
+ concat( text_field_tag("link", "",:id=>"a_tft_link_#{menu_id}"))
143
+ concat( hidden_field_tag(<%= ":#{file_name}_id" %>, menu_id, :id=>"a_tft_<%= "#{file_name}" %>_id_#{menu_id}"))
144
+ #content_tag :button, "Submit", {:type=>"submit", :class=>"button-submit"}
145
+ concat( submit_tag( "Add", :class => "add_button"))
146
+ end
147
+ return nil
148
+ end
149
+
150
+
151
+ def edit_menu_form(menu_model, menu_controller, m)
152
+
153
+ #select only menus that are not submenus!
154
+ cmenus=menu_model.find(:all, :order => "absolute_position")
155
+ cmenus.delete_if{|ame| (ame.id==m.id || ame.isChildOf(m.id))}
156
+ cmenus.each do |cm|
157
+ mtitle=cm.title
158
+ cm.depth.times do
159
+ mtitle="--" + mtitle
160
+ end
161
+ cm.title=mtitle
162
+ end
163
+ form_remote_tag( :url => {:controller=>menu_controller, :action=> 'edit_menu'}, :html => {:id => "edit_<%= "#{file_name}" %>_form_#{m.id}",:class => "edit_<%= "#{file_name}" %>_form", :style=>"display: none"}) do
164
+ concat( "Title:")
165
+ concat( text_field_tag( "title", m.title, :id=>"e_tft_title_#{m.id}" ))
166
+ concat( "Link:")
167
+ concat( text_field_tag( "link", m.link,:id=>"e_tft_link_#{m.id}"))
168
+ concat( "Parent:")
169
+ #concat( text_field_tag("parent_id", m.parent_id, :id=>"e_tft_parent_id_#{m.id}"))
170
+ concat( select_tag( "parent_id", options_from_collection_for_select(cmenus, :id, :title), :id=>"e_tft_parent_id_#{m.id}"))
171
+ concat( hidden_field_tag( <%= ":#{file_name}_id" %>, m.id,:id=>"e_tft_<%= "#{file_name}" %>_id_#{m.id}"))
172
+ #concat content_tag :button, "Submit", {:type=>"submit", :class=>"button-submit"}
173
+ concat( submit_tag( "Edit", :class => "edit__button"))
174
+ end
175
+ return nil
176
+ end
177
+
178
+ end
@@ -0,0 +1,21 @@
1
+ class <%= "#{'Create' + file_name.capitalize + 's'}" %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table <%= ":#{file_name + 's'}" %>,:force => true do |t|
4
+ t.string :title
5
+ t.string :link
6
+ t.integer :parent_id
7
+ t.integer :position
8
+ t.integer :absolute_position
9
+ t.integer :depth
10
+ t.boolean :published
11
+ t.timestamps
12
+ end
13
+
14
+ <%= file_name.capitalize %>.create :id => 1, :position=>0, :absolute_position=>0, :depth=>0, :parent_id=>0,:published=>FALSE, :title=>"ROOT"
15
+ end
16
+
17
+
18
+ def self.down
19
+ drop_table <%= ":#{file_name + 's'}" %>
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ class <%= file_name.capitalize %> < ActiveRecord::Base
2
+ acts_as_menu
3
+ end
@@ -0,0 +1,5 @@
1
+ <%#"The 3. parameter determines normal mode(FALSE) or edit mode(TRUE)...remove this annoying line" %>
2
+ <%# "The 4. parameter determines the depth of the menus to be shown...remove this annoying line" %>
3
+ <%% extend <%= file_name.capitalize %>editorHelper %>
4
+ <%% mega_menus(<%= file_name.capitalize %>, <%= "'#{file_name+ "editor"}'"%>, TRUE, 1..10,1) %>
5
+
@@ -0,0 +1 @@
1
+ page.replace <%="'ul_#{file_name}_1'"%>, :partial => <%= "'#{file_name}editor/#{file_name}'" %>
@@ -0,0 +1,2 @@
1
+ page.toggle "<%="add_#{file_name}"%>_form_#{@menu_id}"
2
+
@@ -0,0 +1 @@
1
+ page.replace <%="'ul_#{file_name}_1'"%>, :partial => <%= "'#{file_name}editor/#{file_name}'" %>
@@ -0,0 +1 @@
1
+ page.replace <%="'ul_#{file_name}_1'"%>, :partial => <%= "'#{file_name}editor/#{file_name}'" %>
@@ -0,0 +1 @@
1
+ page.replace <%="'ul_#{file_name}_1'"%>, :partial => <%= "'#{file_name}editor/#{file_name}'" %>
@@ -0,0 +1 @@
1
+ page.toggle "<%="edit_#{file_name}"%>_form_#{@menu_id}"
@@ -0,0 +1 @@
1
+ page.replace <%="'ul_#{file_name}_1'"%>, :partial => <%= "'#{file_name}editor/#{file_name}'" %>
@@ -0,0 +1 @@
1
+ page.replace <%="'ul_#{file_name}_1'"%>, :partial => <%= "'#{file_name}editor/#{file_name}'" %>
@@ -0,0 +1,50 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ gem 'activerecord'
5
+ require 'active_record'
6
+
7
+ require "#{File.dirname(__FILE__)}/../lib/mega_menus"
8
+ require 'mega_menus'
9
+ MegaMenus.enable_activerecord
10
+
11
+ def setup_db
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :topics do |t|
14
+ t.column :pos, :integer
15
+ t.column :parent_id, :integer
16
+ t.column :created_at, :datetime
17
+ t.column :updated_at, :datetime
18
+ end
19
+ end
20
+ end
21
+
22
+ def teardown_db
23
+ ActiveRecord::Base.connection.tables.each do |table|
24
+ ActiveRecord::Base.connection.drop_table(table)
25
+ end
26
+ end
27
+
28
+ class Topic < ActiveRecord::Base
29
+ acts_as_menu
30
+ end
31
+
32
+ class TestEditor < Test::Unit::TestCase
33
+ def setup
34
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
35
+ setup_db
36
+ (1..5).each { |counter| Topic.create! :pos => counter, :parent_id => counter }
37
+ end
38
+
39
+ def teardown
40
+ teardown_db
41
+ end
42
+
43
+ def test_small
44
+ assert_equal 5, Topic.count
45
+ end
46
+
47
+ def test_other
48
+ end
49
+ #add root, add menu, delete menu, ..., change position,...
50
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mtoros-mega_menus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.4
5
+ platform: ruby
6
+ authors:
7
+ - Marko Toros
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-08 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Adds a model, controller to perform the tasks in order to have a treeview menu. To use this gem simply install it and write script/generate menu name_of_the_menu
25
+ email: mtoros@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.rdoc
32
+ - lib/mega_menus.rb
33
+ - lib/mega_menus/editor.rb
34
+ - lib/mega_menus/view_helpers.rb
35
+ files:
36
+ - Manifest
37
+ - README.rdoc
38
+ - init.rb
39
+ - Rakefile
40
+ - lib/mega_menus.rb
41
+ - lib/mega_menus/editor.rb
42
+ - lib/mega_menus/view_helpers.rb
43
+ - test/test_editor.rb
44
+ - rails_generators/menu/USAGE
45
+ - rails_generators/menu/menu_generator.rb
46
+ - rails_generators/menu/templates/controllers/menu_controller.rb
47
+ - rails_generators/menu/templates/views/add_menu_form.rjs
48
+ - rails_generators/menu/templates/views/add_menu.rjs
49
+ - rails_generators/menu/templates/views/delete_menu.rjs
50
+ - rails_generators/menu/templates/views/edit_menu_form.rjs
51
+ - rails_generators/menu/templates/views/edit_menu.rjs
52
+ - rails_generators/menu/templates/views/up_menu.rjs
53
+ - rails_generators/menu/templates/views/down_menu.rjs
54
+ - rails_generators/menu/templates/views/publish_menu.rjs
55
+ - rails_generators/menu/templates/views/_menu.html.erb
56
+ - rails_generators/menu/templates/helpers/menu_helper.rb
57
+ - rails_generators/menu/templates/models/create_menus.rb
58
+ - rails_generators/menu/templates/models/menu.rb
59
+ - mega_menus.gemspec
60
+ has_rdoc: true
61
+ homepage: ""
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --line-numbers
65
+ - --inline-source
66
+ - --title
67
+ - Mega_menus
68
+ - --main
69
+ - README.rdoc
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "1.2"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project: mega_menus
87
+ rubygems_version: 1.2.0
88
+ signing_key:
89
+ specification_version: 2
90
+ summary: Treeview menu Gem for Rails
91
+ test_files:
92
+ - test/test_editor.rb