Sha256: 4259c60038140ad1a69cf483b9febbfa4571fa0f7b03a26b16a20af8350d96f7

Contents?: true

Size: 1.82 KB

Versions: 26

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Decidim
  module Assemblies
    module Admin
      # A command with all the business logic when creating a new assembly
      # member in the system.
      class CreateAssemblyMember < Rectify::Command
        # Public: Initializes the command.
        #
        # form - A form object with the params.
        # assembly - The Assembly that will hold the member
        def initialize(form, current_user, assembly)
          @form = form
          @current_user = current_user
          @assembly = assembly
        end

        # Executes the command. Broadcasts 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
            create_assembly_member!
          end

          broadcast(:ok)
        end

        private

        attr_reader :form, :assembly, :current_user

        def create_assembly_member!
          log_info = {
            resource: {
              title: form.full_name
            },
            participatory_space: {
              title: assembly.title
            }
          }

          @assembly_member = Decidim.traceability.create!(
            Decidim::AssemblyMember,
            current_user,
            form.attributes.slice(
              :full_name,
              :gender,
              :birthday,
              :birthplace,
              :ceased_date,
              :designation_date,
              :designation_mode,
              :position,
              :position_other,
              :weight
            ).merge(
              assembly: assembly,
              user: form.user
            ),
            log_info
          )
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
decidim-assemblies-0.12.2 app/commands/decidim/assemblies/admin/create_assembly_member.rb
decidim-assemblies-0.13.0 app/commands/decidim/assemblies/admin/create_assembly_member.rb
decidim-assemblies-0.12.1 app/commands/decidim/assemblies/admin/create_assembly_member.rb
decidim-assemblies-0.13.0.pre1 app/commands/decidim/assemblies/admin/create_assembly_member.rb
decidim-assemblies-0.12.0 app/commands/decidim/assemblies/admin/create_assembly_member.rb
decidim-assemblies-0.12.0.pre app/commands/decidim/assemblies/admin/create_assembly_member.rb