Last active
December 16, 2015 01:59
-
-
Save cannapages/5358916 to your computer and use it in GitHub Desktop.
A simple mention able model
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
class Post < ActiveRecord::Base | |
.. | |
def content_for_view | |
content.html_safe | |
end | |
before_save :create_mentionable_links_in_post | |
def create_mentionable_links_in_post | |
reg = %r[@\S+]i | |
tmp_content = content | |
tmp_content.scan( reg ).each do |mention| | |
user = User.find_by_user_name( mention.gsub("@","") ) | |
if user | |
tmp_content.gsub( mention, mention_tag( user ) ) | |
end | |
end | |
self.content = tmp_content | |
end | |
private | |
def mention_tag( user ) | |
"<a href='/users/#{user.id}'>@#{user.user_name}</a>" | |
end | |
.. | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment