Class: Droom::Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/droom/tag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cloud_sizeObject

Clouds

The administrative interface offers a big tag cloud and drag and drop tag-merging. Tag size in the cloud is based on a usage count that is retrieved here in a join with the taggings table. The cloud display logic can be found in the [application_helper](../controllers/application_helper.html).



89
90
91
# File 'app/models/droom/tag.rb', line 89

def cloud_size
  @cloud_size
end

Class Method Details

.for(name, or_create = true) ⇒ Object

Finds or creates a tag with the supplied title



74
75
76
77
78
79
80
# File 'app/models/droom/tag.rb', line 74

def self.for(name, or_create=true)
  if or_create
    where(name: name).first_or_create
  else
    find_by(name: name)
  end
end

.for_cloud(limit = 100) ⇒ Object

self.for_cloud uses that scope to return a list of the most popular tags, weighted for display as a tag cloud and re-sorted into alphabetical order (since to select the most popular we originally had to sort by weighting).



97
98
99
# File 'app/models/droom/tag.rb', line 97

def self.for_cloud(limit=100)
  with_usage_count(limit).sort_by(&:name)
end

.from_list(list = [], or_create = true) ⇒ Object

Keyword lists

Sometimes we want the interface to present a simple comma-separated list. These methods help to move to and from that form.



61
62
63
64
# File 'app/models/droom/tag.rb', line 61

def self.from_list(list=[], or_create=true)
  list = list.split(/[,;]\s*/) if String === list
  list.uniq.map{|t| self.for(t, or_create) }.compact if list && list.any?
end

.to_list(tags = []) ⇒ Object

Renders a set of tags as a comma-separated list.



68
69
70
# File 'app/models/droom/tag.rb', line 68

def self.to_list(tags=[])
  tags.uniq.map(&:name).join(', ')
end

Instance Method Details

#as_json(options = {}) ⇒ Object

Suggestions are returned in a minimal format and need only contain name and (for the public search where there are more possibilities) the type of suggestion.



48
49
50
51
52
53
54
# File 'app/models/droom/tag.rb', line 48

def as_json(options={})
  {
    :id => id,
    :name => name,
    :type => 'tag'
  }
end

#assimilate(tag) ⇒ Object

Admin



109
110
111
112
# File 'app/models/droom/tag.rb', line 109

def assimilate(tag)
  self.taggees << tag.taggees
  tag.destroy
end

#matchingObject

Suggestions

There is a tag-suggesting mechanism in the front end to encourage tag reuse and consistency. It rests on this simple scope, which returns all tags matching a given string fragment.



17
18
19
20
# File 'app/models/droom/tag.rb', line 17

scope :matching, -> fragment {
  fragment = "%#{fragment}%"
  where('tags.name like ?', fragment)
}

#to_sObject

This is here just to make tag interpolation a bit more readable.



103
104
105
# File 'app/models/droom/tag.rb', line 103

def to_s
  name
end