Class: Tag

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/tag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



13
14
15
# File 'app/models/tag.rb', line 13

def description
  @description
end

#keywordsObject

Returns the value of attribute keywords.



13
14
15
# File 'app/models/tag.rb', line 13

def keywords
  @keywords
end

Class Method Details

.collection_to_string(tags) ⇒ Object



51
52
53
54
# File 'app/models/tag.rb', line 51

def self.collection_to_string(tags)
  tags.map(&:display_name).sort.
    map { |name| / /.match?(name) ? "\"#{name}\"" : name }.join ", "
end

.create_from_article!(article) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/tag.rb', line 15

def self.create_from_article!(article)
  return if article.keywords.nil?

  tags = []
  Tag.transaction do
    tagwords = article.keywords.to_s.scan(/((['"]).*?\2|[.:[[:alnum:]]]+)/).map do |x|
      x.first.tr("\"'", "")
    end
    tagwords.uniq.each do |tagword|
      tagname = tagword.to_url
      tags << article.blog.tags.find_or_create_by(name: tagname) do |tag|
        tag.display_name = tagword
      end
    end
  end
  article.tags = tags
  tags
end

.find_all_with_content_countersObject



39
40
41
42
43
44
45
# File 'app/models/tag.rb', line 39

def self.find_all_with_content_counters
  Tag.joins(:contents).
    where(contents: { state: "published" }).
    select(*Tag.column_names, "COUNT(contents_tags.content_id) as content_counter").
    group(*Tag.column_names).
    order("content_counter DESC").limit(1000)
end

.find_with_char(char) ⇒ Object



47
48
49
# File 'app/models/tag.rb', line 47

def self.find_with_char(char)
  where("name LIKE ? ", "%#{char}%").order("name ASC")
end

Instance Method Details

#ensure_naming_conventionsObject



34
35
36
37
# File 'app/models/tag.rb', line 34

def ensure_naming_conventions
  self.display_name = name if display_name.blank?
  self.name = display_name.to_url if display_name.present?
end

#feed_url(format) ⇒ Object



64
65
66
# File 'app/models/tag.rb', line 64

def feed_url(format)
  "#{permalink_url}.#{format.gsub(/\d/, "")}"
end


60
61
62
# File 'app/models/tag.rb', line 60

def permalink
  name
end


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

def permalink_url(_anchor = nil, only_path = false)
  blog.url_for(controller: "tags", action: "show", id: permalink, only_path: only_path)
end

#published_contentsObject



56
57
58
# File 'app/models/tag.rb', line 56

def published_contents
  contents.published
end