2013-10-18 15:09:30 +08:00
|
|
|
require_dependency "concern/positionable"
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class Category < ActiveRecord::Base
|
2013-10-18 15:09:30 +08:00
|
|
|
|
|
|
|
include Concern::Positionable
|
|
|
|
|
2013-03-01 02:54:12 +08:00
|
|
|
belongs_to :topic, dependent: :destroy
|
2013-08-15 23:52:18 +08:00
|
|
|
if rails4?
|
|
|
|
belongs_to :topic_only_relative_url,
|
2013-10-24 00:58:11 +08:00
|
|
|
-> { select "id, title, slug" },
|
|
|
|
class_name: "Topic",
|
|
|
|
foreign_key: "topic_id"
|
2013-08-15 23:52:18 +08:00
|
|
|
else
|
|
|
|
belongs_to :topic_only_relative_url,
|
2013-10-24 00:58:11 +08:00
|
|
|
select: "id, title, slug",
|
|
|
|
class_name: "Topic",
|
|
|
|
foreign_key: "topic_id"
|
2013-08-15 23:52:18 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
belongs_to :user
|
2013-10-17 14:44:56 +08:00
|
|
|
belongs_to :latest_post, class_name: "Post"
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
has_many :topics
|
2013-02-07 23:45:24 +08:00
|
|
|
has_many :category_featured_topics
|
2013-02-06 03:16:51 +08: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 14:33:24 +08:00
|
|
|
has_many :category_groups
|
|
|
|
has_many :groups, through: :category_groups
|
|
|
|
|
2013-03-02 16:57:02 +08:00
|
|
|
validates :user_id, presence: true
|
2013-03-15 05:25:55 +08:00
|
|
|
validates :name, presence: true, uniqueness: true, length: { in: 1..50 }
|
2013-10-24 00:58:11 +08:00
|
|
|
validate :parent_category_validator
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-02 00:26:51 +08:00
|
|
|
before_validation :ensure_slug
|
2013-02-06 03:16:51 +08:00
|
|
|
after_save :invalidate_site_cache
|
2013-07-14 09:24:16 +08:00
|
|
|
before_save :apply_permissions
|
2013-03-02 16:57:02 +08:00
|
|
|
after_create :create_category_definition
|
2013-04-11 03:53:36 +08:00
|
|
|
after_create :publish_categories_list
|
2013-02-06 03:16:51 +08:00
|
|
|
after_destroy :invalidate_site_cache
|
2013-04-11 03:53:36 +08:00
|
|
|
after_destroy :publish_categories_list
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-23 03:33:33 +08:00
|
|
|
has_one :category_search_data
|
2013-10-24 00:58:11 +08:00
|
|
|
belongs_to :parent_category, class_name: 'Category'
|
2013-05-23 03:33:33 +08:00
|
|
|
|
2013-03-28 04:17:49 +08:00
|
|
|
scope :latest, ->{ order('topic_count desc') }
|
2013-03-02 16:57:02 +08:00
|
|
|
|
2013-05-16 04:45:52 +08:00
|
|
|
scope :secured, ->(guardian = nil) {
|
2013-05-13 16:04:03 +08:00
|
|
|
ids = guardian.secure_category_ids if guardian
|
|
|
|
if ids.present?
|
2013-08-26 05:18:11 +08:00
|
|
|
where("NOT categories.read_restricted or categories.id in (:cats)", cats: ids).references(:categories)
|
2013-05-13 16:04:03 +08:00
|
|
|
else
|
2013-08-26 05:18:11 +08:00
|
|
|
where("NOT categories.read_restricted").references(:categories)
|
2013-05-13 16:04:03 +08:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
scope :topic_create_allowed, ->(guardian) {
|
2013-10-13 06:54:48 +08:00
|
|
|
if guardian.anonymous?
|
|
|
|
where("1=0")
|
|
|
|
else
|
|
|
|
scoped_to_permissions(guardian, [:full])
|
|
|
|
end
|
2013-07-14 09:24:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
scope :post_create_allowed, ->(guardian) {
|
2013-10-13 06:54:48 +08:00
|
|
|
if guardian.anonymous?
|
|
|
|
where("1=0")
|
|
|
|
else
|
|
|
|
scoped_to_permissions(guardian, [:create_post, :full])
|
|
|
|
end
|
2013-07-14 09:24:16 +08:00
|
|
|
}
|
2013-03-02 16:57:02 +08:00
|
|
|
delegate :post_template, to: 'self.class'
|
|
|
|
|
2013-07-16 13:44:07 +08:00
|
|
|
# permission is just used by serialization
|
|
|
|
# we may consider wrapping this in another spot
|
2013-11-01 06:02:24 +08:00
|
|
|
attr_accessor :displayable_topics, :permission, :subcategory_ids
|
2013-06-05 14:10:26 +08:00
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
|
|
|
|
def self.scoped_to_permissions(guardian, permission_types)
|
|
|
|
if guardian && guardian.is_staff?
|
2013-08-26 05:18:11 +08:00
|
|
|
rails4? ? all : scoped
|
2013-07-14 09:24:16 +08:00
|
|
|
else
|
|
|
|
permission_types = permission_types.map{ |permission_type|
|
|
|
|
CategoryGroup.permission_types[permission_type]
|
|
|
|
}
|
|
|
|
where("categories.id in (
|
|
|
|
SELECT c.id FROM categories c
|
|
|
|
WHERE (
|
|
|
|
NOT c.read_restricted AND
|
2013-07-16 13:44:07 +08:00
|
|
|
(
|
|
|
|
NOT EXISTS(
|
|
|
|
SELECT 1 FROM category_groups cg WHERE cg.category_id = categories.id )
|
|
|
|
) OR EXISTS(
|
|
|
|
SELECT 1 FROM category_groups cg
|
|
|
|
WHERE permission_type in (?) AND
|
|
|
|
cg.category_id = categories.id AND
|
|
|
|
group_id IN (
|
|
|
|
SELECT g.group_id FROM group_users g where g.user_id = ? UNION SELECT ?
|
|
|
|
)
|
|
|
|
)
|
2013-07-14 09:24:16 +08:00
|
|
|
)
|
2013-07-16 13:44:07 +08:00
|
|
|
)", permission_types,(!guardian || guardian.user.blank?) ? -1 : guardian.user.id, Group[:everyone].id)
|
2013-07-14 09:24:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
# Internal: Update category stats: # of topics in past year, month, week for
|
|
|
|
# all categories.
|
|
|
|
def self.update_stats
|
|
|
|
topics = Topic
|
2013-10-17 10:23:38 +08:00
|
|
|
.select("COUNT(*) topic_count")
|
2013-04-29 14:33:24 +08:00
|
|
|
.where("topics.category_id = categories.id")
|
|
|
|
.where("categories.topic_id <> topics.id")
|
|
|
|
.visible
|
|
|
|
|
2013-10-17 10:23:38 +08:00
|
|
|
topics_with_post_count = Topic
|
2013-10-21 07:20:16 +08:00
|
|
|
.select("topics.category_id, COUNT(*) topic_count, SUM(topics.posts_count) post_count")
|
2013-10-24 07:05:51 +08:00
|
|
|
.where("topics.id NOT IN (select cc.topic_id from categories cc WHERE topic_id IS NOT NULL)")
|
2013-10-21 07:20:16 +08:00
|
|
|
.group("topics.category_id")
|
2013-10-17 10:23:38 +08:00
|
|
|
.visible.to_sql
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
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-10-17 10:23:38 +08:00
|
|
|
|
|
|
|
Category.exec_sql <<SQL
|
|
|
|
UPDATE categories c
|
|
|
|
SET topic_count = x.topic_count,
|
|
|
|
post_count = x.post_count
|
|
|
|
FROM (#{topics_with_post_count}) x
|
|
|
|
WHERE x.category_id = c.id AND
|
2013-10-21 07:20:16 +08:00
|
|
|
(c.topic_count <> x.topic_count OR c.post_count <> x.post_count)
|
2013-10-17 10:23:38 +08:00
|
|
|
|
|
|
|
SQL
|
|
|
|
|
|
|
|
|
|
|
|
# TODO don't update unchanged data
|
|
|
|
Category.update_all("topics_year = (#{topics_year}),
|
2013-04-29 14:33:24 +08:00
|
|
|
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 16:57:02 +08:00
|
|
|
def create_category_definition
|
2013-10-24 07:05:51 +08:00
|
|
|
t = Topic.new(title: I18n.t("category.topic_prefix", category: name), user: user, pinned_at: Time.now, category_id: id)
|
|
|
|
t.skip_callbacks = true
|
2013-12-07 05:39:35 +08:00
|
|
|
t.auto_close_hours = nil
|
2013-10-24 07:05:51 +08:00
|
|
|
t.save!
|
|
|
|
update_column(:topic_id, t.id)
|
|
|
|
t.posts.create(raw: post_template, user: user)
|
2013-03-02 16:57:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def topic_url
|
2013-03-09 05:48:56 +08:00
|
|
|
topic_only_relative_url.try(:relative_url)
|
2013-03-02 16:57:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_slug
|
2013-04-02 00:26:51 +08:00
|
|
|
if name.present?
|
2013-07-31 04:48:19 +08:00
|
|
|
self.name.strip!
|
2013-04-02 00:26:51 +08:00
|
|
|
self.slug = Slug.for(name)
|
|
|
|
|
2013-05-30 23:09:09 +08:00
|
|
|
return if self.slug.blank?
|
|
|
|
|
2013-04-02 00:26:51 +08: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 16:57:02 +08: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-03-01 02:54:12 +08:00
|
|
|
|
2013-04-11 03:53:36 +08:00
|
|
|
def publish_categories_list
|
2013-07-23 02:44:11 +08:00
|
|
|
MessageBus.publish('/categories', {categories: ActiveModel::ArraySerializer.new(Category.latest).as_json})
|
2013-04-11 03:53:36 +08:00
|
|
|
end
|
|
|
|
|
2013-10-24 00:58:11 +08:00
|
|
|
def parent_category_validator
|
|
|
|
if parent_category_id
|
2013-10-25 05:03:28 +08:00
|
|
|
errors.add(:parent_category_id, I18n.t("category.errors.self_parent")) if parent_category_id == id
|
2013-10-24 00:58:11 +08:00
|
|
|
|
|
|
|
grandfather_id = Category.where(id: parent_category_id).pluck(:parent_category_id).first
|
2013-10-25 05:03:28 +08:00
|
|
|
errors.add(:base, I18n.t("category.errors.depth")) if grandfather_id
|
2013-10-24 00:58:11 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-10 14:47:47 +08: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-18 03:11:37 +08:00
|
|
|
ids = Group.where(name: names.split(",")).pluck(:id)
|
2013-05-10 14:47:47 +08:00
|
|
|
ids.each do |id|
|
|
|
|
category_groups.build(group_id: id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
# will reset permission on a topic to a particular
|
|
|
|
# set.
|
|
|
|
#
|
|
|
|
# Available permissions are, :full, :create_post, :readonly
|
|
|
|
# hash can be:
|
|
|
|
#
|
|
|
|
# :everyone => :full - everyone has everything
|
|
|
|
# :everyone => :readonly, :staff => :full
|
|
|
|
# 7 => 1 # you can pass a group_id and permission id
|
|
|
|
def set_permissions(permissions)
|
|
|
|
self.read_restricted, @permissions = Category.resolve_permissions(permissions)
|
|
|
|
|
|
|
|
# Ideally we can just call .clear here, but it runs SQL, we only want to run it
|
|
|
|
# on save.
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-07-16 13:44:07 +08:00
|
|
|
def permissions=(permissions)
|
|
|
|
set_permissions(permissions)
|
|
|
|
end
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
def apply_permissions
|
|
|
|
if @permissions
|
|
|
|
category_groups.destroy_all
|
|
|
|
@permissions.each do |group_id, permission_type|
|
|
|
|
category_groups.build(group_id: group_id, permission_type: permission_type)
|
|
|
|
end
|
|
|
|
@permissions = nil
|
2013-04-29 14:33:24 +08:00
|
|
|
end
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-05-29 16:11:04 +08:00
|
|
|
def secure_group_ids
|
2013-07-14 09:24:16 +08:00
|
|
|
if self.read_restricted?
|
2013-05-29 16:11:04 +08:00
|
|
|
groups.pluck("groups.id")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-17 14:44:56 +08:00
|
|
|
def update_latest
|
|
|
|
latest_post_id = Post
|
|
|
|
.order("posts.created_at desc")
|
|
|
|
.where("NOT hidden")
|
|
|
|
.joins("join topics on topics.id = topic_id")
|
|
|
|
.where("topics.category_id = :id", id: self.id)
|
|
|
|
.limit(1)
|
|
|
|
.pluck("posts.id")
|
|
|
|
.first
|
|
|
|
|
|
|
|
latest_topic_id = Topic
|
|
|
|
.order("topics.created_at desc")
|
|
|
|
.where("visible")
|
|
|
|
.where("topics.category_id = :id", id: self.id)
|
|
|
|
.limit(1)
|
|
|
|
.pluck("topics.id")
|
|
|
|
.first
|
|
|
|
|
|
|
|
self.update_attributes(latest_topic_id: latest_topic_id, latest_post_id: latest_post_id)
|
|
|
|
end
|
2013-07-14 09:24:16 +08:00
|
|
|
|
|
|
|
def self.resolve_permissions(permissions)
|
|
|
|
read_restricted = true
|
|
|
|
|
|
|
|
everyone = Group::AUTO_GROUPS[:everyone]
|
|
|
|
full = CategoryGroup.permission_types[:full]
|
|
|
|
|
|
|
|
mapped = permissions.map do |group,permission|
|
|
|
|
group = group.id if Group === group
|
|
|
|
|
|
|
|
# subtle, using Group[] ensures the group exists in the DB
|
2013-07-16 13:44:07 +08:00
|
|
|
group = Group[group.to_sym].id unless Fixnum === group
|
2013-07-14 09:24:16 +08:00
|
|
|
permission = CategoryGroup.permission_types[permission] unless Fixnum === permission
|
|
|
|
|
|
|
|
[group, permission]
|
|
|
|
end
|
|
|
|
|
|
|
|
mapped.each do |group, permission|
|
|
|
|
if group == everyone && permission == full
|
|
|
|
return [false, []]
|
|
|
|
end
|
|
|
|
|
|
|
|
read_restricted = false if group == everyone
|
|
|
|
end
|
|
|
|
|
|
|
|
[read_restricted, mapped]
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: categories
|
|
|
|
#
|
2013-12-05 14:40:35 +08:00
|
|
|
# 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
|
|
|
|
# read_restricted :boolean default(FALSE), not null
|
2013-12-07 05:39:35 +08:00
|
|
|
# auto_close_hours :float
|
2013-12-05 14:40:35 +08:00
|
|
|
# post_count :integer default(0), not null
|
|
|
|
# latest_post_id :integer
|
|
|
|
# latest_topic_id :integer
|
|
|
|
# position :integer not null
|
|
|
|
# parent_category_id :integer
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_categories_on_forum_thread_count (topic_count)
|
|
|
|
# index_categories_on_name (name) UNIQUE
|
|
|
|
#
|