Sha256: 609017fee96512102027907bca0c34831d877a13beeb2e8df3ee10143149f184

Contents?: true

Size: 1.96 KB

Versions: 7

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic when a user updates a proposal.
      class UpdateProposal < Rectify::Command
        include AttachmentMethods
        include HashtagsMethods

        # Public: Initializes the command.
        #
        # form         - A form object with the params.
        # proposal - the proposal to update.
        def initialize(form, proposal)
          @form = form
          @proposal = proposal
          @attached_to = proposal
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid, together with the proposal.
        # - :invalid if the form wasn't valid and we couldn't proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if form.invalid?

          if process_attachments?
            @proposal.attachments.destroy_all

            build_attachment
            return broadcast(:invalid) if attachment_invalid?
          end

          transaction do
            update_proposal
            update_proposal_author
            create_attachment if process_attachments?
          end

          broadcast(:ok, proposal)
        end

        private

        attr_reader :form, :proposal, :attachment

        def update_proposal
          Decidim.traceability.update!(
            proposal,
            form.current_user,
            title: title_with_hashtags,
            body: body_with_hashtags,
            category: form.category,
            scope: form.scope,
            address: form.address,
            latitude: form.latitude,
            longitude: form.longitude,
            created_in_meeting: form.created_in_meeting
          )
        end

        def update_proposal_author
          proposal.coauthorships.clear
          proposal.add_coauthor(form.author)
          proposal.save!
          proposal
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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