Sha256: 6efd3f1207ee8229183ffa7678ae45f83f3d6b91b475006e33e7479f6b508e4a

Contents?: true

Size: 1.55 KB

Versions: 13

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Decidim
  module ParticipatoryProcesses
    module Admin
      # A command that reorders the steps in a participatory process.
      class ReorderParticipatoryProcessSteps < Decidim::Command
        # Public: Initializes the command.
        #
        # collection - an ActiveRecord::Relation of steps
        # order - an Array holding the order of IDs of steps
        def initialize(collection, order)
          @collection = collection
          @order = order
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if the data was not valid and we could not proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if order.blank?

          reorder_steps
          broadcast(:ok)
        end

        private

        attr_reader :collection

        def reorder_steps
          data = order.each_with_index.inject({}) do |hash, (id, index)|
            hash.update(id => { position: index })
          end

          # rubocop:disable Rails/SkipsModelValidations
          ParticipatoryProcessStep.transaction do
            collection.update_all(position: nil)
            collection.reload
            collection.update(data.keys, data.values)
            collection.each(&:save!)
          end
          # rubocop:enable Rails/SkipsModelValidations
        end

        def order
          return nil unless @order.is_a?(Array) && @order.present?

          @order
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-participatory_processes-0.29.1 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.28.4 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.29.0 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.28.3 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.29.0.rc4 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.29.0.rc3 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.29.0.rc2 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.29.0.rc1 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.28.2 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.28.1 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.28.0 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.28.0.rc5 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.28.0.rc4 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb