Sha256: e4408e85983d3bdc49dd0b47f10bbd9df86c3ceecb9febe71224e50d3b81c95f

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Decidim
  module Amendable
    # A command with all the business logic when a user updates and amendment draft.
    class UpdateDraft < Rectify::Command
      # Public: Initializes the command.
      #
      # form         - A form object with the params.
      def initialize(form)
        @form = form
        @amendment = form.amendment
        @amender = form.amender
        @emendation = form.emendation
        @current_user = form.current_user
        @user_group = Decidim::UserGroup.find_by(id: form.user_group_id)
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the amend.
      # - :invalid if the amendment is not a draft.
      # - :invalid if the form isn't valid or the amender is not the current user.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) unless form.valid? && amendment.draft? && amender == current_user

        transaction do
          update_draft
        end

        broadcast(:ok, @amendment)
      end

      private

      attr_reader :form, :amendment, :amender, :emendation, :current_user, :user_group

      # Prevent PaperTrail from creating an additional version
      # in the amendment multi-step creation process (step 3: complete)
      #
      # A first version will be created in step 4: publish
      # for diff rendering in the amendment control version
      def update_draft
        PaperTrail.request(enabled: false) do
          emendation.assign_attributes(form.emendation_params)
          emendation.add_author(current_user, user_group)
          emendation.save!
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.22.0 app/commands/decidim/amendable/update_draft.rb
decidim-core-0.21.0 app/commands/decidim/amendable/update_draft.rb
decidim-core-0.20.1 app/commands/decidim/amendable/update_draft.rb
decidim-core-0.20.0 app/commands/decidim/amendable/update_draft.rb
decidim-core-0.19.1 app/commands/decidim/amendable/update_draft.rb
decidim-core-0.19.0 app/commands/decidim/amendable/update_draft.rb