FIX: update group.has_messages field weekly
This commit is contained in:
parent
34f98f362f
commit
d64853dfa0
|
@ -308,6 +308,7 @@ class Group < ActiveRecord::Base
|
|||
def self.ensure_consistency!
|
||||
reset_all_counters!
|
||||
refresh_automatic_groups!
|
||||
refresh_has_messages!
|
||||
end
|
||||
|
||||
def self.reset_all_counters!
|
||||
|
@ -331,6 +332,18 @@ class Group < ActiveRecord::Base
|
|||
args.each { |group| refresh_automatic_group!(group) }
|
||||
end
|
||||
|
||||
def self.refresh_has_messages!
|
||||
exec_sql <<-SQL
|
||||
UPDATE groups g SET has_messages = false
|
||||
WHERE NOT EXISTS (SELECT tg.id
|
||||
FROM topic_allowed_groups tg
|
||||
INNER JOIN topics t ON t.id = tg.topic_id
|
||||
WHERE tg.group_id = g.id
|
||||
AND t.deleted_at IS NULL)
|
||||
AND g.has_messages = true
|
||||
SQL
|
||||
end
|
||||
|
||||
def self.ensure_automatic_groups!
|
||||
AUTO_GROUPS.each_key do |name|
|
||||
refresh_automatic_group!(name) unless lookup_group(name)
|
||||
|
|
|
@ -577,4 +577,21 @@ describe Group do
|
|||
expect(group.group_users.map(&:user_id)).to contain_exactly(user.id, admin.id)
|
||||
end
|
||||
end
|
||||
|
||||
it "Correctly updates has_messages" do
|
||||
group = Fabricate(:group, has_messages: true)
|
||||
topic = Fabricate(:private_message_topic)
|
||||
|
||||
# when group message is not present
|
||||
Group.refresh_has_messages!
|
||||
group.reload
|
||||
expect(group.has_messages?).to eq false
|
||||
|
||||
# when group message is present
|
||||
group.update!(has_messages: true)
|
||||
TopicAllowedGroup.create!(topic_id: topic.id, group_id: group.id)
|
||||
Group.refresh_has_messages!
|
||||
group.reload
|
||||
expect(group.has_messages?).to eq true
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue