Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def active?(*routes)
- routes.any? do |route|
- n, ca = route.match(/\//) ? route.split('/') : [nil, route]
- c, a = ca.match(/\#/) ? ca.split('#').map { |s| s.blank? ? nil : s } : [ca, nil] if ca
- (n ? controller.class.to_s.split('::')[0...-1].join('::') == n.camelize : true) &&
- (c ? "#{controller_name}_controller".camelize == "#{c.camelize}Controller" : true) &&
- (a ? action_name == a : true)
- end
- end
- # given these routes in config/routes.rb:
- resources :pages, :only => :show
- namespace :admin do
- resources :pages, :except => :show
- end
- # when on pages controller, show action
- active?('pages') # => true
- active?('pages#show') # => true
- active?('#show') # => true
- # when in the admin namespace, on the pages controller, edit action
- active?('admin/') # => true
- active?('admin/pages') # => true
- active?('admin/pages#edit') # => true
- active?('admin/pages#new', 'admin/pages#edit') # => true
- active?('pages') # => true
- active?('pages#edit') # => true
- active?('#edit') # => true
- active?('admin/#edit') # => true
Add Comment
Please, Sign In to add comment