Created
August 31, 2012 21:46
-
-
Save BaylorRae/3559583 to your computer and use it in GitHub Desktop.
Source code for: http://baylorrae.com/rails-link_to_with_notification-helper-method/
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 link_to_with_notifications(*args, &block) | |
# get notification count from hash | |
notifications = args[2][:notifications] || 0 | |
# create the data- attribute unless notifications.zero? | |
args[2]['data-notifications'] = notifications unless notifications.zero? | |
# delete original notifications hash | |
args[2].delete(:notifications) | |
# run original link_to helper | |
link_to(*args, &block) | |
end |
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
<%= link_to_with_notifications 'Completed Orders', | |
user_carts_path(user, view: 'completed'), | |
class: 'button', | |
notifications: user.shopping_carts.where('completed = ?', true).count %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment