Sha256: 022e86ee6ea6f2461dfc81771f95e00127fbf6e391f625885932203f4f11f6a4

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true
module Decidim
  module Admin
    # A command that sets a step in a participatory process as active (and
    # unsets a previous active step)
    class ActivateParticipatoryProcessStep < Rectify::Command
      # Public: Initializes the command.
      #
      # step - A ParticipatoryProcessStep that will be activated
      def initialize(step)
        @step = step
      end

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

        Decidim::ParticipatoryProcessStep.transaction do
          deactivate_active_steps
          activate_step
        end
        broadcast(:ok)
      end

      private

      attr_reader :step

      def deactivate_active_steps
        step.participatory_process.steps.where(active: true).update_all(active: false)
      end

      def activate_step
        step.update_attribute(:active, true)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
decidim-admin-0.0.1.alpha9 app/commands/decidim/admin/activate_participatory_process_step.rb
decidim-0.0.1.alpha9 decidim-admin/app/commands/decidim/admin/activate_participatory_process_step.rb
decidim-admin-0.0.1.alpha8 app/commands/decidim/admin/activate_participatory_process_step.rb
decidim-0.0.1.alpha8 decidim-admin/app/commands/decidim/admin/activate_participatory_process_step.rb