don't understand few lines ruby codes
I'm working on a ruby on rails project which is developed by someone else rails expert. I don't know ruby well. So while i modifying the existing project, i could not fix a bug because i didnt understand few lines code. It will be great if someone explain. H开发者_运维问答ere are the codes -
on my home controller - home_controller.rb
class HomeController < ApplicationController
menu_default :overview
menu_specific :contact, :contact
on my application controller - application.rb
# report the current menu to the application helper, when forming
# tabs
def current_menu
# work out the action of the current request
action = request.path_parameters['action']
# set the default
menu_id = self.class.menu_structure[:default]
# any specific ?
menu_id = self.class.menu_structure[:specifics][action] unless self.class.menu_structure[:specifics].nil? or self.class.menu_structure[:specifics][action].nil?
menu_id
end
def self.menu_default menu_id
# default the menu
@@menu ||= {}
# work out the controller this relates to
self.menu_structure[:default] = menu_id
end
def self.menu_specific menu_id, actions
# turn the actions into an array
actions = [actions] unless actions.is_a?(Array)
# enumerate actions and setup
actions.each do |action|
self.menu_structure[:specifics] ||= {}
self.menu_structure[:specifics][action.to_s] = menu_id
end
end
def self.menu_structure
controller = self.to_s
@@menu ||= {}
end
on my application helper - application_helper.rb
# page tab helper
def tab menu_id, title, location
# ask the application controller which is the current location
# form the link with the appropriate class
link = link_to title, location
if( menu_id == controller.current_menu )
content_tag("div", link, :class=>"menu_selected" )
else
content_tag("div", link, :class=>"menu_open" )
end
end
on my layout - main.haml
= tab :overview, "Overview", overview_url
I stuck for days. Please help me out. Thanks
Rails can seem somewhat impenetrable without a good reference book. While there are many on the market, I found the Pragmatic Bookshelf to produce some of the best Ruby and Rails specific ones (http://www.pragprog.com/titles).
While Ruby is fairly straightforward to understand, Rails can take longer to fully absorb because there are a number of conventions that may be unfamiliar. Depending on your background, you may not have had much experience with a MVC-type design, or with object oriented programming in general, so those aspects can be a bit bewildering at first.
精彩评论