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



55
56
57
58
59
# File 'lib/simple_hashtag/hashtag.rb', line 55

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.includes(:hashtaggable).collect { |h| h.hashtaggable }
end

#hashtagged_ids_by_typesObject



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

def hashtagged_ids_by_types
  hashtagged_ids ||= {}
  self.hashtaggings.each do |h|
    hashtagged_ids[h.hashtaggable_type] ||= Array.new
    hashtagged_ids[h.hashtaggable_type] << h.hashtaggable_id
  end
  hashtagged_ids
end

#hashtagged_ids_for_type(type) ⇒ Object



47
48
49
# File 'lib/simple_hashtag/hashtag.rb', line 47

def hashtagged_ids_for_type(type)
  hashtagged_ids_by_types[type]
end

#hashtagged_typesObject



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

def hashtagged_types
  self.hashtaggings.pluck(:hashtaggable_type).uniq
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



51
52
53
# File 'lib/simple_hashtag/hashtag.rb', line 51

def to_s
  name
end