Sha256: aec99bdfa5d82a8228065751abcfa86e9482c0f16383d1ab48594559ce3685a2

Contents?: true

Size: 1.24 KB

Versions: 8

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

8 entries across 8 versions & 2 rubygems

Version Path
decidim-system-0.1.0 app/forms/decidim/system/update_organization_form.rb
decidim-0.1.0 decidim-system/app/forms/decidim/system/update_organization_form.rb
decidim-system-0.0.8.1 app/forms/decidim/system/update_organization_form.rb
decidim-0.0.8.1 decidim-system/app/forms/decidim/system/update_organization_form.rb
decidim-system-0.0.7 app/forms/decidim/system/update_organization_form.rb
decidim-0.0.7 decidim-system/app/forms/decidim/system/update_organization_form.rb
decidim-system-0.0.6 app/forms/decidim/system/update_organization_form.rb
decidim-0.0.6 decidim-system/app/forms/decidim/system/update_organization_form.rb