Skip to content

Commit

Permalink
cache comments count
Browse files Browse the repository at this point in the history
  • Loading branch information
mapra99 committed Jul 30, 2023
1 parent f08dc53 commit 23bd626
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 41 deletions.
11 changes: 4 additions & 7 deletions api/app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ class Comment < ApplicationRecord

belongs_to :user
belongs_to :parent, polymorphic: true
belongs_to :issue, counter_cache: true, optional: true
has_many :replies, class_name: 'Comment', as: :parent, dependent: :destroy

validates :content, presence: true

scope :oldest_first, -> { order(created_at: :asc) }

def comments_count
total = 1
before_save :set_issue

replies.each do |reply|
total += reply.comments_count
end

total
def set_issue
self.issue = parent.is_a?(Comment) ? parent.issue : parent
end
end
7 changes: 2 additions & 5 deletions api/app/models/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ class Issue < ApplicationRecord
belongs_to :user
belongs_to :product
belongs_to :issue_category
has_many :comments, as: :parent, dependent: :destroy
has_many :comments, as: :parent

Check warning on line 14 in api/app/models/issue.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] reported by reviewdog 🐶 Specify a `:dependent` option. Raw Output: app/models/issue.rb:14:3: C: Rails/HasManyOrHasOneDependent: Specify a `:dependent` option.
has_many :all_comments, class_name: 'Comment', dependent: :destroy
has_many :issue_upvotes, dependent: :destroy

validates :title, presence: true
validates :detail, presence: true
validates :status, presence: true, inclusion: { in: STATUSES.values }

def comments_count
comments.map(&:comments_count).sum
end

def upvoted_by?(user)
issue_upvotes.exists?(user_id: user.id)
end
Expand Down
5 changes: 5 additions & 0 deletions api/db/migrate/20230730232443_add_issue_id_to_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIssueIdToComments < ActiveRecord::Migration[6.1]
def change
add_reference :comments, :issue, null: true, foreign_key: true
end
end
5 changes: 5 additions & 0 deletions api/db/migrate/20230730232502_add_comments_count_to_issues.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCommentsCountToIssues < ActiveRecord::Migration[6.1]
def change
add_column :issues, :comments_count, :integer, null: false, default: 0
end
end
6 changes: 5 additions & 1 deletion api/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions api/db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'byebug'
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
Expand Down Expand Up @@ -52,6 +53,8 @@
# rubocop:disable Layout/LineLength
Comment.find_or_create_by(parent: issue, user:, content: 'Also, please allow styles to be applied based on system preferences. I would love to be able to browse Frontend Mentor in the evening after my device’s dark mode turns on without the bright background it currently has.')
comment = Comment.find_or_create_by(user:, parent: issue, content: 'Second this! I do a lot of late night coding and reading. Adding a dark theme can be great for preventing eye strain and the headaches that result. It’s also quite a trend with modern apps and apparently saves battery life.')

byebug

Check warning on line 57 in api/db/seeds.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] reported by reviewdog 🐶 Remove debugger entry point `byebug`. Raw Output: db/seeds.rb:57:1: W: Lint/Debugger: Remove debugger entry point `byebug`.
reply = comment.replies.find_or_create_by(user:, content: '@hummingbird1 While waiting for dark mode, there are browser extensions that will also do the job. Search for "dark theme” followed by your browser. There might be a need to turn off the extension for sites with naturally black backgrounds though.')
reply.replies.find_or_create_by(user:, content: "@annev1990 Good point! Using any kind of style extension is great and can be highly customizable, like the ability to change contrast and brightness. I'd prefer not to use one of such extensions, however, for security and privacy reasons.")

Expand Down
14 changes: 0 additions & 14 deletions api/spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,4 @@
expect(comment.uuid).to be_present
end
end

describe '#total_comments_count' do
let(:comment) { create(:comment) }

before do
child_comments = create_list(:comment, 2, parent: comment)
child_child_comment = create(:comment, parent: child_comments.first)
create(:comment, parent: child_child_comment)
end

it 'returns the total number of comments' do
expect(comment.comments_count).to eq(5)
end
end
end
14 changes: 0 additions & 14 deletions api/spec/models/issue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@
end
end

describe '#comments_count' do
let(:issue) { create(:issue) }

before do
comments = create_list(:comment, 2, parent: issue)
child_comment = create(:comment, parent: comments.first)
create(:comment, parent: child_comment)
end

it 'returns the total number of comments' do
expect(issue.comments_count).to eq(4)
end
end

describe '#upvoted_by?' do
subject(:upvoted_by?) { issue.upvoted_by?(user) }

Expand Down

0 comments on commit 23bd626

Please sign in to comment.