Merge pull request #5209 from techAPJ/has-messages

FIX: update group.has_messages field weekly
This commit is contained in:
Arpit Jalan 2017-09-29 09:12:26 +05:30 committed by GitHub
commit 689f949fdb
2 changed files with 30 additions and 0 deletions

View File

@ -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)

View File

@ -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