Refactor find_relevant_topics and find_categories

This breaks down two private methods into smaller methods for
readability. This also moves logic into the Category model class.
This commit is contained in:
Nick Borromeo 2014-02-03 23:32:24 -08:00
parent 40041faf9d
commit 5104c7365f
2 changed files with 56 additions and 41 deletions

View File

@ -63,6 +63,9 @@ class Category < ActiveRecord::Base
scoped_to_permissions(guardian, [:create_post, :full]) scoped_to_permissions(guardian, [:create_post, :full])
end end
} }
scope :featured_users, ->{ includes(:featured_users) }
delegate :post_template, to: 'self.class' delegate :post_template, to: 'self.class'
# permission is just used by serialization # permission is just used by serialization
@ -148,6 +151,14 @@ SQL
posts_week = (#{posts_week})") posts_week = (#{posts_week})")
end end
def self.ordered_list(guardian)
self.secured(guardian)
.order('position asc')
.order('COALESCE(categories.posts_week, 0) DESC')
.order('COALESCE(categories.posts_month, 0) DESC')
.order('COALESCE(categories.posts_year, 0) DESC')
end
def visible_posts def visible_posts
query = Post.joins(:topic) query = Post.joins(:topic)
.where(['topics.category_id = ?', self.id]) .where(['topics.category_id = ?', self.id])
@ -331,8 +342,8 @@ SQL
end end
def self.query_category(slug, parent_category_id) def self.query_category(slug, parent_category_id)
self.where(slug: slug, parent_category_id: parent_category_id).includes(:featured_users).first || self.where(slug: slug, parent_category_id: parent_category_id).featured_users.first ||
self.where(id: slug.to_i, parent_category_id: parent_category_id).includes(:featured_users).first self.where(id: slug.to_i, parent_category_id: parent_category_id).featured_users.first
end end
def has_children? def has_children?

View File

@ -35,16 +35,23 @@ class CategoryList
# Retrieve a list of all the topics we'll need # Retrieve a list of all the topics we'll need
def find_relevant_topics def find_relevant_topics
@topics_by_id = {}
@topics_by_category_id = {} @topics_by_category_id = {}
category_featured_topics = CategoryFeaturedTopic.select([:category_id, :topic_id]).order(:rank) category_featured_topics = CategoryFeaturedTopic.select([:category_id, :topic_id]).order(:rank)
@topics_by_id = {}
build_topics_by_id(category_featured_topics)
build_topics_by_category_id(category_featured_topics)
end
def build_topics_by_id(category_featured_topics)
@all_topics = Topic.where(id: category_featured_topics.map(&:topic_id)) @all_topics = Topic.where(id: category_featured_topics.map(&:topic_id))
@all_topics.each do |t| @all_topics.each do |t|
t.include_last_poster = true if include_latest_posts? # hint for serialization t.include_last_poster = true if include_latest_posts? # hint for serialization
@topics_by_id[t.id] = t @topics_by_id[t.id] = t
end end
end
def build_topics_by_category_id(category_featured_topics)
category_featured_topics.each do |cft| category_featured_topics.each do |cft|
@topics_by_category_id[cft.category_id] ||= [] @topics_by_category_id[cft.category_id] ||= []
@topics_by_category_id[cft.category_id] << cft.topic_id @topics_by_category_id[cft.category_id] << cft.topic_id
@ -53,21 +60,26 @@ class CategoryList
# Find a list of all categories to associate the topics with # Find a list of all categories to associate the topics with
def find_categories def find_categories
@categories = Category @categories = Category.featured_users.ordered_list(@guardian).to_a
.includes(:featured_users, subcategories: [:topic_only_relative_url]) subcategories = {}
.secured(@guardian) to_delete = Set.new
.order('position asc')
.order('COALESCE(categories.posts_week, 0) DESC')
.order('COALESCE(categories.posts_month, 0) DESC')
.order('COALESCE(categories.posts_year, 0) DESC')
.to_a
if latest_post_only? if latest_post_only?
@categories = @categories.includes(:latest_post => {:topic => :last_poster} ) @categories = @categories.includes(:latest_post => {:topic => :last_poster} )
end end
subcategories = {} build_subcategories
to_delete = Set.new
if subcategories.present?
@categories.each { |c| c.subcategory_ids = subcategories[c.id] }
@categories.delete_if {|c| to_delete.include?(c) }
end
set_all_topics if latest_post_only?
set_displayable_category_topics if @topics_by_category_id
end
def build_subcategories
@categories.each do |c| @categories.each do |c|
if c.parent_category_id.present? if c.parent_category_id.present?
subcategories[c.parent_category_id] ||= [] subcategories[c.parent_category_id] ||= []
@ -75,44 +87,36 @@ class CategoryList
to_delete << c to_delete << c
end end
end end
end
if subcategories.present? def set_all_topics
@categories.each do |c| @all_topics = []
c.subcategory_ids = subcategories[c.id] @categories.each do |c|
end if c.latest_post && c.latest_post.topic
@categories.delete_if {|c| to_delete.include?(c) } c.displayable_topics = [c.latest_post.topic]
end topic = c.latest_post.topic
topic.include_last_poster = true # hint for serialization
if latest_post_only? @all_topics << topic
@all_topics = []
@categories.each do |c|
if c.latest_post && c.latest_post.topic
c.displayable_topics = [c.latest_post.topic]
topic = c.latest_post.topic
topic.include_last_poster = true # hint for serialization
@all_topics << topic
end
end end
end end
end
if @topics_by_category_id def set_displayable_category_topics
@categories.each do |c| @categories.each do |c|
topics_in_cat = @topics_by_category_id[c.id] topics_in_cat = @topics_by_category_id[c.id]
if topics_in_cat.present? if topics_in_cat.present?
c.displayable_topics = [] c.displayable_topics = []
topics_in_cat.each do |topic_id| topics_in_cat.each do |topic_id|
topic = @topics_by_id[topic_id] topic = @topics_by_id[topic_id]
if topic.present? if topic.present?
topic.category = c topic.category = c
c.displayable_topics << topic c.displayable_topics << topic
end
end end
end end
end end
end end
end end
# Remove any empty categories unless we can create them (so we can see the controls) # Remove any empty categories unless we can create them (so we can see the controls)
def prune_empty def prune_empty
if !@guardian.can_create?(Category) if !@guardian.can_create?(Category)