[deploy] Set UserSubscription sourceable columns to nullable (#10033)
* Set UserSubscription sourceable columns to nullable * Fix spec
This commit is contained in:
parent
2e6d081243
commit
56f1f2d777
5 changed files with 46 additions and 9 deletions
|
|
@ -4,7 +4,7 @@ module UserSubscriptionSourceable
|
|||
# This all assumes there's an association with User under the column user_id.
|
||||
|
||||
included do
|
||||
has_many :user_subscriptions, as: :user_subscription_sourceable
|
||||
has_many :user_subscriptions, as: :user_subscription_sourceable, dependent: :nullify
|
||||
has_many :sourced_subscribers,
|
||||
class_name: "User",
|
||||
through: :user_subscriptions,
|
||||
|
|
|
|||
|
|
@ -9,14 +9,23 @@ class UserSubscription < ApplicationRecord
|
|||
|
||||
belongs_to :author, class_name: "User", inverse_of: :source_authored_user_subscriptions
|
||||
belongs_to :subscriber, class_name: "User", inverse_of: :subscribed_to_user_subscriptions
|
||||
belongs_to :user_subscription_sourceable, polymorphic: true
|
||||
belongs_to :user_subscription_sourceable, polymorphic: true, optional: true
|
||||
|
||||
validates :author_id, presence: true
|
||||
|
||||
validates :subscriber_email, presence: true
|
||||
validates :subscriber_id, presence: true, uniqueness: { scope: %i[subscriber_email user_subscription_sourceable_type
|
||||
user_subscription_sourceable_id] }
|
||||
validates :user_subscription_sourceable_id, presence: true
|
||||
validates :user_subscription_sourceable_type, presence: true, inclusion: { in: ALLOWED_TYPES }
|
||||
validates :subscriber_id, presence: true, uniqueness: {
|
||||
scope: %i[subscriber_email user_subscription_sourceable_type user_subscription_sourceable_id]
|
||||
}
|
||||
|
||||
validates :user_subscription_sourceable_id, presence: true, on: :create
|
||||
validates :user_subscription_sourceable_id, presence: true, on: :update, if: :user_subscription_sourceable_type
|
||||
validates :user_subscription_sourceable_type, presence: true, on: :create
|
||||
validates :user_subscription_sourceable_type, presence: true, on: :update, if: :user_subscription_sourceable_id
|
||||
|
||||
validates :user_subscription_sourceable_type, inclusion: { in: ALLOWED_TYPES }, on: :create
|
||||
validates :user_subscription_sourceable_type,
|
||||
inclusion: { in: ALLOWED_TYPES }, on: :update, if: :user_subscription_sourceable_id
|
||||
|
||||
validate :tag_enabled
|
||||
validate :non_apple_auth_subscriber
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
class SetUserSubscriptionSourceableColumnsToNull < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
change_column_null :user_subscriptions, :user_subscription_sourceable_id, true
|
||||
change_column_null :user_subscriptions, :user_subscription_sourceable_type, true
|
||||
end
|
||||
end
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2020_08_22_092853) do
|
||||
ActiveRecord::Schema.define(version: 2020_08_27_073520) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "citext"
|
||||
|
|
@ -1161,8 +1161,8 @@ ActiveRecord::Schema.define(version: 2020_08_22_092853) do
|
|||
t.string "subscriber_email", null: false
|
||||
t.bigint "subscriber_id", null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.bigint "user_subscription_sourceable_id", null: false
|
||||
t.string "user_subscription_sourceable_type", null: false
|
||||
t.bigint "user_subscription_sourceable_id"
|
||||
t.string "user_subscription_sourceable_type"
|
||||
t.index ["author_id"], name: "index_user_subscriptions_on_author_id"
|
||||
t.index ["subscriber_email"], name: "index_user_subscriptions_on_subscriber_email"
|
||||
t.index ["subscriber_id", "subscriber_email", "user_subscription_sourceable_type", "user_subscription_sourceable_id"], name: "index_subscriber_id_and_email_with_user_subscription_source", unique: true
|
||||
|
|
|
|||
|
|
@ -44,6 +44,28 @@ RSpec.describe UserSubscription, type: :model do
|
|||
error = "Can't subscribe with an Apple private relay. Please update email."
|
||||
expect(user_subscription.errors[:subscriber_email]).to include(error)
|
||||
end
|
||||
|
||||
describe "#user_subscription_sourceable" do
|
||||
it "is required on creation" do
|
||||
subscription = described_class.new(
|
||||
user_subscription_sourceable: nil, subscriber: subscriber, subscriber_email: subscriber.email,
|
||||
author: source.user
|
||||
)
|
||||
subscription.save
|
||||
|
||||
expect(subscription).not_to be_valid
|
||||
expect(subscription.errors.messages.keys).to include(
|
||||
:user_subscription_sourceable_id, :user_subscription_sourceable_type
|
||||
)
|
||||
end
|
||||
|
||||
it "can be nulled on update" do
|
||||
subscription = described_class.make(source: source, subscriber: subscriber)
|
||||
subscription.update(user_subscription_sourceable: nil)
|
||||
|
||||
expect(subscription).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#build" do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue