Class: TopicList
- Inherits:
-
Object
- Object
- TopicList
- Includes:
- ActiveModel::Serialization
- Defined in:
- app/models/topic_list.rb
Instance Attribute Summary collapse
-
#category ⇒ Object
Returns the value of attribute category.
-
#current_user ⇒ Object
Returns the value of attribute current_user.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#for_period ⇒ Object
Returns the value of attribute for_period.
-
#more_topics_url ⇒ Object
Returns the value of attribute more_topics_url.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#prev_topics_url ⇒ Object
Returns the value of attribute prev_topics_url.
-
#shared_drafts ⇒ Object
Returns the value of attribute shared_drafts.
-
#tags ⇒ Object
Returns the value of attribute tags.
Class Method Summary collapse
- .cancel_preload(&blk) ⇒ Object
- .on_preload(&blk) ⇒ Object
- .on_preload_user_ids(&blk) ⇒ Object
- .preload(topics, object) ⇒ Object
- .preload_user_ids(topics, user_ids, object) ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #categories ⇒ Object
-
#initialize(filter, current_user, topics, opts = nil) ⇒ TopicList
constructor
A new instance of TopicList.
- #load_topics ⇒ Object
- #preload_key ⇒ Object
- #top_tags ⇒ Object
-
#topics ⇒ Object
Lazy initialization.
Constructor Details
#initialize(filter, current_user, topics, opts = nil) ⇒ TopicList
Returns a new instance of TopicList.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/topic_list.rb', line 50 def initialize(filter, current_user, topics, opts = nil) @filter = filter @current_user = current_user @topics_input = topics @opts = opts || {} @category = Category.find_by(id: @opts[:category_id]) if @opts[:category] @tags = Tag.where(id: @opts[:tag_ids]).all if @opts[:tag_ids].present? @publish_read_state = !!@opts[:publish_read_state] end |
Instance Attribute Details
#category ⇒ Object
Returns the value of attribute category.
37 38 39 |
# File 'app/models/topic_list.rb', line 37 def category @category end |
#current_user ⇒ Object
Returns the value of attribute current_user.
37 38 39 |
# File 'app/models/topic_list.rb', line 37 def current_user @current_user end |
#filter ⇒ Object
Returns the value of attribute filter.
37 38 39 |
# File 'app/models/topic_list.rb', line 37 def filter @filter end |
#for_period ⇒ Object
Returns the value of attribute for_period.
37 38 39 |
# File 'app/models/topic_list.rb', line 37 def for_period @for_period end |
#more_topics_url ⇒ Object
Returns the value of attribute more_topics_url.
37 38 39 |
# File 'app/models/topic_list.rb', line 37 def more_topics_url @more_topics_url end |
#per_page ⇒ Object
Returns the value of attribute per_page.
37 38 39 |
# File 'app/models/topic_list.rb', line 37 def per_page @per_page end |
#prev_topics_url ⇒ Object
Returns the value of attribute prev_topics_url.
37 38 39 |
# File 'app/models/topic_list.rb', line 37 def prev_topics_url @prev_topics_url end |
#shared_drafts ⇒ Object
Returns the value of attribute shared_drafts.
37 38 39 |
# File 'app/models/topic_list.rb', line 37 def shared_drafts @shared_drafts end |
#tags ⇒ Object
Returns the value of attribute tags.
37 38 39 |
# File 'app/models/topic_list.rb', line 37 def @tags end |
Class Method Details
.cancel_preload(&blk) ⇒ Object
13 14 15 16 17 18 |
# File 'app/models/topic_list.rb', line 13 def self.cancel_preload(&blk) if @preload @preload.delete blk @preload = nil if @preload.length == 0 end end |
.on_preload(&blk) ⇒ Object
9 10 11 |
# File 'app/models/topic_list.rb', line 9 def self.on_preload(&blk) (@preload ||= Set.new) << blk end |
.on_preload_user_ids(&blk) ⇒ Object
24 25 26 |
# File 'app/models/topic_list.rb', line 24 def self.on_preload_user_ids(&blk) (@preload_user_ids ||= Set.new) << blk end |
.preload(topics, object) ⇒ Object
20 21 22 |
# File 'app/models/topic_list.rb', line 20 def self.preload(topics, object) @preload.each { |preload| preload.call(topics, object) } if @preload end |
.preload_user_ids(topics, user_ids, object) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'app/models/topic_list.rb', line 28 def self.preload_user_ids(topics, user_ids, object) if @preload_user_ids @preload_user_ids.each do |preload_user_ids| user_ids = preload_user_ids.call(topics, user_ids, object) end end user_ids end |
Instance Method Details
#attributes ⇒ Object
157 158 159 |
# File 'app/models/topic_list.rb', line 157 def attributes { "more_topics_url" => page } end |
#categories ⇒ Object
78 79 80 81 |
# File 'app/models/topic_list.rb', line 78 def categories @categories ||= topics.map { |t| [t.category&.parent_category, t.category] }.flatten.uniq.compact end |
#load_topics ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/models/topic_list.rb', line 83 def load_topics @topics = @topics_input # Attach some data for serialization to each topic @topic_lookup = TopicUser.lookup_for(@current_user, @topics) if @current_user @dismissed_topic_users_lookup = DismissedTopicUser.lookup_for(@current_user, @topics) if @current_user post_action_type = if @current_user if @opts[:filter].present? PostActionType.types[:like] if @opts[:filter] == "liked" end end # Data for bookmarks or likes post_action_lookup = PostAction.lookup_for(@current_user, @topics, post_action_type) if post_action_type # Create a lookup for all the user ids we need user_ids = [] group_ids = [] @topics.each do |ft| user_ids << ft.user_id << ft.last_post_user_id << ft.featured_user_ids << ft.allowed_user_ids group_ids |= (ft.allowed_group_ids || []) end user_ids = TopicList.preload_user_ids(@topics, user_ids, self) user_lookup = UserLookup.new(user_ids) group_lookup = GroupLookup.new(group_ids) @topics.each do |ft| ft.user_data = @topic_lookup[ft.id] if @topic_lookup.present? if ft.regular? && category_user_lookup.present? ft.category_user_data = @category_user_lookup[ft.category_id] end ft.dismissed = @current_user && @dismissed_topic_users_lookup.include?(ft.id) if ft.user_data && post_action_lookup && actions = post_action_lookup[ft.id] ft.user_data.post_action_data = { post_action_type => actions } end ft.posters = ft.posters_summary(user_lookup: user_lookup) ft.participants = ft.participants_summary(user_lookup: user_lookup, user: @current_user) ft.participant_groups = ft.participant_groups_summary(group_lookup: group_lookup, group: @opts[:group]) ft.topic_list = self end topic_preloader_associations = [ :image_upload, { topic_thumbnails: :optimized_image }, { category: :parent_category }, ] topic_preloader_associations.concat(DiscoursePluginRegistry.topic_preloader_associations.to_a) ActiveRecord::Associations::Preloader.new( records: @topics, associations: topic_preloader_associations, ).call if preloaded_custom_fields.present? Topic.preload_custom_fields(@topics, preloaded_custom_fields) end TopicList.preload(@topics, self) @topics end |
#preload_key ⇒ Object
69 70 71 |
# File 'app/models/topic_list.rb', line 69 def preload_key "topic_list" end |
#top_tags ⇒ Object
63 64 65 66 67 |
# File 'app/models/topic_list.rb', line 63 def opts = @category ? { category: @category } : {} opts[:guardian] = Guardian.new(@current_user) Tag.(**opts) end |
#topics ⇒ Object
Lazy initialization
74 75 76 |
# File 'app/models/topic_list.rb', line 74 def topics @topics ||= load_topics end |