From 8e76bc046bdc00ae7f79f196561c62f2423ab7d8 Mon Sep 17 00:00:00 2001 From: Mac Siri Date: Fri, 27 Jan 2023 08:39:07 -0500 Subject: [PATCH] Refactor CreatorSettingsForm & spec (#19011) --- .../admin/creator_settings_controller.rb | 3 +- app/forms/creator_settings_form.rb | 33 ++++++++----------- spec/forms/creator_settings_form_spec.rb | 25 +++++++------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/app/controllers/admin/creator_settings_controller.rb b/app/controllers/admin/creator_settings_controller.rb index bd99da2bd..31fdfba43 100644 --- a/app/controllers/admin/creator_settings_controller.rb +++ b/app/controllers/admin/creator_settings_controller.rb @@ -26,9 +26,8 @@ module Admin checked_code_of_conduct: @creator_settings_form.checked_code_of_conduct, checked_terms_and_conditions: @creator_settings_form.checked_terms_and_conditions, ) - @creator_settings_form.save - if @creator_settings_form.success + if @creator_settings_form.save current_user.update!(saw_onboarding: true) redirect_to root_path else diff --git a/app/forms/creator_settings_form.rb b/app/forms/creator_settings_form.rb index 43cc173f2..4a70d03db 100644 --- a/app/forms/creator_settings_form.rb +++ b/app/forms/creator_settings_form.rb @@ -10,8 +10,7 @@ class CreatorSettingsForm attribute :primary_brand_color_hex, :string attribute :public, :boolean - validates :community_name, - :primary_brand_color_hex, presence: true + validates :community_name, :primary_brand_color_hex, presence: true validates :checked_code_of_conduct, inclusion: { in: [true, false] } validates :checked_terms_and_conditions, inclusion: { in: [true, false] } @@ -21,26 +20,20 @@ class CreatorSettingsForm attr_accessor :success def save - if valid? - begin - ::Settings::Community.community_name = community_name - ::Settings::UserExperience.primary_brand_color_hex = primary_brand_color_hex - ::Settings::Authentication.invite_only_mode = invite_only_mode - ::Settings::UserExperience.public = public + ::Settings::Community.community_name = community_name + ::Settings::UserExperience.primary_brand_color_hex = primary_brand_color_hex + ::Settings::Authentication.invite_only_mode = invite_only_mode + ::Settings::UserExperience.public = public - if logo - logo_uploader = upload_logo(logo) - ::Settings::General.original_logo = logo_uploader.url - ::Settings::General.resized_logo = logo_uploader.resized_logo.url - end - @success = true - rescue StandardError => e - errors.add(:base, e.message) - @success = false - end - else - @success = false + if logo + logo_uploader = upload_logo(logo) + ::Settings::General.original_logo = logo_uploader.url + ::Settings::General.resized_logo = logo_uploader.resized_logo.url end + @success = true + rescue StandardError => e + errors.add(:base, e.message) + @success = false end private diff --git a/spec/forms/creator_settings_form_spec.rb b/spec/forms/creator_settings_form_spec.rb index 079dfd282..e36739095 100644 --- a/spec/forms/creator_settings_form_spec.rb +++ b/spec/forms/creator_settings_form_spec.rb @@ -33,31 +33,31 @@ RSpec.describe CreatorSettingsForm, type: :model do describe "#save" do let(:current_user) { create(:user) } + let(:form_data) do + { checked_code_of_conduct: true, + checked_terms_and_conditions: true, + community_name: "Climbing Life", + invite_only_mode: false, + public: false, + logo: "logo.png", + primary_brand_color_hex: "#a81adb" } + end after do - # prevent changes here from leaking into other tests Settings::Community.clear_cache Settings::UserExperience.clear_cache Settings::Authentication.clear_cache end - # rubocop:disable RSpec/ExampleLength - it "saves the updated attributes to the correct Settings values", :aggregate_failures do + it "saves the updated attributes to the correct Settings values" do # NOTE: override the profile migration hack from rails_helper.rb # TODO: remove this once we remove it in rails_helper.rb allow(Settings::UserExperience).to receive(:public).and_call_original - creator_settings_form = described_class.new( - checked_code_of_conduct: true, - checked_terms_and_conditions: true, - community_name: "Climbing Life", - invite_only_mode: false, - public: false, - primary_brand_color_hex: "#a81adb", - ) + creator_settings_form = described_class.new(form_data) expect(creator_settings_form.valid?).to be(true) - creator_settings_form.save + creator_settings_form.save expect(creator_settings_form.success).to be(true) expect(Settings::Community.community_name).to eq("Climbing Life") expect(Settings::UserExperience.primary_brand_color_hex).to eq("#a81adb") @@ -66,6 +66,5 @@ RSpec.describe CreatorSettingsForm, type: :model do expect(current_user.checked_code_of_conduct).to be(true) expect(current_user.checked_terms_and_conditions).to be(true) end - # rubocop:enable RSpec/ExampleLength end end