Sha256: a29498cf7a8772a845e9dc8f374f1a8c090379ac09adba0b9546c819a8c753e7

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require "decidim/translatable_attributes"

module Decidim
  module System
    # A form object used to update organizations from the system dashboard.
    #
    class UpdateOrganizationForm < Form
      include TranslatableAttributes

      mimic :organization

      attribute :name, String
      attribute :host, String
      attribute :secondary_hosts, String
      attribute :available_authorizations, Array[String]
      attribute :users_registration_mode, String

      validates :name, :host, :users_registration_mode, presence: true
      validate :validate_organization_uniqueness
      validates :users_registration_mode, inclusion: { in: Decidim::Organization.users_registration_modes }

      def map_model(model)
        self.secondary_hosts = model.secondary_hosts.join("\n")
      end

      def clean_secondary_hosts
        return unless secondary_hosts
        secondary_hosts.split("\n").map(&:chomp).select(&:present?)
      end

      def clean_available_authorizations
        return unless available_authorizations
        available_authorizations.select(&:present?)
      end

      private

      def validate_organization_uniqueness
        errors.add(:name, :taken) if Decidim::Organization.where(name: name).where.not(id: id).exists?
        errors.add(:host, :taken) if Decidim::Organization.where(host: host).where.not(id: id).exists?
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-system-0.17.2 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.17.1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.16.1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.17.0 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.16.0 app/forms/decidim/system/update_organization_form.rb