Sha256: 42d3b932048dd78ea1b3883fa0dbadb863daa730014cc8ae607c8db15e1ec12c

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic when an admin updates participatory text proposals.
      class UpdateParticipatoryText < Rectify::Command
        # Public: Initializes the command.
        #
        # form - A PreviewParticipatoryTextForm form object with the params.
        def initialize(form)
          @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
          transaction do
            @failures = {}
            update_contents_and_resort_proposals(form)
          end

          if @failures.any?
            broadcast(:invalid, @failures)
          else
            broadcast(:ok)
          end
        end

        private

        attr_reader :form

        def update_contents_and_resort_proposals(form)
          form.proposals.each do |prop_form|
            proposal = Decidim::Proposals::Proposal.where(component: form.current_component).find(prop_form.id)
            proposal.set_list_position(prop_form.position) if proposal.position != prop_form.position
            proposal.title = prop_form.title
            proposal.body = prop_form.body if proposal.participatory_text_level == Decidim::Proposals::ParticipatoryTextSection::LEVELS[:article]

            add_failure(proposal) unless proposal.save
          end
          raise ActiveRecord::Rollback if @failures.any?
        end

        def add_failure(proposal)
          @failures[proposal.id] = proposal.errors.full_messages
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-proposals-0.18.1 app/commands/decidim/proposals/admin/update_participatory_text.rb
decidim-proposals-0.17.2 app/commands/decidim/proposals/admin/update_participatory_text.rb
decidim-proposals-0.18.0 app/commands/decidim/proposals/admin/update_participatory_text.rb
decidim-proposals-0.17.1 app/commands/decidim/proposals/admin/update_participatory_text.rb
decidim-proposals-0.16.1 app/commands/decidim/proposals/admin/update_participatory_text.rb
decidim-proposals-0.17.0 app/commands/decidim/proposals/admin/update_participatory_text.rb