Class: SimpleHashtag::Hashtag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/simple_hashtag/hashtag.rb

Constant Summary collapse

HASHTAG_REGEX =

TODO Beef up the regex (ie.:what if content is HTML) this is how Twitter does it: github.com/twitter/twitter-text-rb/blob/master/lib/twitter-text/regex.rb

/(?:\s|^)(#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$))([a-z0-9\-_]+))/i

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean_orphansObject

From DB



38
39
40
41
42
# File 'lib/simple_hashtag/hashtag.rb', line 38

def self.clean_orphans # From DB
  # TODO Make this method call a single SQL query
  orphans = self.all.select { |h| h.hashtaggables.size == 0 }
  orphans.map(&:destroy)
end

.find_by_name(name) ⇒ Object



14
15
16
# File 'lib/simple_hashtag/hashtag.rb', line 14

def self.find_by_name(name)
  Hashtag.where("lower(name) =?", name.downcase).first
end

.find_or_create_by_name(name, &block) ⇒ Object



17
18
19
# File 'lib/simple_hashtag/hashtag.rb', line 17

def self.find_or_create_by_name(name, &block)
  find_by_name(name) || create(name: name, &block)
end

Instance Method Details

#hashtaggablesObject



30
31
32
# File 'lib/simple_hashtag/hashtag.rb', line 30

def hashtaggables
  self.hashtaggings.collect { |h| h.hashtaggable }
end

#nameObject



26
27
28
# File 'lib/simple_hashtag/hashtag.rb', line 26

def name
  read_attribute(:name).downcase
end

#name=(val) ⇒ Object



22
23
24
# File 'lib/simple_hashtag/hashtag.rb', line 22

def name=(val)
  write_attribute(:name, val.downcase)
end

#to_sObject



34
35
36
# File 'lib/simple_hashtag/hashtag.rb', line 34

def to_s
  name
end