Sha256: fba13e27cc352b8c931ea06b63d18e78086d74f8146f15f0c68235e28ef892e7

Contents?: true

Size: 1.24 KB

Versions: 73

Compression:

Stored size: 1.24 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]

      validates :name, presence: true
      validates :host, presence: true
      validate :validate_organization_uniqueness

      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

73 entries across 73 versions & 2 rubygems

Version Path
decidim-system-0.15.2 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.15.1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.15.0 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.14.4 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.14.3 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.14.2 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.14.1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.13.1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.12.2 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.13.0 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.12.1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.13.0.pre1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.12.0 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.11.2 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.12.0.pre app/forms/decidim/system/update_organization_form.rb
decidim-system-0.11.1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.11.0.pre1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.10.1 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.10.0 app/forms/decidim/system/update_organization_form.rb
decidim-system-0.9.3 app/forms/decidim/system/update_organization_form.rb