-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api][webui] Use a polymorphic relationship in Notifications
- Loading branch information
1 parent
0520e67
commit a03c953
Showing
13 changed files
with
137 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
class CleanupNotifications < ApplicationJob | ||
def perform | ||
Notifications::RssFeedItem.cleanup | ||
Notification::RssFeedItem.cleanup | ||
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
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
15 changes: 15 additions & 0 deletions
15
src/api/db/migrate/20170619111734_alter_notifications_to_use_polymorphic.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,15 @@ | ||
class AlterNotificationsToUsePolymorphic < ActiveRecord::Migration[5.0] | ||
def change | ||
add_reference :notifications, :subscriber, polymorphic: true | ||
|
||
Notification.find_in_batches batch_size: 500 do |batch| | ||
batch.each do |notification| | ||
notification.subscriber = notification.user || notification.group | ||
notification.save! | ||
end | ||
end | ||
|
||
remove_reference :notifications, :group | ||
remove_reference :notifications, :user | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
FactoryGirl.define do | ||
factory :notification, class: 'Notifications::Base' do | ||
factory :notification do | ||
event_type 'FakeEventType' | ||
event_payload 'FakeJsonPayload' | ||
subscription_receiver_role 'owner' | ||
end | ||
|
||
factory :rss_notification, parent: :notification, class: 'Notifications::RssFeedItem' do | ||
factory :rss_notification, parent: :notification, class: 'Notification::RssFeedItem' do | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,85 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Notifications::RssFeedItem do | ||
RSpec.describe Notification::RssFeedItem do | ||
let(:payload) { { comment: 'SuperFakeComment', requestid: 1 } } | ||
let(:delete_package_event) { Event::DeletePackage.new(payload) } | ||
|
||
describe '.cleanup' do | ||
let(:user) { create(:confirmed_user) } | ||
let(:user2) { create(:confirmed_user) } | ||
let(:group) { create(:group, users: [user, user2]) } | ||
let(:unconfirmed_user) { create(:user) } | ||
let(:max_items_per_user) { Notifications::RssFeedItem::MAX_ITEMS_PER_USER } | ||
let(:max_items_per_group) { Notifications::RssFeedItem::MAX_ITEMS_PER_GROUP } | ||
let(:max_items_per_user) { Notification::RssFeedItem::MAX_ITEMS_PER_USER } | ||
let(:max_items_per_group) { Notification::RssFeedItem::MAX_ITEMS_PER_GROUP } | ||
let(:greater_than_max_items_per_user) { max_items_per_user + 3 } | ||
let(:greater_than_max_items_per_group) { max_items_per_group + 5 } | ||
|
||
context 'without any RSS items' do | ||
it { expect { Notifications::RssFeedItem.cleanup }.not_to change { Notifications::RssFeedItem.count } } | ||
it { expect { Notification::RssFeedItem.cleanup }.not_to change { Notification::RssFeedItem.count } } | ||
end | ||
|
||
context 'with a single users' do | ||
context 'and not enough RSS items' do | ||
before do | ||
create_list(:rss_notification, max_items_per_user, user: user) | ||
create_list(:rss_notification, max_items_per_user, subscriber: user) | ||
end | ||
|
||
it { expect { Notifications::RssFeedItem.cleanup }.not_to change { Notifications::RssFeedItem.count } } | ||
it { expect { Notification::RssFeedItem.cleanup }.not_to change { Notification::RssFeedItem.count } } | ||
end | ||
|
||
context 'and enough RSS items' do | ||
context 'for an active user' do | ||
before do | ||
create_list(:rss_notification, greater_than_max_items_per_user, user: user) | ||
create_list(:rss_notification, greater_than_max_items_per_user, subscriber: user) | ||
end | ||
|
||
it { expect { Notifications::RssFeedItem.cleanup }.to change { Notifications::RssFeedItem.count }.by(-3) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { Notification::RssFeedItem.count }.by(-3) } | ||
end | ||
|
||
context 'for a non active user' do | ||
before do | ||
create_list(:rss_notification, greater_than_max_items_per_user, user: unconfirmed_user) | ||
create_list(:rss_notification, greater_than_max_items_per_user, subscriber: unconfirmed_user) | ||
end | ||
|
||
it { expect { Notifications::RssFeedItem.cleanup }.to change { Notifications::RssFeedItem.count }.by(-greater_than_max_items_per_user) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { Notification::RssFeedItem.count }.by(-greater_than_max_items_per_user) } | ||
end | ||
end | ||
end | ||
|
||
context 'with multiple users' do | ||
context 'all of them active' do | ||
before do | ||
create_list(:rss_notification, greater_than_max_items_per_user, user: user) | ||
create_list(:rss_notification, greater_than_max_items_per_user, user: user2) | ||
create_list(:rss_notification, greater_than_max_items_per_user, subscriber: user) | ||
create_list(:rss_notification, greater_than_max_items_per_user, subscriber: user2) | ||
end | ||
|
||
it { expect { Notifications::RssFeedItem.cleanup }.to change { user.rss_feed_items.count }.by(-3) } | ||
it { expect { Notifications::RssFeedItem.cleanup }.to change { user2.rss_feed_items.count }.by(-3) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { user.rss_feed_items.count }.by(-3) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { user2.rss_feed_items.count }.by(-3) } | ||
end | ||
|
||
context 'being a mixture of active and non active users' do | ||
before do | ||
create_list(:rss_notification, greater_than_max_items_per_user, user: user) | ||
create_list(:rss_notification, greater_than_max_items_per_user, user: unconfirmed_user) | ||
create_list(:rss_notification, greater_than_max_items_per_user, subscriber: user) | ||
create_list(:rss_notification, greater_than_max_items_per_user, subscriber: unconfirmed_user) | ||
end | ||
|
||
it { expect { Notifications::RssFeedItem.cleanup }.to change { user.rss_feed_items.count }.by(-3) } | ||
it { expect { Notifications::RssFeedItem.cleanup }.to change { unconfirmed_user.rss_feed_items.count }.by(-greater_than_max_items_per_user) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { user.rss_feed_items.count }.by(-3) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { unconfirmed_user.rss_feed_items.count }.by(-greater_than_max_items_per_user) } | ||
end | ||
end | ||
|
||
context 'with users and group notifications' do | ||
before do | ||
create_list(:rss_notification, greater_than_max_items_per_user, user: user) | ||
create_list(:rss_notification, greater_than_max_items_per_user, user: user2) | ||
create_list(:rss_notification, greater_than_max_items_per_user, user: unconfirmed_user) | ||
create_list(:rss_notification, greater_than_max_items_per_group, group: group) | ||
create_list(:rss_notification, greater_than_max_items_per_user, subscriber: user) | ||
create_list(:rss_notification, greater_than_max_items_per_user, subscriber: user2) | ||
create_list(:rss_notification, greater_than_max_items_per_user, subscriber: unconfirmed_user) | ||
create_list(:rss_notification, greater_than_max_items_per_group, subscriber: group) | ||
end | ||
|
||
it { expect { Notifications::RssFeedItem.cleanup }.to change { user.rss_feed_items.count }.by(-3) } | ||
it { expect { Notifications::RssFeedItem.cleanup }.to change { user2.rss_feed_items.count }.by(-3) } | ||
it { expect { Notifications::RssFeedItem.cleanup }.to change { group.rss_feed_items.count }.by(-5) } | ||
it { expect { Notifications::RssFeedItem.cleanup }.to change { unconfirmed_user.rss_feed_items.count }.by(-greater_than_max_items_per_user) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { user.rss_feed_items.count }.by(-3) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { user2.rss_feed_items.count }.by(-3) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { group.rss_feed_items.count }.by(-5) } | ||
it { expect { Notification::RssFeedItem.cleanup }.to change { unconfirmed_user.rss_feed_items.count }.by(-greater_than_max_items_per_user) } | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Notification do | ||
let(:payload) { {comment: 'SuperFakeComment', requestid: 1} } | ||
let(:delete_package_event) { Event::DeletePackage.new(payload) } | ||
|
||
describe '#event' do | ||
subject { create(:rss_notification, event_type: 'Event::DeletePackage', event_payload: payload).event } | ||
|
||
it { expect(subject.class).to eq(delete_package_event.class) } | ||
it { expect(subject.payload).to eq(delete_package_event.payload) } | ||
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