Skip to content

Commit

Permalink
Merge pull request #3795 from fjordllc/feature/delete-unassigned-coun…
Browse files Browse the repository at this point in the history
…t-cache-when-original-value-changed

提出物一覧ページの「未アサイン」タブのバッジに表示する件数を適切に増減させる
  • Loading branch information
komagata authored Jan 18, 2022
2 parents 4a43058 + a1b414c commit aec88e8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def other_checker_exists?(current_user_id)
checker_id.present? && checker_id.to_s != current_user_id
end

def unassigned?
checker_id.nil?
end

def checker_name
checker_id ? User.find(checker_id).login_name : nil
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/product_callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def after_update(product)

checker_id = product.checker_id || product.attribute_before_last_save('checker_id')
Cache.delete_self_assigned_no_replied_product_count(checker_id)
Cache.delete_unassigned_product_count
end

def after_save(product)
Expand Down Expand Up @@ -42,7 +43,7 @@ def after_destroy(product)
delete_notification(product)

Cache.delete_unchecked_product_count
Cache.delete_unassigned_product_count
Cache.delete_unassigned_product_count if product.unassigned?
Cache.delete_self_assigned_no_replied_product_count(product.checker_id)
end

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/products.yml
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,9 @@ product63:
body: 「自分の担当」かつ「返信済み」の提出物
wip: false
checker_id: <%= ActiveRecord::FixtureSet.identify(:machida) %>

product64:
practice: practice10
user: kimura
body: 担当者のいる提出物です。
checker_id: "<%= ActiveRecord::FixtureSet.identify(:machida) %>"
35 changes: 35 additions & 0 deletions test/models/caches/unassigned_product_count_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'test_helper'

class UnassignedProductCountTest < ActiveSupport::TestCase
test 'cached count of unassigned products increases by 1 after creating a product' do
assert_difference 'Cache.unassigned_product_count', 1 do
Product.create!(practice: practices(:practice5), user: users(:kimura), body: 'test')
end
end

test 'cached count of unassigned products decreases by 1 after destroying an unassigned product' do
unassigned_product = Product.not_wip.unassigned.first

assert_difference 'Cache.unassigned_product_count', -1 do
unassigned_product.destroy!
end
end

test 'cached count of unassigned products decreases by 1 after a mentor gets assigned to an unassigned product' do
unassigned_product = Product.not_wip.unassigned.first

assert_difference 'Cache.unassigned_product_count', -1 do
unassigned_product.update!(checker_id: users(:machida).id)
end
end

test 'cached count of unassigned products increases by 1 after a mentor gets unassigned from an assigned product' do
assigned_product = Product.self_assigned_product(users(:machida).id).first

assert_difference 'Cache.unassigned_product_count', 1 do
assigned_product.update!(checker_id: nil)
end
end
end

0 comments on commit aec88e8

Please sign in to comment.