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



38
39
40
# File 'lib/app/models/tag.rb', line 38

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



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

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



42
43
44
45
46
47
# File 'lib/app/models/tag.rb', line 42

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



24
25
26
# File 'lib/app/models/tag.rb', line 24

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

.top_25(klass = nil) ⇒ Object



49
50
51
# File 'lib/app/models/tag.rb', line 49

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

Instance Method Details

#count_for(klass = nil) ⇒ Object



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

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



58
59
60
# File 'lib/app/models/tag.rb', line 58

def save_or_destroy
  taggings.empty? ? destroy : save
end