I'm creating an app using twitter bootstrap. I'm using Font Awesome to add icons to various places, very often links. So far I've been using a global helper. Here's the simplified version:
# app/helpers/link_to_with_icon.rb
def link_to_with_icon(text, path, options={})
options = options.clone
icon = options.delete(:icon)
text = "<i class='#{icon}'></i> #{text}" if icon.present?
link_to(text, path, options)
end
This worked, but I had to remember changing link_to to link_to_with_icon every time I needed to add an icon to a new link (the app is new, so that's kindof in constant flux). So I decided to stop using link_to completely, and replaced it with link_to_with_icon (since it is compatible).
But then I realized that since I'm not using link_to any more, I might as well modify link_to in the first place. Surely I could add some monkeypatching on the lib folder to make it understand the :icon option.
# lib/extensions/url_helper_extensions.rb
module ActionView
module Helpers
module UrlHelper
# do some magic here
end
end
end
I've done things similar to this before, a couple years ago. In that time alias_method_chain helper was the right tool for the job. In rails 3.x it seems to be deprecated in favor of modules and inheritance.
But if I'm understanding the examples in that page correctly, I would need the link_to method to be provided by some kind of Base module - otherwise you can't add a "Pre-Extension" to it.
Hence my question: Can I extend link_to using modules? Or must I use alias_method_chain?
In particular, a working implementation of the :icon option would be greatly appreciated.