Sha256: a258d62af2fef9cacbe2c761d13a5985837c2a3208d3135177f6ba4e0051434c

Contents?: true

Size: 1.54 KB

Versions: 79

Compression:

Stored size: 1.54 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) 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

79 entries across 79 versions & 1 rubygems

Version Path
decidim-participatory_processes-0.26.10 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.9 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.8 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.7 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.5 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.4 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.3 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.2 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.1 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.0 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.0.rc2 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.26.0.rc1 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.25.2 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.25.1 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.25.0 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.25.0.rc4 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.25.0.rc3 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.25.0.rc2 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.25.0.rc1 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb
decidim-participatory_processes-0.24.3 app/commands/decidim/participatory_processes/admin/reorder_participatory_process_steps.rb