UX: Improve category filtering and include subcategories

* category_filtering
  1. report_top_referred_topics
  2. report_top_traffic_sources
  3. report_post_edit
* category_filtering with subcategory topics
  1. report_top_referred_topics
  2. report_top_traffic_sources
  3. report_post_edit
  4. report_posts
  5. report_topics
  6. report_topics_with_no_response
* category_filtering tests (without subcategory topics)
  1. report_posts
  2. report_topics_with_no_response
* subcategory topics tests `in_category_and_subcategories` in `topic_spec.rb`
  1. `in_category_and_subcategories` in `topic_spec.rb`
  2. topics, posts, flags and topics_with_no_response in `report_spec.rb`
This commit is contained in:
Misaka 0x4e21
2018-08-10 08:50:05 +08:00
committed by Sam
parent ef4b9f98c1
commit 6db623ef6b
6 changed files with 175 additions and 32 deletions

View File

@@ -190,6 +190,17 @@ class Topic < ActiveRecord::Base
where("topics.category_id IS NULL OR topics.category_id IN (SELECT id FROM categories WHERE #{condition[0]})", condition[1])
}
IN_CATEGORY_AND_SUBCATEGORIES_SQL = <<~SQL
t.category_id = :category_id
OR t.category_id IN (SELECT id FROM categories WHERE categories.parent_category_id = :category_id)
SQL
scope :in_category_and_subcategories, lambda { |category_id|
where("topics.category_id = ? OR topics.category_id IN (SELECT id FROM categories WHERE categories.parent_category_id = ?)",
category_id,
category_id) if category_id
}
scope :with_subtype, ->(subtype) { where('topics.subtype = ?', subtype) }
attr_accessor :ignore_category_auto_close
@@ -1258,7 +1269,7 @@ class Topic < ActiveRecord::Base
builder = DB.build(sql)
builder.where("t.created_at >= :start_date", start_date: opts[:start_date]) if opts[:start_date]
builder.where("t.created_at < :end_date", end_date: opts[:end_date]) if opts[:end_date]
builder.where("t.category_id = :category_id", category_id: opts[:category_id]) if opts[:category_id]
builder.where(IN_CATEGORY_AND_SUBCATEGORIES_SQL, category_id: opts[:category_id]) if opts[:category_id]
builder.where("t.archetype <> '#{Archetype.private_message}'")
builder.where("t.deleted_at IS NULL")
builder.where("p.deleted_at IS NULL")
@@ -1297,7 +1308,7 @@ class Topic < ActiveRecord::Base
builder = DB.build(WITH_NO_RESPONSE_SQL)
builder.where("t.created_at >= :start_date", start_date: start_date) if start_date
builder.where("t.created_at < :end_date", end_date: end_date) if end_date
builder.where("t.category_id = :category_id", category_id: category_id) if category_id
builder.where(IN_CATEGORY_AND_SUBCATEGORIES_SQL, category_id: category_id) if category_id
builder.where("t.archetype <> '#{Archetype.private_message}'")
builder.where("t.deleted_at IS NULL")
builder.query_hash
@@ -1317,7 +1328,7 @@ class Topic < ActiveRecord::Base
def self.with_no_response_total(opts = {})
builder = DB.build(WITH_NO_RESPONSE_TOTAL_SQL)
builder.where("t.category_id = :category_id", category_id: opts[:category_id]) if opts[:category_id]
builder.where(IN_CATEGORY_AND_SUBCATEGORIES_SQL, category_id: opts[:category_id]) if opts[:category_id]
builder.where("t.archetype <> '#{Archetype.private_message}'")
builder.where("t.deleted_at IS NULL")
builder.query_single.first.to_i