Sha256: 3df6263c1e69b777a5b6775272b77bc0f7deb36d190a17f05206e04e9bc4ffb7

Contents?: true

Size: 1.84 KB

Versions: 12

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module Decidim
  module Amendable
    # A command with all the business logic to reject an amend.
    class Reject < Decidim::Command
      # Public: Initializes the command.
      #
      # form         - A form object with the params.
      # amendable    - The resource that is being amended.
      def initialize(form)
        @form = form
        @amendment = form.amendment
        @amendable = form.amendable
        @emendation = form.emendation
      end

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

        transaction do
          reject_amendment!
          notify_emendation_state_change!
          notify_emendation_authors_and_followers
        end

        broadcast(:ok, @emendation)
      end

      private

      def reject_amendment!
        @amendment = Decidim.traceability.update!(
          @amendment,
          @amendable.creator_author,
          { state: "rejected" },
          visibility: "public-only"
        )
      end

      def notify_emendation_state_change!
        @emendation.process_amendment_state_change!
      end

      def notify_emendation_authors_and_followers
        affected_users = @emendation.authors + @amendable.notifiable_identities
        followers = @emendation.followers + @amendable.followers - affected_users

        Decidim::EventsManager.publish(
          event: "decidim.events.amendments.amendment_rejected",
          event_class: Decidim::Amendable::AmendmentRejectedEvent,
          resource: @emendation,
          affected_users: affected_users.uniq,
          followers: followers.uniq
        )
      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/reject.rb
decidim-core-0.27.8 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.7 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.6 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.5 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.4 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.3 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.2 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.1 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.0 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.0.rc2 app/commands/decidim/amendable/reject.rb
decidim-core-0.27.0.rc1 app/commands/decidim/amendable/reject.rb