Sha256: 14a02f36ce933ca8b8afe3e0c1cf756879e17ce5b1da50432b8120f03c136534

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic to unendorse a resource, both as a user or as a user_group.
  class UnendorseResource < Decidim::Command
    # Public: Initializes the command.
    #
    # resource     - A Decidim::Endorsable object.
    # current_user - The current user.
    # current_group- (optional) The current_group that is unendorsing the Resource.
    def initialize(resource, current_user, current_group = nil)
      @resource = resource
      @current_user = current_user
      @current_group = current_group
    end

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

    private

    def destroy_resource_endorsement
      query = if @current_group.present?
                @resource.endorsements.where(decidim_user_group_id: @current_group&.id)
              else
                @resource.endorsements.where(author: @current_user, decidim_user_group_id: nil)
              end
      query.destroy_all
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-core-0.27.4 app/commands/decidim/unendorse_resource.rb
decidim-core-0.27.3 app/commands/decidim/unendorse_resource.rb
decidim-core-0.27.2 app/commands/decidim/unendorse_resource.rb