and as I''m now on about day 6 of rails & ruby coding...
looking for input on improvements...
this is very preliminary code...
i did this as much as a learning exercise as anything else...
basic gist:
have controller specific submenu''s...
defined as follows in the controller:
class ShowController < ApplicationController
include ControllerMenu
has_menu :menu => ''shared/submenu'',
:items => [
{ :text => ''All Shows'', :action
=> ''list'' },
{ :text => ''Add Show'', :action =>
''new'' }
]
... controller methods et al...
end
layout template contains:
<%= render_controller_menu %>
shared/_submenu.rhtml example:
<% if item.selected? %>
<b><%= item.render %></b>
<% else %>
<%= item.render %>
<% end %>
<br/>
and the actual meat of it all:
module ControllerMenu
def self.append_features( base )
super
base.extend( ClassMethods )
end
module ClassMethods
def has_menu( options = {} )
menu, items = options[ :menu ], options[ :items ]
module_eval(%Q{
def self.controller_menu
unless @controller_menu
@controller_menu = Controller::Menu.new( "#{menu}",
#{items.inspect} )
end
@controller_menu
end
})
end
end
end
module ApplicationHelper
def render_controller_menu
begin
menu = controller.class.controller_menu
menu.view = self
menu.render
rescue
""
end
end
end
module Controller
class Menu
attr_accessor :template, :items, :view
def initialize( template, item_list )
@items = []
@template = template
item_list.each { |i| @items << MenuItem.new( self, i ) }
end
def render
out = []
@items.each { |i| out << @view.render_partial( @template, nil,
''item'' => i ) }
out.join( "\n" )
end
end
class MenuItem
def initialize( menu, options = {} )
@menu = menu
@text = options[ :text ]
@options = options if options.delete( :text )
end
def render
@menu.view.link_to( @text, @options )
end
def selected?
@options[ :action ] == current_action()
end
private
def current_action
@menu.view.controller.action_name
end
end
end
Any general... that isnt very ruby-ish, there is a better way...
Specific... you could... etc etc
comments welcome...
like i said, i did this as much as a learning exercise as i did for
learning out something (re)usable.
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails