[deploy] Remove UserOptionalField (#9579)

* Remove UserOptionalField

* Remove optional fields from user and users factory

* Remove optional fields from user spec
This commit is contained in:
Michael Kohl 2020-07-31 20:59:48 +07:00 committed by GitHub
parent f940b1fdc5
commit 6edb476acd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 23 additions and 63 deletions

View file

@ -92,7 +92,6 @@ class User < ApplicationRecord
has_many :messages, dependent: :destroy
has_many :notes, as: :noteable, inverse_of: :noteable
has_many :notification_subscriptions, dependent: :destroy
has_many :user_optional_fields, dependent: :destroy
has_many :notifications, dependent: :destroy
has_many :offender_feedback_messages, class_name: "FeedbackMessage",
inverse_of: :offender, foreign_key: :offender_id, dependent: :nullify

View file

@ -1,13 +0,0 @@
class UserOptionalField < ApplicationRecord
belongs_to :user
validates :label, presence: true, length: { maximum: 30 }, uniqueness: { scope: :user_id }
validates :value, presence: true, length: { maximum: 128 }
validate :validate_quota
def validate_quota
return unless user
errors.add(:user_id, "already has the maximum allowable optional fields") unless user.user_optional_fields.count < 3
end
end

View file

@ -0,0 +1,22 @@
class DropUserOptionalFields < ActiveRecord::Migration[6.0]
INDEX = [:user_optional_fields, [:label, :user_id], unique: true].freeze
def up
drop_table :user_optional_fields
remove_index(*INDEX) if index_exists?(*INDEX)
end
def down
create_table :user_optional_fields do |t|
t.string :label, null: false
t.string :value, null: false
t.references :user, foreign_key: true, null: false
t.timestamps
end
add_index(*INDEX) unless index_exists?(*INDEX)
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_07_27_163200) do
ActiveRecord::Schema.define(version: 2020_07_31_033002) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
@ -1121,16 +1121,6 @@ ActiveRecord::Schema.define(version: 2020_07_27_163200) do
t.index ["blocked_id", "blocker_id"], name: "index_user_blocks_on_blocked_id_and_blocker_id", unique: true
end
create_table "user_optional_fields", force: :cascade do |t|
t.datetime "created_at", null: false
t.string "label", null: false
t.datetime "updated_at", null: false
t.bigint "user_id", null: false
t.string "value", null: false
t.index ["label", "user_id"], name: "index_user_optional_fields_on_label_and_user_id", unique: true
t.index ["user_id"], name: "index_user_optional_fields_on_user_id"
end
create_table "user_subscriptions", force: :cascade do |t|
t.bigint "author_id", null: false
t.datetime "created_at", precision: 6, null: false
@ -1352,7 +1342,6 @@ ActiveRecord::Schema.define(version: 2020_07_27_163200) do
add_foreign_key "tag_adjustments", "users", on_delete: :cascade
add_foreign_key "user_blocks", "users", column: "blocked_id"
add_foreign_key "user_blocks", "users", column: "blocker_id"
add_foreign_key "user_optional_fields", "users"
add_foreign_key "user_subscriptions", "users", column: "author_id"
add_foreign_key "user_subscriptions", "users", column: "subscriber_id"
add_foreign_key "users_roles", "users", on_delete: :cascade

View file

@ -1,7 +0,0 @@
FactoryBot.define do
factory :user_optional_field do
user
label { "Pronoun" }
value { "They/them" }
end
end

View file

@ -139,14 +139,6 @@ FactoryBot.define do
end
end
trait :with_user_optional_fields do
after(:create) do |user|
create(:user_optional_field, user: user)
create(:user_optional_field, user: user, label: "another field1", value: "another value1")
create(:user_optional_field, user: user, label: "another field2", value: "another value2")
end
end
trait :with_all_info do
education { "DEV University" }
employment_title { "Software Engineer" }

View file

@ -1,14 +0,0 @@
require "rails_helper"
RSpec.describe UserOptionalField, type: :model do
subject { user_optional_field }
let(:user_optional_field) { create(:user_optional_field) }
it { is_expected.to belong_to(:user) }
it { is_expected.to validate_presence_of(:label) }
it { is_expected.to validate_uniqueness_of(:label).scoped_to(:user_id) }
it { is_expected.to validate_length_of(:label).is_at_most(30) }
it { is_expected.to validate_presence_of(:value) }
it { is_expected.to validate_length_of(:value).is_at_most(128) }
end

View file

@ -12,7 +12,6 @@ end
RSpec.describe User, type: :model do
let(:user) { create(:user) }
let(:other_user) { create(:user) }
let(:user_with_user_optional_fields) { create(:user, :with_user_optional_fields) }
let(:org) { create(:organization) }
before { omniauth_mock_providers_payload }
@ -133,13 +132,6 @@ RSpec.describe User, type: :model do
end
# rubocop:enable RSpec/NamedSubject
it "has at most three optional fields" do
expect(user_with_user_optional_fields).to have_many(:user_optional_fields).dependent(:destroy)
fourth_field = user_with_user_optional_fields.user_optional_fields.create(label: "some field",
value: "some value")
expect(fourth_field).not_to be_valid
end
it { is_expected.not_to allow_value("#xyz").for(:bg_color_hex) }
it { is_expected.not_to allow_value("#xyz").for(:text_color_hex) }
it { is_expected.not_to allow_value("AcMe_1%").for(:username) }