Class: SemanticallyTaggable::Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/semantically_taggable/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_create_all_with_like_by_name(*list) ⇒ Object

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

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



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

def self.named(name)
  where(["name #{like_operator} ?", name])
end

.named_any(list) ⇒ Object



50
51
52
# 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) ⇒ Object



54
55
56
# File 'lib/semantically_taggable/tag.rb', line 54

def self.named_like(name)
  where(["name #{like_operator} ?", "%#{name}%"])
end

.named_like_any(list) ⇒ Object



58
59
60
# 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?Boolean

SCOPES:

Returns:

  • (Boolean)


42
43
44
# File 'lib/semantically_taggable/tag.rb', line 42

def self.using_postgresql?
  connection.adapter_name == 'PostgreSQL'
end

Instance Method Details

#==(object) ⇒ Object



127
128
129
# File 'lib/semantically_taggable/tag.rb', line 127

def ==(object)
  super || (object.is_a?(Tag) && name == object.name && scheme == object.scheme)
end

#all_models_totalObject

Gets a total of all tagged models for this tag



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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

#countObject



135
136
137
# File 'lib/semantically_taggable/tag.rb', line 135

def count
  read_attribute(:count).to_i
end

#create_synonyms(*synonyms) ⇒ Object



139
140
141
142
143
144
# 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_countsObject

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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_sObject



131
132
133
# File 'lib/semantically_taggable/tag.rb', line 131

def to_s
  name
end