Skip to content

Commit

Permalink
Add counter cache for good/bad events.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Guo committed Nov 15, 2024
1 parent 213107f commit a610f89
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/models/bad_event.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class BadEvent < ApplicationRecord
has_many :user_bad_events, dependent: :destroy
has_many :users, through: :user_bad_events
has_many :users, through: :user_bad_events, counter_cache: true
end
2 changes: 1 addition & 1 deletion app/models/good_event.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class GoodEvent < ApplicationRecord
has_many :user_good_events, dependent: :destroy
has_many :users, through: :user_good_events
has_many :users, through: :user_good_events, counter_cache: true
end
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class User < ApplicationRecord
has_many :user_job_roles, dependent: :destroy
has_many :job_roles, through: :user_job_roles
has_many :user_good_events, dependent: :destroy
has_many :good_events, through: :user_good_events
has_many :good_events, through: :user_good_events, counter_cache: true
has_many :user_bad_events, dependent: :destroy
has_many :bad_events, through: :user_bad_events
has_many :bad_events, through: :user_bad_events, counter_cache: true

normalizes :email, with: ->(email) { email.downcase.strip }

Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20241115082525_add_users_count_to_bad_events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddUsersCountToBadEvents < ActiveRecord::Migration[8.0]
def change
add_column :bad_events, :users_count, :integer, null: false, default: 0
add_column :good_events, :users_count, :integer, null: false, default: 0
add_column :users, :good_events_count, :integer, null: false, default: 0
add_column :users, :bad_events_count, :integer, null: false, default: 0
end
end
6 changes: 5 additions & 1 deletion db/schema.rb

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

15 changes: 15 additions & 0 deletions lib/tasks/maintain.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace :maintain do
desc "Reset counters"
task reset_counters: :environment do
BadEvent.find_each do |bad_event|
BadEvent.reset_counters(bad_event.id, :users) if bad_event.users.present?
end
GoodEvent.find_each do |good_event|
GoodEvent.reset_counters(good_event.id, :users) if good_event.users.present?
end
User.find_each do |user|
User.reset_counters(user.id, :bad_events) if user.bad_events.present?
User.reset_counters(user.id, :good_events) if user.good_events.present?
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures/bad_events.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
bad_one:
bad_title: "Bad things title"
users_count: 1

worse_two:
bad_title: "Worse things title"
users_count: 2
2 changes: 2 additions & 0 deletions test/fixtures/good_events.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
good_one:
good_title: "Good things title"
users_count: 2

great_two:
good_title: "Great things title"
users_count: 1
4 changes: 4 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ user_guochunzhong:
chinese_name: "过纯中"
hire_date: "2019-03-04"
clerk_code: "015454"
bad_events_count: 2
good_events_count: 1

user_fangzixue:
email: fangzixue@thape.com.cn
chinese_name: "方子雪"
hire_date: "2018-07-23"
clerk_code: "013771"
bad_events_count: 1
good_events_count: 2
2 changes: 2 additions & 0 deletions test/models/bad_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ class BadEventTest < ActiveSupport::TestCase
bad_one = bad_events(:bad_one)
assert bad_one.valid?
assert_equal bad_one.users.count, 1
assert_equal bad_one.users_count, 1
end

test "Worse two has 2 users" do
worse_two = bad_events(:worse_two)
assert worse_two.valid?
assert_equal worse_two.users.count, 2
assert_equal worse_two.users_count, 2
end
end
2 changes: 2 additions & 0 deletions test/models/good_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ class GoodEventTest < ActiveSupport::TestCase
good_one = good_events(:good_one)
assert good_one.valid?
assert_equal good_one.users.count, 2
assert_equal good_one.users_count, 2
end

test "Good two has 1 user" do
great_two = good_events(:great_two)
assert great_two.valid?
assert_equal great_two.users.count, 1
assert_equal great_two.users_count, 1
end
end
4 changes: 4 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ class UserTest < ActiveSupport::TestCase
guochunzhong = users(:user_guochunzhong)
assert guochunzhong.valid?
assert_equal guochunzhong.bad_events.count, 2
assert_equal guochunzhong.bad_events_count, 2
end

test "User user_fangzixue has 1 record of bad_events" do
fangzixue = users(:user_fangzixue)
assert fangzixue.valid?
assert_equal fangzixue.bad_events.count, 1
assert_equal fangzixue.bad_events_count, 1
end

test "User guochunzhong has 1 record of good_events" do
guochunzhong = users(:user_guochunzhong)
assert guochunzhong.valid?
assert_equal guochunzhong.good_events.count, 1
assert_equal guochunzhong.good_events_count, 1
end

test "User user_fangzixue has 2 record of good_events" do
fangzixue = users(:user_fangzixue)
assert fangzixue.valid?
assert_equal fangzixue.good_events.count, 2
assert_equal fangzixue.good_events_count, 2
end
end

0 comments on commit a610f89

Please sign in to comment.