Sha256: a21df191dcfe469e0e30c234346c6a92f315938d814f2eec1f9bd6d9d44af83a

Contents?: true

Size: 1.41 KB

Versions: 18

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Decidim
  module ParticipatoryProcesses
    module Admin
      # A command that reorders the steps in a participatory process.
      class ReorderParticipatoryProcessSteps < Rectify::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 wasn't valid and we couldn't proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) unless order.present?

          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

          ParticipatoryProcessStep.transaction do
            collection.update_all(position: nil)
            collection.reload
            collection.update(data.keys, data.values)
          end
        end

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

          @order
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
decidim-participatory_processes-0.6.8 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-0.6.8 decidim-participatory_processes/app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.6.7 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-0.6.7 decidim-participatory_processes/app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.6.6 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-0.6.6 decidim-participatory_processes/app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.6.5 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-0.6.5 decidim-participatory_processes/app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.6.4 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-0.6.4 decidim-participatory_processes/app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.6.3 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-0.6.3 decidim-participatory_processes/app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.6.2 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-0.6.2 decidim-participatory_processes/app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.6.1 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-0.6.1 decidim-participatory_processes/app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.6.0 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-0.6.0 decidim-participatory_processes/app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb