Class: Tag
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Tag
- Defined in:
- app/models/tag.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#keywords ⇒ Object
Returns the value of attribute keywords.
Class Method Summary collapse
- .collection_to_string(tags) ⇒ Object
- .create_from_article!(article) ⇒ Object
- .find_all_with_content_counters ⇒ Object
- .find_with_char(char) ⇒ Object
Instance Method Summary collapse
- #ensure_naming_conventions ⇒ Object
- #feed_url(format) ⇒ Object
- #permalink ⇒ Object
- #permalink_url(_anchor = nil, only_path = false) ⇒ Object
- #published_contents ⇒ Object
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
13 14 15 |
# File 'app/models/tag.rb', line 13 def description @description end |
#keywords ⇒ Object
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() .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? = [] 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 << article.blog..find_or_create_by(name: tagname) do |tag| tag.display_name = tagword end end end article. = end |
.find_all_with_content_counters ⇒ Object
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_conventions ⇒ Object
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 |
#permalink ⇒ Object
60 61 62 |
# File 'app/models/tag.rb', line 60 def permalink name end |
#permalink_url(_anchor = nil, only_path = false) ⇒ Object
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_contents ⇒ Object
56 57 58 |
# File 'app/models/tag.rb', line 56 def published_contents contents.published end |