Class: UserSummary
- Inherits:
-
Object
- Object
- UserSummary
- Defined in:
- app/models/user_summary.rb
Overview
ViewModel used on Summary tab on User page
Defined Under Namespace
Classes: CategoryWithCounts, UserWithCount
Constant Summary collapse
- MAX_SUMMARY_RESULTS =
6
- MAX_BADGES =
6
Instance Method Summary collapse
- #badges ⇒ Object
- #bookmark_count ⇒ Object
-
#initialize(user, guardian) ⇒ UserSummary
constructor
A new instance of UserSummary.
- #links ⇒ Object
- #most_liked_by_users ⇒ Object
- #most_liked_users ⇒ Object
- #most_replied_to_users ⇒ Object
- #recent_time_read ⇒ Object
- #replies ⇒ Object
- #top_categories ⇒ Object
- #topics ⇒ Object
- #user ⇒ Object
- #user_id ⇒ Object
- #user_stat ⇒ Object
Constructor Details
#initialize(user, guardian) ⇒ UserSummary
Returns a new instance of UserSummary.
11 12 13 14 |
# File 'app/models/user_summary.rb', line 11 def initialize(user, guardian) @user = user @guardian = guardian end |
Instance Method Details
#badges ⇒ Object
101 102 103 |
# File 'app/models/user_summary.rb', line 101 def badges @user.featured_user_badges(MAX_BADGES) end |
#bookmark_count ⇒ Object
117 118 119 |
# File 'app/models/user_summary.rb', line 117 def bookmark_count Bookmark.where(user: @user).count end |
#links ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/user_summary.rb', line 33 def links TopicLink .joins(:topic, :post) .where(posts: { user_id: @user.id }) .includes(:topic, :post) .where("posts.post_type IN (?)", Topic.visible_post_types(@guardian && @guardian.user)) .merge(Topic.listable_topics.visible.secured(@guardian)) .where(user: @user) .where(internal: false, reflection: false, quote: false) .order("clicks DESC, topic_links.created_at DESC") .limit(MAX_SUMMARY_RESULTS) end |
#most_liked_by_users ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/user_summary.rb', line 50 def most_liked_by_users likers = {} UserAction .joins(:target_topic, :target_post) .merge(Topic.listable_topics.visible.secured(@guardian)) .where(user: @user) .where(action_type: UserAction::WAS_LIKED) .group(:acting_user_id) .order("COUNT(*) DESC") .limit(MAX_SUMMARY_RESULTS) .pluck("acting_user_id, COUNT(*)") .each { |l| likers[l[0]] = l[1] } user_counts(likers) end |
#most_liked_users ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/models/user_summary.rb', line 66 def most_liked_users liked_users = {} UserAction .joins(:target_topic, :target_post) .merge(Topic.listable_topics.visible.secured(@guardian)) .where(action_type: UserAction::WAS_LIKED) .where(acting_user_id: @user.id) .group(:user_id) .order("COUNT(*) DESC") .limit(MAX_SUMMARY_RESULTS) .pluck("user_actions.user_id, COUNT(*)") .each { |l| liked_users[l[0]] = l[1] } user_counts(liked_users) end |
#most_replied_to_users ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/models/user_summary.rb', line 84 def most_replied_to_users replied_users = {} post_query .joins( "JOIN posts replies ON posts.topic_id = replies.topic_id AND posts.reply_to_post_number = replies.post_number", ) .where("replies.user_id <> ?", @user.id) .group("replies.user_id") .order("COUNT(*) DESC") .limit(MAX_SUMMARY_RESULTS) .pluck("replies.user_id, COUNT(*)") .each { |r| replied_users[r[0]] = r[1] } user_counts(replied_users) end |
#recent_time_read ⇒ Object
121 122 123 |
# File 'app/models/user_summary.rb', line 121 def recent_time_read @user.recent_time_read end |
#replies ⇒ Object
26 27 28 29 30 31 |
# File 'app/models/user_summary.rb', line 26 def replies post_query .where("post_number > 1") .order("posts.like_count DESC, posts.created_at DESC") .limit(MAX_SUMMARY_RESULTS) end |
#top_categories ⇒ Object
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 156 157 158 159 160 161 162 163 |
# File 'app/models/user_summary.rb', line 130 def top_categories post_count_query = post_query.group("topics.category_id") top_categories = {} Category .where( id: post_count_query.order("count(*) DESC").limit(MAX_SUMMARY_RESULTS).pluck("category_id"), ) .pluck(:id, :name, :color, :text_color, :slug, :read_restricted, :parent_category_id) .each do |c| top_categories[c[0].to_i] = CategoryWithCounts.new( Hash[CategoryWithCounts::KEYS.zip(c)].merge(topic_count: 0, post_count: 0), ) end post_count_query .where("post_number > 1") .where("topics.category_id in (?)", top_categories.keys) .pluck("category_id, COUNT(*)") .each { |r| top_categories[r[0].to_i].post_count = r[1] } Topic .listable_topics .visible .secured(@guardian) .where("topics.category_id in (?)", top_categories.keys) .where(user: @user) .group("topics.category_id") .pluck("category_id, COUNT(*)") .each { |r| top_categories[r[0].to_i].topic_count = r[1] } top_categories.values.sort_by { |r| -(r[:post_count] + r[:topic_count]) } end |
#topics ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'app/models/user_summary.rb', line 16 def topics Topic .secured(@guardian) .listable_topics .visible .where(user: @user) .order("like_count DESC, created_at DESC") .limit(MAX_SUMMARY_RESULTS) end |
#user ⇒ Object
109 110 111 |
# File 'app/models/user_summary.rb', line 109 def user @user end |
#user_id ⇒ Object
105 106 107 |
# File 'app/models/user_summary.rb', line 105 def user_id @user.id end |
#user_stat ⇒ Object
113 114 115 |
# File 'app/models/user_summary.rb', line 113 def user_stat @user.user_stat end |