2016-05-04 14:02:47 -04:00
class Tag < ActiveRecord :: Base
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
2016-05-04 14:02:47 -04:00
def self . tags_by_count_query ( opts = { } )
q = TopicTag . joins ( :tag , :topic ) . group ( " topic_tags.tag_id, tags.name " ) . order ( 'count_all DESC' )
q = q . limit ( opts [ :limit ] ) if opts [ :limit ]
q
end
2016-05-31 16:27:13 -04:00
def self . category_tags_by_count_query ( category , opts = { } )
tags_by_count_query ( opts ) . where ( " tags.id in (select tag_id from category_tags where category_id = ?) " , category . id )
. where ( " topics.category_id = ? " , category . id )
end
2016-07-07 21:17:56 +08:00
def self . top_tags ( limit_arg : nil , category : nil )
limit = limit_arg || SiteSetting . max_tags_in_filter_list
2016-07-08 17:13:32 -04:00
tags = DiscourseTagging . filter_allowed_tags ( tags_by_count_query ( limit : limit ) , nil , category : category )
2016-07-07 21:17:56 +08:00
tags . count . map { | name , _ | name }
end
def self . include_tags?
SiteSetting . tagging_enabled && SiteSetting . show_filter_by_tag
2016-05-04 14:02:47 -04:00
end
end
2016-05-30 10:45:32 +10:00
# == Schema Information
#
# Table name: tags
#
# id :integer not null, primary key
# name :string not null
# topic_count :integer default(0), not null
# created_at :datetime
# updated_at :datetime
#
# Indexes
#
# index_tags_on_name (name) UNIQUE
#