From 9794305412241f59c266c2331e90c49135f1548c Mon Sep 17 00:00:00 2001 From: Michael Kohl Date: Fri, 16 Jul 2021 20:45:12 +0700 Subject: [PATCH] Remove unused profile fields (#14206) * Remove unused profile fields * Clean up CSV * Account for user settings in Profiles::Update * Move brand color validation to Users::Setting * Fix specs --- app/models/users/setting.rb | 5 ++++ app/services/profiles/update.rb | 10 ++++--- app/validators/profile_validator.rb | 8 ++--- app/views/users/_profile.html.erb | 2 +- lib/data/dev_profile_fields.csv | 3 -- ...0712054300_remove_unused_profile_fields.rb | 25 ++++++++++++++++ .../create_profile_fields_spec.rb | 4 +-- spec/models/profile_spec.rb | 29 ------------------- spec/models/users/setting_spec.rb | 29 +++++++++++++++++++ spec/services/profiles/update_spec.rb | 4 +-- 10 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 lib/data_update_scripts/20210712054300_remove_unused_profile_fields.rb diff --git a/app/models/users/setting.rb b/app/models/users/setting.rb index 893c2717a..a998f66d3 100644 --- a/app/models/users/setting.rb +++ b/app/models/users/setting.rb @@ -2,6 +2,8 @@ module Users class Setting < ApplicationRecord self.table_name_prefix = "users_" + HEX_COLOR_REGEXP = /\A#?(?:\h{6}|\h{3})\z/.freeze + belongs_to :user, touch: true scope :with_feed, -> { where.not(feed_url: [nil, ""]) } @@ -13,6 +15,9 @@ module Users enum config_theme: { default: 0, minimal_light_theme: 1, night_theme: 2, pink_theme: 3, ten_x_hacker_theme: 4 } + validates :brand_color1, + :brand_color2, + format: { with: HEX_COLOR_REGEXP, message: "is not a valid hex color" } validates :user_id, presence: true validates :experience_level, numericality: { less_than_or_equal_to: 10 }, allow_blank: true validates :feed_referential_link, inclusion: { in: [true, false] } diff --git a/app/services/profiles/update.rb b/app/services/profiles/update.rb index 4c418d49f..77b2f5b74 100644 --- a/app/services/profiles/update.rb +++ b/app/services/profiles/update.rb @@ -4,8 +4,9 @@ module Profiles using HashAnyKey include ImageUploads - CORE_PROFILE_FIELDS = %i[summary brand_color1 brand_color2].freeze + CORE_PROFILE_FIELDS = %i[summary].freeze CORE_USER_FIELDS = %i[name username profile_image].freeze + CORE_SETTINGS_FIELDS = %i[brand_color1 brand_color2].freeze # @param user [User] the user whose profile we are updating # @param updated_attributes [Hash>] the profile @@ -112,15 +113,16 @@ module Profiles end def conditionally_resave_articles - return unless core_profile_details_changed? && !@user.suspended? + return unless resave_articles? && !@user.suspended? Users::ResaveArticlesWorker.perform_async(@user.id) end - def core_profile_details_changed? + def resave_articles? user_fields = CORE_USER_FIELDS + Authentication::Providers.username_fields @updated_user_attributes.any_key?(user_fields) || - @updated_profile_attributes.any_key?(CORE_PROFILE_FIELDS) + @updated_profile_attributes.any_key?(CORE_PROFILE_FIELDS) || + @updated_users_setting_attributes.any_key?(CORE_SETTINGS_FIELDS) end end end diff --git a/app/validators/profile_validator.rb b/app/validators/profile_validator.rb index 23b19bf93..678b8146d 100644 --- a/app/validators/profile_validator.rb +++ b/app/validators/profile_validator.rb @@ -5,10 +5,7 @@ class ProfileValidator < ActiveModel::Validator MAX_TEXT_AREA_LENGTH = 200 MAX_TEXT_FIELD_LENGTH = 100 - HEX_COLOR_REGEXP = /^#?(?:\h{6}|\h{3})$/.freeze - ERRORS = { - color_field: "is not a valid hex color", text_area: "is too long (maximum is #{MAX_TEXT_AREA_LENGTH} characters)", text_field: "is too long (maximum is #{MAX_TEXT_FIELD_LENGTH} characters)" }.with_indifferent_access.freeze @@ -45,9 +42,8 @@ class ProfileValidator < ActiveModel::Validator true # checkboxes are always valid end - def color_field_valid?(record, attribute) - hex_value = record.public_send(attribute) - hex_value.nil? || hex_value.match?(HEX_COLOR_REGEXP) + def color_field_valid?(_record, _attribute) + true # we do not currently validate color fields here end def text_area_valid?(record, attribute) diff --git a/app/views/users/_profile.html.erb b/app/views/users/_profile.html.erb index e5a7e6321..fee68612b 100644 --- a/app/views/users/_profile.html.erb +++ b/app/views/users/_profile.html.erb @@ -58,7 +58,7 @@
<%= f.check_box "profile[display_email_on_profile]", - checked: profile.display_email_on_profile, + checked: @user.setting.display_email_on_profile, class: "crayons-checkbox" %>