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



57
58
59
60
61
# File 'lib/simple_hashtag/hashtag.rb', line 57

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



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

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

.find_or_create_by_name(name, &block) ⇒ Object



19
20
21
# File 'lib/simple_hashtag/hashtag.rb', line 19

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

Instance Method Details

#hashtaggables(conditions = {}) ⇒ Object



32
33
34
# File 'lib/simple_hashtag/hashtag.rb', line 32

def hashtaggables(conditions = {})
  self.hashtaggings.includes(:hashtaggable).where(conditions).collect {|h| h.hashtaggable}
end

#hashtagged_ids_by_typesObject



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

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



49
50
51
# File 'lib/simple_hashtag/hashtag.rb', line 49

def hashtagged_ids_for_type(type)
  hashtagged_ids_by_types[type]
end

#hashtagged_typesObject



36
37
38
# File 'lib/simple_hashtag/hashtag.rb', line 36

def hashtagged_types
  self.hashtaggings.pluck(:hashtaggable_type).uniq
end

#nameObject



28
29
30
# File 'lib/simple_hashtag/hashtag.rb', line 28

def name
  read_attribute(:name).downcase
end

#name=(val) ⇒ Object



24
25
26
# File 'lib/simple_hashtag/hashtag.rb', line 24

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

#to_sObject



53
54
55
# File 'lib/simple_hashtag/hashtag.rb', line 53

def to_s
  name
end