Refactor CreatorSettingsForm & spec (#19011)

This commit is contained in:
Mac Siri 2023-01-27 08:39:07 -05:00 committed by GitHub
parent 909fd7f103
commit 8e76bc046b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 35 deletions

View file

@ -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

View file

@ -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

View file

@ -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