Class: SemanticallyTaggable::Scheme

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

Overview

A tagging scheme in which many SemanticallyTaggable::Tag instances exist Holds further information about the scheme - whether it’s polyhierarchical, what meta scheme to use when rendering tags to HTML, and what delimiter to use when parsing

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_name(name) ⇒ Object

CLASS METHODS



14
15
16
17
18
19
20
21
# File 'lib/semantically_taggable/scheme.rb', line 14

def self.by_name(name)
  @@schemes ||= Scheme.all.inject({}) do |schemes, scheme|
    schemes[scheme.name.to_sym] = scheme
    schemes
  end
  @@schemes[name.to_sym] || raise(ActiveRecord::RecordNotFound,
                                  "SemanticallyTaggable::Scheme #{name.to_sym} not found (you will need to seed the schemes table)")
end

Instance Method Details

#create_tag(attributes) ⇒ Object

INSTANCE METHODS



25
26
27
28
29
30
# File 'lib/semantically_taggable/scheme.rb', line 25

def create_tag(attributes)
  Tag.create(attributes) do |tag|
    tag.scheme = self
    yield tag if block_given?
  end
end

#import_skos(skos_filename, &block) ⇒ Object



43
44
45
# File 'lib/semantically_taggable/scheme.rb', line 43

def import_skos(skos_filename, &block)
  SkosImporter.new(skos_filename, self).import(&block)
end

#model_counts_for(*tag_strings) ⇒ Object

Given a list of tag strings, find how many resources are tagged with each and return a hash of the results e.g. { ‘news’ => 93, ‘tax’ => 124 }



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/semantically_taggable/scheme.rb', line 51

def model_counts_for(*tag_strings)
  return [] if tag_strings.empty?
  like_conditions = tag_strings.map { 'tags.name LIKE ?' }.join(' OR ')
  Tag.all(
      :select => 'tags.name, COUNT(DISTINCT taggings.taggable_type, taggings.taggable_id) as tagged_models',
      :joins => self.polyhierarchical ? [
          'LEFT JOIN tag_parentages ON tags.id = tag_parentages.parent_tag_id',
          'INNER JOIN taggings on taggings.tag_id = tag_parentages.child_tag_id'
      ] :
      'INNER JOIN taggings on taggings.tag_id = tags.id',
      :conditions => ["tags.scheme_id = ? AND (#{like_conditions})", self.id, *tag_strings],
      :group => 'tags.name'
  ).inject({}) do |summary_hash, tag|
    summary_hash[tag.name] = tag.tagged_models.to_i
    summary_hash
  end
end

#root_tagObject

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
# File 'lib/semantically_taggable/scheme.rb', line 32

def root_tag
  raise ArgumentError, "No root tags in non-hierarchical schemes" unless polyhierarchical
  Tag.find_by_sql(%{
     SELECT DISTINCT t.* FROM tags t
     INNER JOIN tag_parentages tp on t.id = tp.parent_tag_id AND tp.distance <> 0
     LEFT JOIN tag_parentages children ON tp.parent_tag_id = children.child_tag_id AND children.distance <> 0
     WHERE children.child_tag_id IS NULL
     AND t.scheme_id = #{self.id}
  }).first
end