On Aug 1, 2006, at 2:53 PM, Rabbit wrote:
Here’s what I’ve got for a first-run:
http://rafb.net/paste/results/myCYgs62.html
For some reason, it feels very PHP-ish. Any thoughts?
First thought: bottom post 
The first things we can try to get rid of is that “tabs” variable.
Sadly, the pattern isn’t exactly consistent so we have to get
tricky. But we do have a little bit of patterning going on in
everything except calendar.
You can factor that out (at it’s simplest idea) by adding a method
like so
[‘Calendar’, ‘Schedules’, ‘Blackouts’, ‘Ticket Templates’, ‘Venue
Layout’].each do |tab|
if current_page?(location_for_name(tab))
…link to…
else
…link to…
end
end
location_for_name would turn ‘Ticket Templates’ into
‘ticket_templates’ by string manipulation (there may already be a
method somewhere for this) and have a special case for Calendar.
Finally, I dislike html in methods, so you might want to consider
embedding this logic into a layout partial with some imbedded code.
The final function might be something like:
def navigation
@tabs = ‘Calendar’, ‘Schedules’, ‘Blackouts’, ‘Ticket Templates’,
‘Venue Layout’]
render :template => ‘layouts/navigation’
end
def location_for_name tab_name
if tab_name == ‘Calendar’
{ :controller => ‘tickets’, :action => ‘calendar’ }
else
{:controller => tab_name.gsub(’ ', ‘_’).downcase, :action => ‘index’ }
end
end
def link_for_name tab_name
if current_page?(location_for_name(tab))
link_to tab_name, location_for_name(tab_name), {:class => “active”}
else
link_to tab_name, location_for_name(tab_name), {:class => “active”}
end
end
with navigation.rhtml being
<% for tab in @tabs do %>
- <%= link_for_name tab %>
<% end %>
The moral of the story is: Ruby makes introducing new functions easy
and quick. So do it.
To make this even cooler and more ruby-ish, we could make all the
controllers subclasses of some parent that had an inherited()
function defined and kept track of it’s children. Then instead of
loose functions, we can call .as_link or .title or .location on the
controller classes themselves. Doing this would yield a
naviation.rhtml like:
<% for controller in MainController.children do %>
<%= controller.to_link %>
<% end %>
MainController then contains the munging functions and the Tickets
controller (the oddball) overrides .name and .location with it’s own
idea of what a tab should be.
Wow… that was a lot. Sorry if I’m confusing you.
-Mat