Guest User

Untitled

a guest
Dec 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. def active?(*routes)
  2. routes.any? do |route|
  3. n, ca = route.match(/\//) ? route.split('/') : [nil, route]
  4. c, a = ca.match(/\#/) ? ca.split('#').map { |s| s.blank? ? nil : s } : [ca, nil] if ca
  5. (n ? controller.class.to_s.split('::')[0...-1].join('::') == n.camelize : true) &&
  6. (c ? "#{controller_name}_controller".camelize == "#{c.camelize}Controller" : true) &&
  7. (a ? action_name == a : true)
  8. end
  9. end
  10.  
  11. # given these routes in config/routes.rb:
  12. resources :pages, :only => :show
  13. namespace :admin do
  14. resources :pages, :except => :show
  15. end
  16.  
  17. # when on pages controller, show action
  18. active?('pages') # => true
  19. active?('pages#show') # => true
  20. active?('#show') # => true
  21.  
  22. # when in the admin namespace, on the pages controller, edit action
  23. active?('admin/') # => true
  24. active?('admin/pages') # => true
  25. active?('admin/pages#edit') # => true
  26. active?('admin/pages#new', 'admin/pages#edit') # => true
  27. active?('pages') # => true
  28. active?('pages#edit') # => true
  29. active?('#edit') # => true
  30. active?('admin/#edit') # => true
Add Comment
Please, Sign In to add comment