Sha256: 7ee78146a75e663d89a586c61b0b74df03e5d8ce4b8f4488a68ba8305d2ad506

Contents?: true

Size: 1.54 KB

Versions: 10

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true
module Decidim
  module System
    # A command with all the business logic when creating a new organization in
    # the system. It creates the organization and invites the admin to the
    # system.
    class RegisterOrganization < Rectify::Command
      # Public: Initializes the command.
      #
      # form - A form object with the params.
      def initialize(form)
        @form = form
      end

      # Executes the command. Braodcasts these events:
      #
      # - :ok when everything is valid.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if form.invalid?

        transaction do
          organization = create_organization
          invite_admin(organization)
        end

        broadcast(:ok)
      rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
        broadcast(:invalid)
      end

      private

      attr_reader :form

      def create_organization
        Decidim::Organization.create!(
          name: form.name,
          host: form.host,
          description: form.description
        )
      end

      def invite_admin(organization)
        Decidim::User.invite!(
          {
            name: form.organization_admin_name,
            email: form.organization_admin_email,
            organization: organization,
            roles: ["admin"]
          },
          nil,
          invitation_instructions: "organization_admin_invitation_instructions"
        )
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
decidim-system-0.0.1.alpha9 app/commands/decidim/system/register_organization.rb
decidim-0.0.1.alpha9 decidim-system/app/commands/decidim/system/register_organization.rb
decidim-system-0.0.1.alpha8 app/commands/decidim/system/register_organization.rb
decidim-0.0.1.alpha8 decidim-system/app/commands/decidim/system/register_organization.rb
decidim-system-0.0.1.alpha7 app/commands/decidim/system/register_organization.rb
decidim-0.0.1.alpha7 decidim-system/app/commands/decidim/system/register_organization.rb
decidim-system-0.0.1.alpha6 app/commands/decidim/system/register_organization.rb
decidim-0.0.1.alpha6 decidim-system/app/commands/decidim/system/register_organization.rb
decidim-system-0.0.1.alpha5 app/commands/decidim/system/register_organization.rb
decidim-0.0.1.alpha5 decidim-system/app/commands/decidim/system/register_organization.rb