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-03-08 08:34:19 -05:00
|
|
|
select: "id, title",
|
|
|
|
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-03-02 03:57:02 -05:00
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :name, presence: true, uniqueness: true
|
2013-02-05 14:16:51 -05:00
|
|
|
validate :uncategorized_validator
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
before_save :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-02-05 14:16:51 -05:00
|
|
|
after_destroy :invalidate_site_cache
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
scope :popular, ->{ order('topic_count desc') }
|
|
|
|
|
|
|
|
delegate :post_template, to: 'self.class'
|
|
|
|
|
|
|
|
def create_category_definition
|
2013-03-08 10:49:25 -05: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
|
|
|
|
topic.try(:relative_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_slug
|
|
|
|
self.slug = Slug.for(name)
|
|
|
|
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-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-03-02 03:57:02 -05:00
|
|
|
# Internal: Update category stats: # of topics in past year, month, week for
|
|
|
|
# all categories.
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.update_stats
|
2013-02-16 15:57:16 -05:00
|
|
|
topics = Topic
|
|
|
|
.select("COUNT(*)")
|
|
|
|
.where("topics.category_id = categories.id")
|
2013-03-07 12:45:49 -05:00
|
|
|
.where("categories.topic_id <> topics.id")
|
2013-02-16 15:57:16 -05:00
|
|
|
.visible
|
|
|
|
|
|
|
|
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
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
Category.update_all("topics_year = (#{topics_year}),
|
|
|
|
topics_month = (#{topics_month}),
|
2013-02-16 15:57:16 -05:00
|
|
|
topics_week = (#{topics_week})")
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
# Internal: Generate the text of post prompting to enter category
|
|
|
|
# description.
|
2013-02-21 18:09:56 -05:00
|
|
|
def self.post_template
|
|
|
|
I18n.t("category.post_template", replace_paragraph: I18n.t("category.replace_paragraph"))
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|