2013-02-05 14:16:51 -05:00
|
|
|
class Category < ActiveRecord::Base
|
2013-02-28 13:54:12 -05:00
|
|
|
belongs_to :topic, dependent: :destroy
|
2013-03-08 10:49:25 -05:00
|
|
|
belongs_to :topic_only_relative_url,
|
2013-04-24 00:39:31 -04:00
|
|
|
select: "id, title, slug",
|
2013-03-08 08:34:19 -05:00
|
|
|
class_name: "Topic",
|
|
|
|
foreign_key: "topic_id"
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
has_many :topics
|
2013-02-07 10:45:24 -05:00
|
|
|
has_many :category_featured_topics
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :featured_topics, through: :category_featured_topics, source: :topic
|
|
|
|
|
|
|
|
has_many :category_featured_users
|
|
|
|
has_many :featured_users, through: :category_featured_users, source: :user
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
has_many :category_groups
|
|
|
|
has_many :groups, through: :category_groups
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
validates :user_id, presence: true
|
2013-03-14 17:25:55 -04:00
|
|
|
validates :name, presence: true, uniqueness: true, length: { in: 1..50 }
|
2013-02-05 14:16:51 -05:00
|
|
|
validate :uncategorized_validator
|
|
|
|
|
2013-04-01 12:26:51 -04:00
|
|
|
before_validation :ensure_slug
|
2013-02-05 14:16:51 -05:00
|
|
|
after_save :invalidate_site_cache
|
2013-03-02 03:57:02 -05:00
|
|
|
after_create :create_category_definition
|
2013-04-10 15:53:36 -04:00
|
|
|
after_create :publish_categories_list
|
2013-02-05 14:16:51 -05:00
|
|
|
after_destroy :invalidate_site_cache
|
2013-04-10 15:53:36 -04:00
|
|
|
after_destroy :publish_categories_list
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-22 15:33:33 -04:00
|
|
|
has_one :category_search_data
|
|
|
|
|
2013-03-27 16:17:49 -04:00
|
|
|
scope :latest, ->{ order('topic_count desc') }
|
2013-03-02 03:57:02 -05:00
|
|
|
|
2013-05-15 16:45:52 -04:00
|
|
|
scope :secured, ->(guardian = nil) {
|
2013-05-13 04:04:03 -04:00
|
|
|
ids = guardian.secure_category_ids if guardian
|
|
|
|
if ids.present?
|
2013-05-29 15:46:03 -04:00
|
|
|
where("NOT categories.secure or categories.id in (:cats)", cats: ids)
|
2013-05-13 04:04:03 -04:00
|
|
|
else
|
2013-05-29 15:46:03 -04:00
|
|
|
where("NOT categories.secure")
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
delegate :post_template, to: 'self.class'
|
|
|
|
|
2013-06-05 02:10:26 -04:00
|
|
|
attr_accessor :displayable_topics
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
# Internal: Update category stats: # of topics in past year, month, week for
|
|
|
|
# all categories.
|
|
|
|
def self.update_stats
|
|
|
|
topics = Topic
|
|
|
|
.select("COUNT(*)")
|
|
|
|
.where("topics.category_id = categories.id")
|
|
|
|
.where("categories.topic_id <> topics.id")
|
|
|
|
.visible
|
|
|
|
|
|
|
|
topic_count = topics.to_sql
|
|
|
|
topics_year = topics.created_since(1.year.ago).to_sql
|
|
|
|
topics_month = topics.created_since(1.month.ago).to_sql
|
|
|
|
topics_week = topics.created_since(1.week.ago).to_sql
|
|
|
|
|
|
|
|
Category.update_all("topic_count = (#{topic_count}),
|
|
|
|
topics_year = (#{topics_year}),
|
|
|
|
topics_month = (#{topics_month}),
|
|
|
|
topics_week = (#{topics_week})")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Internal: Generate the text of post prompting to enter category
|
|
|
|
# description.
|
|
|
|
def self.post_template
|
|
|
|
I18n.t("category.post_template", replace_paragraph: I18n.t("category.replace_paragraph"))
|
|
|
|
end
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
def create_category_definition
|
2013-03-14 17:25:55 -04:00
|
|
|
create_topic!(title: I18n.t("category.topic_prefix", category: name), user: user, pinned_at: Time.now)
|
2013-03-02 03:57:02 -05:00
|
|
|
update_column(:topic_id, topic.id)
|
|
|
|
topic.update_column(:category_id, id)
|
|
|
|
topic.posts.create(raw: post_template, user: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_url
|
2013-03-08 16:48:56 -05:00
|
|
|
topic_only_relative_url.try(:relative_url)
|
2013-03-02 03:57:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_slug
|
2013-04-01 12:26:51 -04:00
|
|
|
if name.present?
|
|
|
|
self.slug = Slug.for(name)
|
|
|
|
|
2013-05-30 11:09:09 -04:00
|
|
|
return if self.slug.blank?
|
|
|
|
|
2013-04-01 12:26:51 -04:00
|
|
|
# If a category with that slug already exists, set the slug to nil so the category can be found
|
|
|
|
# another way.
|
|
|
|
category = Category.where(slug: self.slug)
|
|
|
|
category = category.where("id != ?", id) if id.present?
|
|
|
|
self.slug = '' if category.exists?
|
|
|
|
end
|
2013-03-02 03:57:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Categories are cached in the site json, so the caches need to be
|
|
|
|
# invalidated whenever the category changes.
|
|
|
|
def invalidate_site_cache
|
|
|
|
Site.invalidate_cache
|
|
|
|
end
|
2013-02-28 13:54:12 -05:00
|
|
|
|
2013-04-10 15:53:36 -04:00
|
|
|
def publish_categories_list
|
|
|
|
MessageBus.publish('/categories', {categories: ActiveModel::ArraySerializer.new(Category.latest.all).as_json})
|
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
def uncategorized_validator
|
2013-03-02 03:57:02 -05:00
|
|
|
errors.add(:name, I18n.t(:is_reserved)) if name == SiteSetting.uncategorized_name
|
|
|
|
errors.add(:slug, I18n.t(:is_reserved)) if slug == SiteSetting.uncategorized_name
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-10 02:47:47 -04:00
|
|
|
def group_names=(names)
|
|
|
|
# this line bothers me, destroying in AR can not seem to be queued, thinking of extending it
|
|
|
|
category_groups.destroy_all unless new_record?
|
2013-05-17 15:11:37 -04:00
|
|
|
ids = Group.where(name: names.split(",")).pluck(:id)
|
2013-05-10 02:47:47 -04:00
|
|
|
ids.each do |id|
|
|
|
|
category_groups.build(group_id: id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
def deny(group)
|
|
|
|
if group == :all
|
|
|
|
self.secure = true
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
def allow(group)
|
|
|
|
if group == :all
|
|
|
|
self.secure = false
|
2013-05-10 02:47:47 -04:00
|
|
|
# this is kind of annoying, there should be a clean way of queuing this stuff
|
|
|
|
category_groups.destroy_all unless new_record?
|
2013-04-29 02:33:24 -04:00
|
|
|
else
|
|
|
|
groups.push(group)
|
|
|
|
end
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2013-05-29 04:11:04 -04:00
|
|
|
def secure_group_ids
|
|
|
|
if self.secure
|
|
|
|
groups.pluck("groups.id")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: categories
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string(50) not null
|
|
|
|
# color :string(6) default("AB9364"), not null
|
|
|
|
# topic_id :integer
|
|
|
|
# topic_count :integer default(0), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# topics_year :integer
|
|
|
|
# topics_month :integer
|
|
|
|
# topics_week :integer
|
|
|
|
# slug :string(255) not null
|
|
|
|
# description :text
|
|
|
|
# text_color :string(6) default("FFFFFF"), not null
|
|
|
|
# hotness :float default(5.0), not null
|
|
|
|
# secure :boolean default(FALSE), not null
|
|
|
|
# auto_close_days :float
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_categories_on_forum_thread_count (topic_count)
|
|
|
|
# index_categories_on_name (name) UNIQUE
|
|
|
|
#
|
|
|
|
|