Sha256: 826e6ba021896c01b3b2f500da236696ecb80e9d8bf49a4bfd4715536e244088

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    module Admin
      # A command with all the business logic that publishes an
      # existing initiative.
      class PublishInitiative < Rectify::Command
        # Public: Initializes the command.
        #
        # initiative - Decidim::Initiative
        # current_user - the user performing the action
        def initialize(initiative, current_user)
          @initiative = initiative
          @current_user = current_user
        end

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

          @initiative = Decidim.traceability.perform_action!(
            :publish,
            initiative,
            current_user
          ) do
            initiative.publish!
            increment_score
            initiative
          end
          broadcast(:ok, initiative)
        end

        private

        attr_reader :initiative, :current_user

        def increment_score
          Decidim::Gamification.increment_score(initiative.author, :initiatives)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-initiatives-0.14.4 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.14.3 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.14.2 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.14.1 app/commands/decidim/initiatives/admin/publish_initiative.rb