Sha256: c48c5ad15a4cfcc7d5bfb23c1ab4a5af54adf828b3d31c322bbb89c539bf6015

Contents?: true

Size: 1.71 KB

Versions: 12

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # A command with all the business logic when a user updates a collaborative_draft.
    class UpdateCollaborativeDraft < Decidim::Command
      include HashtagsMethods

      # Public: Initializes the command.
      #
      # form         - A form object with the params.
      # current_user - The current user.
      # collaborative_draft - the collaborative_draft to update.
      def initialize(form, current_user, collaborative_draft)
        @form = form
        @current_user = current_user
        @collaborative_draft = collaborative_draft
      end

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

        transaction do
          update_collaborative_draft
        end

        broadcast(:ok, collaborative_draft)
      end

      private

      attr_reader :form, :collaborative_draft, :current_user

      def update_collaborative_draft
        Decidim.traceability.update!(
          @collaborative_draft,
          @current_user,
          attributes,
          visibility: "public-only"
        )
      end

      def attributes
        {
          title: title_with_hashtags,
          body: body_with_hashtags,
          category: form.category,
          scope: form.scope,
          address: form.address,
          latitude: form.latitude,
          longitude: form.longitude
        }
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-proposals-0.27.9 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.8 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.7 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.6 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.5 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.4 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.3 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.2 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.1 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.0 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.0.rc2 app/commands/decidim/proposals/update_collaborative_draft.rb
decidim-proposals-0.27.0.rc1 app/commands/decidim/proposals/update_collaborative_draft.rb