Sha256: 2cc7d49683a79ac7cd944381ba85efd1a8e51ad05489c8e0585771dc727ef8fe

Contents?: true

Size: 1.76 KB

Versions: 22

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A command with all the business logic when creating a new participatory
    # process group in the system.
    class UpdateParticipatoryProcessGroup < Rectify::Command
      # Public: Initializes the command.
      #
      # participatory_process_group - the ParticipatoryProcessGroup to update
      # form - A form object with the params.
      def initialize(participatory_process_group, form)
        @participatory_process_group = participatory_process_group
        @form = form
      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?
        update_participatory_process_group

        if @participatory_process_group.valid?
          broadcast(:ok, @participatory_process_group)
        else
          form.errors.add(:hero_image, @participatory_process_group.errors[:hero_image]) if @participatory_process_group.errors.include? :hero_image
          broadcast(:invalid)
        end
      end

      private

      attr_reader :form

      def update_participatory_process_group
        @participatory_process_group.assign_attributes(attributes)
        @participatory_process_group.save! if @participatory_process_group.valid?
      end

      def attributes
        {
          name: form.name,
          hero_image: form.hero_image,
          description: form.description,
          participatory_processes: participatory_processes
        }
      end

      def participatory_processes
        Decidim::ParticipatoryProcess.where(id: form.participatory_process_ids)
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
decidim-admin-0.5.1 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.5.1 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb
decidim-admin-0.5.0 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.5.0 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb
decidim-admin-0.4.4 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.4.4 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb
decidim-admin-0.4.3 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-admin-0.4.2 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-admin-0.4.1 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.4.3 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.4.2 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.4.1 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb
decidim-admin-0.4.0 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.4.0 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb
decidim-admin-0.3.2 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.3.2 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb
decidim-admin-0.3.1 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.3.1 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb
decidim-admin-0.3.0 app/commands/decidim/admin/update_participatory_process_group.rb
decidim-0.3.0 decidim-admin/app/commands/decidim/admin/update_participatory_process_group.rb