Sha256: d25a288072d6d05af465d7d8e9dfe64a27d898ed186a01ec4e9ebff280d54d71

Contents?: true

Size: 1.87 KB

Versions: 12

Compression:

Stored size: 1.87 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 < Decidim::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.title = { I18n.locale => form.emendation_params.with_indifferent_access[:title] }
          emendation.body = { I18n.locale => form.emendation_params.with_indifferent_access[:body] }
          emendation.add_author(current_user, user_group)
          emendation.save!
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

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