Sha256: e53809d0f2574356fd7c373f9c6957190ed3fde48fa916fd8a927648f1cb8f57

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 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 < Decidim::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 was not valid and we could not proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if initiative.published?

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

        private

        attr_reader :initiative, :current_user

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-initiatives-0.29.1 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.28.4 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.29.0 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.28.3 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.29.0.rc4 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.29.0.rc3 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.29.0.rc2 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.29.0.rc1 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.28.2 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.28.1 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.28.0 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.28.0.rc5 app/commands/decidim/initiatives/admin/publish_initiative.rb
decidim-initiatives-0.28.0.rc4 app/commands/decidim/initiatives/admin/publish_initiative.rb