@@ -35,23 +35,16 @@ def latest_posts_count
35
35
36
36
# Retrieve a list of all the topics we'll need
37
37
def find_relevant_topics
38
- @topics_by_id = { }
39
38
@topics_by_category_id = { }
40
39
category_featured_topics = CategoryFeaturedTopic . select ( [ :category_id , :topic_id ] ) . order ( :rank )
40
+ @topics_by_id = { }
41
41
42
- build_topics_by_id ( category_featured_topics )
43
- build_topics_by_category_id ( category_featured_topics )
44
- end
45
-
46
- def build_topics_by_id ( category_featured_topics )
47
42
@all_topics = Topic . where ( id : category_featured_topics . map ( &:topic_id ) )
48
43
@all_topics . each do |t |
49
44
t . include_last_poster = true if include_latest_posts? # hint for serialization
50
45
@topics_by_id [ t . id ] = t
51
46
end
52
- end
53
47
54
- def build_topics_by_category_id ( category_featured_topics )
55
48
category_featured_topics . each do |cft |
56
49
@topics_by_category_id [ cft . category_id ] ||= [ ]
57
50
@topics_by_category_id [ cft . category_id ] << cft . topic_id
@@ -60,63 +53,66 @@ def build_topics_by_category_id(category_featured_topics)
60
53
61
54
# Find a list of all categories to associate the topics with
62
55
def find_categories
63
- @categories = Category . featured_users . ordered_list ( @guardian ) . to_a
64
- subcategories = { }
65
- to_delete = Set . new
56
+ @categories = Category
57
+ . includes ( :featured_users , subcategories : [ :topic_only_relative_url ] )
58
+ . secured ( @guardian )
59
+ . order ( 'position asc' )
60
+ . order ( 'COALESCE(categories.posts_week, 0) DESC' )
61
+ . order ( 'COALESCE(categories.posts_month, 0) DESC' )
62
+ . order ( 'COALESCE(categories.posts_year, 0) DESC' )
63
+ . to_a
66
64
67
65
if latest_post_only?
68
66
@categories = @categories . includes ( :latest_post => { :topic => :last_poster } )
69
67
end
70
68
71
- build_subcategories
72
-
73
- if subcategories . present?
74
- @categories . each { |c | c . subcategory_ids = subcategories [ c . id ] }
75
- @categories . delete_if { |c | to_delete . include? ( c ) }
76
- end
77
-
78
- set_all_topics if latest_post_only?
79
- set_displayable_category_topics if @topics_by_category_id
80
- end
81
-
82
- def build_subcategories
69
+ subcategories = { }
70
+ to_delete = Set . new
83
71
@categories . each do |c |
84
72
if c . parent_category_id . present?
85
73
subcategories [ c . parent_category_id ] ||= [ ]
86
74
subcategories [ c . parent_category_id ] << c . id
87
75
to_delete << c
88
76
end
89
77
end
90
- end
91
78
92
- def set_all_topics
93
- @all_topics = [ ]
94
- @categories . each do |c |
95
- if c . latest_post && c . latest_post . topic
96
- c . displayable_topics = [ c . latest_post . topic ]
97
- topic = c . latest_post . topic
98
- topic . include_last_poster = true # hint for serialization
99
- @all_topics << topic
79
+ if subcategories . present?
80
+ @categories . each do |c |
81
+ c . subcategory_ids = subcategories [ c . id ]
100
82
end
83
+ @categories . delete_if { |c | to_delete . include? ( c ) }
101
84
end
102
- end
103
85
104
- def set_displayable_category_topics
105
- @categories . each do |c |
106
- topics_in_cat = @topics_by_category_id [ c . id ]
107
- if topics_in_cat . present?
108
- c . displayable_topics = [ ]
109
- topics_in_cat . each do |topic_id |
110
- topic = @topics_by_id [ topic_id ]
111
- if topic . present?
112
- topic . category = c
113
- c . displayable_topics << topic
86
+ if latest_post_only?
87
+ @all_topics = [ ]
88
+ @categories . each do |c |
89
+ if c . latest_post && c . latest_post . topic
90
+ c . displayable_topics = [ c . latest_post . topic ]
91
+ topic = c . latest_post . topic
92
+ topic . include_last_poster = true # hint for serialization
93
+ @all_topics << topic
94
+ end
95
+ end
96
+ end
97
+
98
+ if @topics_by_category_id
99
+ @categories . each do |c |
100
+ topics_in_cat = @topics_by_category_id [ c . id ]
101
+ if topics_in_cat . present?
102
+ c . displayable_topics = [ ]
103
+ topics_in_cat . each do |topic_id |
104
+ topic = @topics_by_id [ topic_id ]
105
+ if topic . present?
106
+ topic . category = c
107
+ c . displayable_topics << topic
108
+ end
114
109
end
115
110
end
116
111
end
117
112
end
118
113
end
119
114
115
+
120
116
# Remove any empty categories unless we can create them (so we can see the controls)
121
117
def prune_empty
122
118
if !@guardian . can_create? ( Category )
0 commit comments