-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'EN-7826-denorm-messageable-count'
- Loading branch information
Showing
11 changed files
with
92 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class CountChatMessageJob | ||
include Sidekiq::Worker | ||
|
||
sidekiq_options :retry => true, queue: :default | ||
|
||
def perform messageable_type, messageable_id | ||
return unless klass = messageable_type.constantize | ||
return unless instance = klass.find(messageable_id) | ||
return unless instance.respond_to?(:number_of_root_chat_messages) | ||
|
||
instance.update_attribute(:number_of_root_chat_messages, | ||
number_of_root_chat_messages(messageable_type, messageable_id) | ||
) | ||
end | ||
|
||
def number_of_root_chat_messages messageable_type, messageable_id | ||
ChatMessage | ||
.where(messageable_type: messageable_type, messageable_id: messageable_id, ancestry: nil) | ||
.where.not(status: :deleted) | ||
.count | ||
end | ||
|
||
def self.perform_later messageable_type, messageable_id | ||
CountChatMessageJob.perform_async(messageable_type, messageable_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...observers/unread_chat_message_observer.rb → ...observers/denorm_chat_message_observer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
db/migrate/20241120115400_add_number_of_root_chat_messages_to_messageables.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddNumberOfRootChatMessagesToMessageables < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :entourages, :number_of_root_chat_messages, :integer, default: 0 | ||
add_column :neighborhoods, :number_of_root_chat_messages, :integer, default: 0 | ||
end | ||
end |
34 changes: 34 additions & 0 deletions
34
db/migrate/20241120115401_initialize_number_of_root_chat_messages.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class InitializeNumberOfRootChatMessages < ActiveRecord::Migration[6.1] | ||
def up | ||
# entourages | ||
execute <<-SQL.squish | ||
UPDATE entourages | ||
SET number_of_root_chat_messages = ( | ||
SELECT COUNT(*) | ||
FROM chat_messages | ||
WHERE chat_messages.messageable_id = entourages.id | ||
AND chat_messages.messageable_type = 'Entourage' | ||
AND chat_messages.ancestry IS NULL | ||
AND chat_messages.status != 'deleted' | ||
) | ||
SQL | ||
|
||
# neighborhoods | ||
execute <<-SQL.squish | ||
UPDATE neighborhoods | ||
SET number_of_root_chat_messages = ( | ||
SELECT COUNT(*) | ||
FROM chat_messages | ||
WHERE chat_messages.messageable_id = neighborhoods.id | ||
AND chat_messages.messageable_type = 'Neighborhood' | ||
AND chat_messages.ancestry IS NULL | ||
AND chat_messages.status != 'deleted' | ||
) | ||
SQL | ||
end | ||
|
||
def down | ||
Entourage.update_all(number_of_root_chat_messages: nil) | ||
Neighborhood.update_all(number_of_root_chat_messages: nil) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters