-
Notifications
You must be signed in to change notification settings - Fork 572
Pre-calculate comment counts and activity scores #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
b4e8adc
f1c8ae2
8cd22a7
f17b060
0c38699
2a9be42
9ac136b
df746e5
c88fefd
e117f4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| module Bubble::Commentable | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| scope :ordered_by_comments, -> { order comments_count: :desc } | ||
| end | ||
|
|
||
| def comment_created | ||
| increment! :comments_count | ||
| rescore | ||
| end | ||
|
|
||
| def comment_destroyed | ||
| decrement! :comments_count | ||
| rescore | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,15 @@ class Message < ApplicationRecord | |
|
|
||
| scope :chronologically, -> { order created_at: :asc, id: :desc } | ||
|
|
||
| # FIXME: Will be made redundant when we compute activity and comment count at write time. See commit. | ||
| scope :left_joins_messageable, ->(messageable_type) do | ||
| joins "LEFT OUTER JOIN #{messageable_type} ON messages.messageable_id = #{messageable_type}.id" | ||
| end | ||
| after_create :created | ||
| after_destroy :destroyed | ||
|
|
||
| private | ||
| def created | ||
| bubble.comment_created if comment? | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We considered this sort of design as well: class Message < AR
after_create :captured
after_destroy :uncaptured
private
def captured
messageable.captured_as(self)
end
def uncaptured
messageable.uncaptured_as(self)
end
end
class Comment < AR
def captured_as(message)
message.bubble.comment_captured
end
def uncaptured_as(message)
message.bubble.comment_uncaptured
end
endBut it felt premature in that comments are the only kind of messageable (of which there's only two now) that needs to notify the bubble it's been captured. We can migrate to that design if needed. Doing the simplest thing for now. |
||
| end | ||
|
|
||
| def destroyed | ||
| bubble.comment_destroyed if comment? | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddCommentsCountToBubbles < ActiveRecord::Migration[8.0] | ||
| def change | ||
| add_column :bubbles, :comments_count, :integer, null: false, default: 0 | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddActivityScoreToBubbles < ActiveRecord::Migration[8.0] | ||
| def change | ||
| add_column :bubbles, :activity_score, :integer, null: false, default: 0 | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.