Class: CategoryList

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization
Defined in:
app/models/category_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(guardian = nil, options = {}) ⇒ CategoryList

Returns a new instance of CategoryList.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/category_list.rb', line 26

def initialize(guardian = nil, options = {})
  @guardian = guardian || Guardian.new
  @options = options

  find_relevant_topics if options[:include_topics]
  find_categories

  prune_empty
  find_user_data
  sort_unpinned
  trim_results
  demote_muted

  if preloaded_topic_custom_fields.present?
    displayable_topics = @categories.map(&:displayable_topics)
    displayable_topics.flatten!
    displayable_topics.compact!

    if displayable_topics.present?
      Topic.preload_custom_fields(displayable_topics, preloaded_topic_custom_fields)
    end
  end
end

Instance Attribute Details

#categoriesObject

Returns the value of attribute categories.



9
10
11
# File 'app/models/category_list.rb', line 9

def categories
  @categories
end

#uncategorizedObject

Returns the value of attribute uncategorized.



9
10
11
# File 'app/models/category_list.rb', line 9

def uncategorized
  @uncategorized
end

Class Method Details

.included_associationsObject



16
17
18
19
20
21
22
23
24
# File 'app/models/category_list.rb', line 16

def self.included_associations
  [
    :uploaded_background,
    :uploaded_logo,
    :uploaded_logo_dark,
    :topic_only_relative_url,
    subcategories: [:topic_only_relative_url],
  ].concat(@included_assocations || [])
end

.order_categories(categories) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/category_list.rb', line 54

def self.order_categories(categories)
  if SiteSetting.fixed_category_positions
    categories.order(:position, :id)
  else
    allowed_category_ids = categories.pluck(:id) << nil # `nil` is necessary to include categories without any associated topics
    categories
      .left_outer_joins(:featured_topics)
      .where(topics: { category_id: allowed_category_ids })
      .group("categories.id")
      .order("max(topics.bumped_at) DESC NULLS LAST")
      .order("categories.id ASC")
  end
end

.register_included_association(association) ⇒ Object



11
12
13
14
# File 'app/models/category_list.rb', line 11

def self.register_included_association(association)
  @included_assocations ||= []
  @included_assocations << association if !@included_assocations.include?(association)
end

Instance Method Details

#preload_keyObject



50
51
52
# File 'app/models/category_list.rb', line 50

def preload_key
  "categories_list"
end