From 6edb476acdaaf1bb67b8640fc892b93d3ab5d0c2 Mon Sep 17 00:00:00 2001 From: Michael Kohl Date: Fri, 31 Jul 2020 20:59:48 +0700 Subject: [PATCH] [deploy] Remove UserOptionalField (#9579) * Remove UserOptionalField * Remove optional fields from user and users factory * Remove optional fields from user spec --- app/models/user.rb | 1 - app/models/user_optional_field.rb | 13 ----------- ...0200731033002_drop_user_optional_fields.rb | 22 +++++++++++++++++++ db/schema.rb | 13 +---------- spec/factories/user_optional_fields.rb | 7 ------ spec/factories/users.rb | 8 ------- spec/models/user_optional_field_spec.rb | 14 ------------ spec/models/user_spec.rb | 8 ------- 8 files changed, 23 insertions(+), 63 deletions(-) delete mode 100644 app/models/user_optional_field.rb create mode 100644 db/migrate/20200731033002_drop_user_optional_fields.rb delete mode 100644 spec/factories/user_optional_fields.rb delete mode 100644 spec/models/user_optional_field_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb index 7fe69ae30..cf6dd679a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user_optional_field.rb b/app/models/user_optional_field.rb deleted file mode 100644 index 07d8f127d..000000000 --- a/app/models/user_optional_field.rb +++ /dev/null @@ -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 diff --git a/db/migrate/20200731033002_drop_user_optional_fields.rb b/db/migrate/20200731033002_drop_user_optional_fields.rb new file mode 100644 index 000000000..c66dcd672 --- /dev/null +++ b/db/migrate/20200731033002_drop_user_optional_fields.rb @@ -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 + diff --git a/db/schema.rb b/db/schema.rb index de41e1a16..b3f5c3041 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/factories/user_optional_fields.rb b/spec/factories/user_optional_fields.rb deleted file mode 100644 index c2daec856..000000000 --- a/spec/factories/user_optional_fields.rb +++ /dev/null @@ -1,7 +0,0 @@ -FactoryBot.define do - factory :user_optional_field do - user - label { "Pronoun" } - value { "They/them" } - end -end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index e11365331..20917e838 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -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" } diff --git a/spec/models/user_optional_field_spec.rb b/spec/models/user_optional_field_spec.rb deleted file mode 100644 index 255b843f9..000000000 --- a/spec/models/user_optional_field_spec.rb +++ /dev/null @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 585d2bd0e..3ed5e475d 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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) }