Module: Tagalong::Tagger::InstanceMethods

Defined in:
lib/tagalong/tagger.rb

Instance Method Summary collapse

Instance Method Details

#create_tag(tag_name) ⇒ Object



23
24
25
26
27
# File 'lib/tagalong/tagger.rb', line 23

def create_tag(tag_name)
  raise Tagalong::TagAlreadyInUse, "A tag already exists with the name '#{tag_name}'" if tagalong_tags.find_by_name(tag_name).present?
  raise Tagalong::TagCannotBeBlank, "A tag cannot have a blank name" if tag_name.blank?
  TagalongTag.create!(:tagger_id => self.id, :tagger_type => self.class.to_s, :name => tag_name)
end

#delete_tag(tag_name) ⇒ Object



45
46
47
48
49
50
# File 'lib/tagalong/tagger.rb', line 45

def delete_tag(tag_name)
  tag = tagalong_tags.find_by_name(tag_name)
  if tag.present?
    tag.destroy
  end
end

#has_tag?(tag_name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/tagalong/tagger.rb', line 52

def has_tag?(tag_name)
  tags.include?(tag_name)
end

#rename_tag(existing_tag, rename_to) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/tagalong/tagger.rb', line 29

def rename_tag(existing_tag, rename_to)
  if tag = tagalong_tags.find_by_name(existing_tag)
    raise Tagalong::TagAlreadyInUse, "A tag already exists with the name '#{rename_to}'" if tagalong_tags.find_by_name(rename_to).present?
    raise Tagalong::TagCannotBeBlank, "A tag cannot have a blank name" if rename_to.blank?
    tag.update_attribute(:name, rename_to)
  else
    raise Tagalong::TagNotFound, "Tried to rename a tag that does not exist."
  end
end

#tag(taggable_obj, tag_name) ⇒ Object



17
18
19
20
21
# File 'lib/tagalong/tagger.rb', line 17

def tag(taggable_obj, tag_name)
  raise Tagalong::TaggableNotPersisted, "Taggable must be persisted to tag it." if !taggable_obj.persisted?
  tag_manager = Tagalong::TagManager.new(taggable_obj, self)
  tag_manager.add_tag(tag_name)
end

#taggables_with(name) ⇒ Object



104
105
106
107
108
# File 'lib/tagalong/tagger.rb', line 104

def taggables_with(name)
  self.tagalong_tags.where(:name => name).each do |t|
    return t.taggables
  end
end

#tagsObject



56
57
58
# File 'lib/tagalong/tagger.rb', line 56

def tags
  self.tagalong_tags.order("name ASC").map { |r| r.name }
end

#tags_including(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tagalong/tagger.rb', line 60

def tags_including(options={})
  out = []
  query = self.tagalong_tags.order("name ASC")

  if options[:has_been_tagged]
    query = query.
              select("tagalong_tags.id, tagalong_tags.name, tagalong_tags.number_of_references, tagalong_taggings.id as used").
              joins("LEFT OUTER JOIN tagalong_taggings ON tagalong_taggings.tagalong_tag_id = tagalong_tags.id AND tagalong_taggings.taggable_id = '#{options[:has_been_tagged].id.to_s}'")
  end
  
  query.each do |tag|
    hash = {:name => tag.name}
    if options[:number_of_references]
      hash[:number_of_references] = tag.number_of_references
    end
    if options[:has_been_tagged]
      hash[:has_been_tagged] = !tag.used.nil?
    end
    out << hash
  end
  
  return out
end

#tags_matching(search_phrase, limit = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tagalong/tagger.rb', line 84

def tags_matching(search_phrase, limit = nil)
  if Tagalong.sunspot_enabled?
    tmp_tagger_type = self.class.to_s # this is needed because self.class apparently changes inside the Sunspot.search scope.
    tag_search = Sunspot.search(Tagalong::TagalongTag) do
      fulltext "\"#{search_phrase}\""
      with :tagger_id, self.id
      with :tagger_type, tmp_tagger_type
      order_by(:number_of_references, :desc)
      paginate(:page => 1, :per_page => limit) if limit
    end
    tag_search.results.map { |r| { :name => r.name, :number_of_references => r.number_of_references } }
  else
    if limit
      self.tagalong_tags.order("number_of_references DESC").where("name like ?", ["%#{search_phrase}%"]).limit(limit).map { |r| { :name => r.name } }
    else
      self.tagalong_tags.order("number_of_references DESC").where("name like ?", ["%#{search_phrase}%"]).map { |r| { :name => r.name } }
    end
  end
end

#untag(taggable_obj, tag_name) ⇒ Object



39
40
41
42
43
# File 'lib/tagalong/tagger.rb', line 39

def untag(taggable_obj, tag_name)
  raise Tagalong::TaggableNotPersisted, "Taggable must be persisted to untag it." if !taggable_obj.persisted?
  tag_manager = Tagalong::TagManager.new(taggable_obj, self)
  tag_manager.remove_tag(tag_name)
end