Skip to content

Instantly share code, notes, and snippets.

@cannapages
Last active December 16, 2015 01:59
Show Gist options
  • Save cannapages/5358916 to your computer and use it in GitHub Desktop.
Save cannapages/5358916 to your computer and use it in GitHub Desktop.
A simple mention able model
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