Created
June 4, 2010 04:15
-
-
Save rpheath/424940 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
| module RPH | |
| module LinkConverter | |
| def self.included(klass) | |
| klass.extend ClassMethods | |
| end | |
| module ClassMethods | |
| def convert_links_for(*columns) | |
| methods = [] | |
| columns.each do |column| | |
| define_method "converted_link_#{column}" do | |
| html = self[column].to_s | |
| html.gsub!(/\swww\./, ' http://www.').to_s | |
| html.gsub!(/((https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/, '<a href="\1">\1</a>') | |
| self[column] = html | |
| end | |
| methods << "converted_link_#{column}".to_sym | |
| end | |
| before_save *methods | |
| end | |
| end | |
| end | |
| 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
| class Comment < ActiveRecord::Base | |
| include RPH::LinkConverter | |
| convert_links_for :body | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment