Class: Tag

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document
Defined in:
lib/app/models/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_for_class(klass, opts = {}) ⇒ Object



46
47
48
# File 'lib/app/models/tag.rb', line 46

def self.all_for_class(klass, opts = {})
  all(opts.merge('taggings.taggable_type' => klass.to_s))
end

.like(string, klass = nil) ⇒ Object

takes a string and produces an array of words from the db that are ‘like’ this one great for those oh-so-fancy autocomplete/suggestion text fields



40
41
42
43
44
# File 'lib/app/models/tag.rb', line 40

def self.like(string, klass = nil)
  opts = {:word => /^#{string}/}
  opts['taggings.taggable_type'] = klass.to_s if klass
  all(opts)
end

.most_tagged(klass = nil, opts = {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/app/models/tag.rb', line 50

def self.most_tagged(klass = nil, opts = {})
  order = klass ?  "#{taggings_count_key_for(klass)} desc" : 'taggings_count desc'
  lo = opts.merge(:order => order)
  lo['taggings.taggable_type'] = klass.to_s if klass
  all(lo)
end

.register_taggable_type(type) ⇒ Object



32
33
34
# File 'lib/app/models/tag.rb', line 32

def self.register_taggable_type(type)
  key taggings_count_key_for(type), Integer, :index => true
end

.search(query) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/app/models/tag.rb', line 57

def self.search(query)
  results = []
  all.each do |tag|
    results << tag if tag.word =~ Regexp.new(query)
  end
  results.blank? ? nil : results
end

.top_25(klass = nil) ⇒ Object



65
66
67
# File 'lib/app/models/tag.rb', line 65

def self.top_25(klass = nil)
  most_tagged(klass, :limit => 25)
end

Instance Method Details

#count_for(klass = nil) ⇒ Object



73
74
75
# File 'lib/app/models/tag.rb', line 73

def count_for(klass = nil)
  klass ? send(taggings_count_key_for(klass)) : taggings_count
end

#save_or_destroyObject

Called when removing taggings. If no taggings left, destroy, otherwise save



78
79
80
# File 'lib/app/models/tag.rb', line 78

def save_or_destroy
  taggings.empty? ? destroy : save
end

#to_sObject



69
70
71
# File 'lib/app/models/tag.rb', line 69

def to_s
  word
end