Guest User

Untitled

a guest
Jun 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. def link_to_active_if(condition, link_title, link_path={}, opts={})
  2. if condition
  3. classes = opts.delete(:class) || ""
  4. classes.rstrip!
  5. # checking to see if "active" is already one of the classes assigned, and if NOT, append it to the classes string (sorry, Ruby 1.8 doesn't support negative lookbehind)
  6. unless classes =~ /^(\w*\s+)*active(?!\w)/
  7. if classes.blank?
  8. classes = 'active'
  9. else
  10. classes << " active"
  11. end
  12. end
  13. end
  14. old_opts = opts.dup
  15. link_to_if(condition, link_title, link_path, opts.merge({:class => classes})){
  16. link_to(link_title, link_path, old_opts)
  17. }
  18. end
Add Comment
Please, Sign In to add comment