Sha256: 24438f3c1233a748174a2ea8bd270f1bfdfc3a5ffbef62e65087479bbb18c51e

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic when an admin imports proposals from
      # a participatory text.
      class PublishParticipatoryText < UpdateParticipatoryText
        # 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)
            publish_drafts
          end

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

        private

        attr_reader :form

        def publish_drafts
          Decidim::Proposals::Proposal.where(component: form.current_component).drafts.find_each do |proposal|
            add_failure(proposal) unless proposal.update(published_at: Time.current)
          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/publish_participatory_text.rb
decidim-proposals-0.17.2 app/commands/decidim/proposals/admin/publish_participatory_text.rb
decidim-proposals-0.18.0 app/commands/decidim/proposals/admin/publish_participatory_text.rb
decidim-proposals-0.17.1 app/commands/decidim/proposals/admin/publish_participatory_text.rb
decidim-proposals-0.16.1 app/commands/decidim/proposals/admin/publish_participatory_text.rb
decidim-proposals-0.17.0 app/commands/decidim/proposals/admin/publish_participatory_text.rb