* Added unique index to notifications #2124 * Fix migration + add index to schema.rb * Shoulda matcher notification unique validation
This commit is contained in:
parent
ed53191929
commit
065ef29e31
5 changed files with 10 additions and 2 deletions
|
|
@ -8,7 +8,7 @@ class Notification < ApplicationRecord
|
|||
|
||||
before_create :mark_notified_at_time
|
||||
|
||||
validates :user_id, uniqueness: { scope: %i[notifiable_id notifiable_type action] }
|
||||
validates :user_id, uniqueness: { scope: %i[organization_id notifiable_id notifiable_type action] }
|
||||
|
||||
class << self
|
||||
def send_new_follower_notification(follow, is_read = false)
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ module Notifications
|
|||
notification = Notification.find_by(notification_params)&.destroy
|
||||
else
|
||||
json_data = { user: user_data(follower), aggregated_siblings: aggregated_siblings }
|
||||
notification = Notification.find_or_create_by(notification_params)
|
||||
notification = Notification.find_or_initialize_by(notification_params)
|
||||
notification.notifiable_id = recent_follows.first.id
|
||||
notification.notifiable_type = "Follow"
|
||||
notification.json_data = json_data
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
class AddUniqueIndexToNotifications < ActiveRecord::Migration[5.1]
|
||||
def change
|
||||
add_index :notifications, %i[user_id organization_id notifiable_id notifiable_type action], unique: true, name: "index_notifications_on_user_organization_notifiable_and_action"
|
||||
end
|
||||
end
|
||||
|
|
@ -515,6 +515,7 @@ ActiveRecord::Schema.define(version: 2019_04_30_123156) do
|
|||
t.index ["json_data"], name: "index_notifications_on_json_data", using: :gin
|
||||
t.index ["notifiable_id"], name: "index_notifications_on_notifiable_id"
|
||||
t.index ["notifiable_type"], name: "index_notifications_on_notifiable_type"
|
||||
t.index ["user_id", "organization_id", "notifiable_id", "notifiable_type", "action"], name: "index_notifications_on_user_organization_notifiable_and_action", unique: true
|
||||
t.index ["user_id"], name: "index_notifications_on_user_id"
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ RSpec.describe Notification, type: :model do
|
|||
let(:follow_instance) { user.follow(user2) }
|
||||
let(:badge_achievement) { create(:badge_achievement) }
|
||||
|
||||
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(%i[organization_id notifiable_id notifiable_type action]) }
|
||||
|
||||
describe "when trying to #send_new_follower_notification after following a tag" do
|
||||
let(:tag) { create(:tag) }
|
||||
let(:tag_follow) { user.follow(tag) }
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue