docbrown/app/forms/creator_settings_form.rb
Ridhwana e250b46ed2
Using a Form Object that will persist for the Creator Settings Form (#15684)
* WIP: add a creatore settings form

* WIP: updat the controller to use the Creator Settings FOREM

* feat: use the creator settings form for the new action

* feat: add some default values for the new action

* a note about form data

* update the initiaize function to set some default values

* feat: update the form to use the model data

* feat: permit adn use the attributes within creator_settings_form

* update the flash error

* refactor: require and permit parameters

* chore: use booleans, set defaults and validate the form

* spec: update all the creator_settings tests

* chore: remove comment

* refactor: use self

* feat: aggregate failures'

* chore: remove the logo uploader in the controller

* refactor: update error handling

* feat: update the wasy the controller handles success and error

* chore: remove the resource errors

* feat: show flash message on new line

* fix: use a redirect so that we can get back to /new

* refactor: pass these values through as they seem to be caching whne setting them as default

* chore: change default values

* spec: update tests

* Fix CreatorSettingsForm specs

* fix: use a boolean for public

* spec: add another test for the success var

* fix: radio button labels to correspond + cyress specs

* spec: update based on new changes

* spec: update the params and the expected output

* spec: update the comments and status

* feat: no need for the initialize as we use Active Record Attributes

* feat: update the tac and coc to be persisted when ticked

* fix: amend spec

* blank space

* Message

Co-authored-by: Michael Kohl <me@citizen428.net>
2021-12-10 17:07:40 +02:00

53 lines
1.5 KiB
Ruby

class CreatorSettingsForm
include ActiveModel::Model
include ActiveModel::Attributes
attribute :checked_code_of_conduct, :boolean, default: false
attribute :checked_terms_and_conditions, :boolean, default: false
attribute :community_name, :string
attribute :invite_only_mode, :boolean
attribute :logo
attribute :primary_brand_color_hex, :string
attribute :public, :boolean
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] }
validates :invite_only_mode, inclusion: { in: [true, false] }
validates :public, inclusion: { in: [true, false] }
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
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
end
end
private
def upload_logo(image)
LogoUploader.new.tap do |uploader|
uploader.store!(image)
end
end
end