2016-05-04 14:02:47 -04:00
|
|
|
class Tag < ActiveRecord::Base
|
2017-08-25 11:52:18 -04:00
|
|
|
include Searchable
|
|
|
|
|
2016-05-04 14:02:47 -04:00
|
|
|
validates :name, presence: true, uniqueness: true
|
2016-05-30 16:37:06 -04:00
|
|
|
|
|
|
|
has_many :tag_users # notification settings
|
|
|
|
|
2016-05-04 14:02:47 -04:00
|
|
|
has_many :topic_tags, dependent: :destroy
|
|
|
|
has_many :topics, through: :topic_tags
|
2016-05-30 16:37:06 -04:00
|
|
|
|
|
|
|
has_many :category_tags, dependent: :destroy
|
|
|
|
has_many :categories, through: :category_tags
|
2016-05-04 14:02:47 -04:00
|
|
|
|
2016-06-06 14:18:15 -04:00
|
|
|
has_many :tag_group_memberships
|
|
|
|
has_many :tag_groups, through: :tag_group_memberships
|
|
|
|
|
2017-08-25 11:52:18 -04:00
|
|
|
after_save :index_search
|
|
|
|
|
2018-01-11 11:59:51 -05:00
|
|
|
def self.ensure_consistency!
|
|
|
|
update_topic_counts # topic_count counter cache can miscount
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.update_topic_counts
|
|
|
|
Category.exec_sql <<~SQL
|
|
|
|
UPDATE tags t
|
|
|
|
SET topic_count = x.topic_count
|
|
|
|
FROM (
|
|
|
|
SELECT COUNT(topics.id) AS topic_count, tags.id AS tag_id
|
|
|
|
FROM tags
|
|
|
|
LEFT JOIN topic_tags ON tags.id = topic_tags.tag_id
|
2018-02-22 09:57:02 -05:00
|
|
|
LEFT JOIN topics ON topics.id = topic_tags.topic_id AND topics.deleted_at IS NULL AND topics.archetype != 'private_message'
|
2018-01-11 11:59:51 -05:00
|
|
|
GROUP BY tags.id
|
|
|
|
) x
|
|
|
|
WHERE x.tag_id = t.id
|
|
|
|
AND x.topic_count <> t.topic_count
|
|
|
|
SQL
|
2016-05-31 16:27:13 -04:00
|
|
|
end
|
|
|
|
|
2016-09-22 15:23:10 -04:00
|
|
|
def self.top_tags(limit_arg: nil, category: nil, guardian: nil)
|
2016-07-07 09:17:56 -04:00
|
|
|
limit = limit_arg || SiteSetting.max_tags_in_filter_list
|
2017-07-27 21:20:09 -04:00
|
|
|
scope_category_ids = (guardian || Guardian.new).allowed_category_ids
|
2016-07-07 09:17:56 -04:00
|
|
|
|
2016-09-22 15:23:10 -04:00
|
|
|
if category
|
|
|
|
scope_category_ids &= ([category.id] + category.subcategories.pluck(:id))
|
|
|
|
end
|
|
|
|
|
2018-02-08 16:36:39 -05:00
|
|
|
return [] if scope_category_ids.empty?
|
|
|
|
|
|
|
|
tag_names_with_counts = Tag.exec_sql <<~SQL
|
|
|
|
SELECT tags.name as tag_name, SUM(stats.topic_count) AS sum_topic_count
|
|
|
|
FROM category_tag_stats stats
|
|
|
|
INNER JOIN tags ON stats.tag_id = tags.id AND stats.topic_count > 0
|
|
|
|
WHERE stats.category_id in (#{scope_category_ids.join(',')})
|
|
|
|
GROUP BY tags.name
|
|
|
|
ORDER BY sum_topic_count DESC, tag_name ASC
|
|
|
|
LIMIT #{limit}
|
|
|
|
SQL
|
2016-07-07 09:17:56 -04:00
|
|
|
|
2018-02-08 16:36:39 -05:00
|
|
|
tag_names_with_counts.map { |row| row['tag_name'] }
|
2016-07-07 09:17:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.include_tags?
|
|
|
|
SiteSetting.tagging_enabled && SiteSetting.show_filter_by_tag
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
2016-08-01 22:54:32 -04:00
|
|
|
|
|
|
|
def full_url
|
|
|
|
"#{Discourse.base_url}/tags/#{self.name}"
|
|
|
|
end
|
2017-08-25 11:52:18 -04:00
|
|
|
|
|
|
|
def index_search
|
|
|
|
SearchIndexer.index(self)
|
|
|
|
end
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
2016-05-29 20:45:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: tags
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string not null
|
|
|
|
# topic_count :integer default(0), not null
|
2018-02-20 01:28:58 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2016-05-29 20:45:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_tags_on_name (name) UNIQUE
|
|
|
|
#
|