Last active
November 12, 2015 23:44
-
-
Save anlek/66defd01a66b51a013d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nav_link(name, path, options={}) | |
options[:class] = append_class(options[:class], "#{"active" if active_path?(path)}") | |
options[:role] ||= "menuitem" | |
link_to name, path, options | |
end | |
def active_path?(path) | |
request.path == path | |
end | |
# This respects 'cancan' rules which the above cannot. | |
def admin_link(name, class_obj = nil, options={}) | |
class_obj, name = name, name.name.underscore.humanize.pluralize if class_obj.nil? | |
return unless can? :manage, class_obj # for non-admin users, you might want to check if they can? :read, class_obj | |
path = url_for(class_obj) | |
options[:class] = append_class(options[:class], "#{"active" if active_path?(path)}") | |
options[:role] ||= "menuitem" | |
content_tag :li, role:"presentation" do | |
link_to name, path, options | |
end | |
end | |
# Here is how to use admin_link | |
# admin_link(User) | |
# admin_link("User", Member) | |
def extract_class(class_string = "") | |
(class_string || "").split(" ").flatten | |
end | |
def stringify_class(klass=[]) | |
Array(klass).compact.uniq.join(" ") | |
end | |
def append_class(original_class, *fields) | |
klass = extract_class(original_class) | |
klass.concat(fields) | |
stringify_class(klass) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment