Created
May 29, 2015 09:14
-
-
Save schatteleyn/d9228404170fbff44cac to your computer and use it in GitHub Desktop.
hashtag.rb
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 SimpleHashtag | |
class Hashtag < ActiveRecord::Base | |
acts_as_tenant :tenant | |
def self.find_by_name(name, tenant) | |
Hashtag.where("lower(name) =? AND tenant_id=?", name.downcase, tenant.id).first | |
end | |
def self.find_or_create_by_name(name, tenant, &block) | |
find_by_name(name, tenant) || create(name: name, &block) | |
end | |
end | |
module Hashtaggable | |
extend ActiveSupport::Concern | |
included do | |
before_save :update_hashtags_s | |
end | |
def update_hashtags_s | |
self.hashtags = parsed_hashtags | |
end | |
def parsed_hashtags | |
parsed_hashtags = [] | |
array_of_hashtags_as_string = scan_for_hashtags(hashtaggable_content) | |
array_of_hashtags_as_string.each do |s| | |
parsed_hashtags << Hashtag.find_or_create_by_name(s[1]) | |
end | |
parsed_hashtags | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment