class SemanticallyTaggable::Tag

Public Class Methods

find_or_create_all_with_like_by_name(*list) click to toggle source

If the last param is a symbol, it’s taken to be the scheme_name

# File lib/semantically_taggable/tag.rb, line 65
def self.find_or_create_all_with_like_by_name(*list)
  scheme_name = list.pop
  raise ArgumentError, "Last item must be the symbol of the scheme name" unless scheme_name.is_a? Symbol

  scheme = SemanticallyTaggable::Scheme.by_name(scheme_name)
  list = [list].flatten
  return [] if list.empty?

  existing_tags = scheme.tags.named_any(list).all
  if scheme.restrict_to_known_tags
    created_tags = []
  else
    new_tag_names = list.reject do |name|
      name = comparable_name(name)
      existing_tags.any? { |tag| comparable_name(tag.name) == name }
    end
    created_tags = new_tag_names.map { |name| Tag.create(:name => name) { |tag| tag.scheme = scheme } }
  end

  existing_tags + created_tags
end
named(name) click to toggle source
# File lib/semantically_taggable/tag.rb, line 46
def self.named(name)
  where(["name #{like_operator} ?", name])
end
named_any(list) click to toggle source
# File lib/semantically_taggable/tag.rb, line 50
def self.named_any(list)
  where(list.map { |tag_name| sanitize_sql(["tags.name #{like_operator} ?", tag_name.to_s]) }.join(" OR "))
end
named_like(name) click to toggle source
# File lib/semantically_taggable/tag.rb, line 54
def self.named_like(name)
  where(["name #{like_operator} ?", "%#{name}%"])
end
named_like_any(list) click to toggle source
# File lib/semantically_taggable/tag.rb, line 58
def self.named_like_any(list)
  where(list.map { |tag| sanitize_sql(["name #{like_operator} ?", "%#{tag.to_s}%"]) }.join(" OR "))
end
using_postgresql?() click to toggle source

SCOPES:

# File lib/semantically_taggable/tag.rb, line 42
def self.using_postgresql?
  connection.adapter_name == 'PostgreSQL'
end

Private Class Methods

comparable_name(str) click to toggle source
# File lib/semantically_taggable/tag.rb, line 152
def comparable_name(str)
  RUBY_VERSION >= "1.9" ? str.downcase : str.mb_chars.downcase
end
like_operator() click to toggle source
# File lib/semantically_taggable/tag.rb, line 148
def like_operator
  using_postgresql? ? 'ILIKE' : 'LIKE'
end

Public Instance Methods

==(object) click to toggle source
Calls superclass method
# File lib/semantically_taggable/tag.rb, line 127
def ==(object)
  super || (object.is_a?(Tag) && name == object.name && scheme == object.scheme)
end
all_models_total() click to toggle source

Gets a total of all tagged models for this tag

# File lib/semantically_taggable/tag.rb, line 111
def all_models_total
  conditions = "taggings.tag_id = #{self.id}"
  joins = nil

  if scheme.polyhierarchical
    joins = "LEFT JOIN tag_parentages on (taggings.tag_id = tag_parentages.child_tag_id AND tag_parentages.parent_tag_id = #{self.id})"
    conditions << " OR tag_parentages.child_tag_id IS NOT NULL"
  end
  scope =  SemanticallyTaggable::Tagging.all(
    :select => 'COUNT(DISTINCT taggings.taggable_type, taggings.taggable_id) as all_models_count',
    :joins => joins,
    :conditions => conditions
  )
  scope.first.all_models_count.to_i
end
count() click to toggle source
# File lib/semantically_taggable/tag.rb, line 135
def count
  read_attribute(:count).to_i
end
create_synonyms(*synonyms) click to toggle source
# File lib/semantically_taggable/tag.rb, line 139
def create_synonyms(*synonyms)
  synonyms = synonyms.to_a.flatten.uniq
  synonyms.each do |synonym|
    self.synonyms << SemanticallyTaggable::Synonym.create(:name => synonym, :tag => self)
  end
end
model_counts() click to toggle source

Gets a summary of counts of each model for this tag e.g. { ”Article’ => 20, ‘Contact’ => 5 }

# File lib/semantically_taggable/tag.rb, line 90
def model_counts
  joins = nil
  if scheme.polyhierarchical
    joins = 'LEFT JOIN tag_parentages ON taggings.tag_id = tag_parentages.child_tag_id'
    conditions = ['tag_parentages.parent_tag_id = ?', self.id]
  else
    conditions = ['taggings.tag_id = ?', self.id]
  end
  SemanticallyTaggable::Tagging.all(
    :select => 'taggings.taggable_type, COUNT(DISTINCT taggings.taggable_id) as model_count',
    :joins => joins,
    :conditions => conditions,
    :group => 'taggings.taggable_type'
  ).inject({}) do |summary_hash, tagging|
    summary_hash[tagging.taggable_type.to_s] = tagging.model_count.to_i
    summary_hash
  end
end
to_s() click to toggle source
# File lib/semantically_taggable/tag.rb, line 131
def to_s
  name
end