Created
October 23, 2009 11:47
-
-
Save jamesbebbington/216830 to your computer and use it in GitHub Desktop.
This file contains 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
# Extension to #link_to to automagically accept an ActiveRecord instance. | |
# Accepts an optional <tt>:with_icon => true</tt> which will add AR instance's icon as a background image and set the appropriate css classes. | |
# | |
# ==== Examples: | |
# <tt>link_to(@user) => link_to(@user.to_s, user_path(@user))</tt> | |
# <tt>link_to(@user) { 'User Foo' } => link_to('User Foo', user_path(@user))</tt> | |
def link_to(*args, &block) | |
if args.first.kind_of?(ActiveRecord::Base) | |
record = args.first | |
name = block_given? ? capture(&block) : record.to_s | |
html_options = add_icon_to_html_options_for(record, args.second || {}) | |
url = record.is_a?(User) ? user_path(record) : url_for(record) | |
super(name, url, html_options, &nil) | |
else | |
super(*args, &block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment