Sha256: afd310787baa42b9975022a32879ab68e7839bda03afa8ef18bc1913f414df20

Contents?: true

Size: 1.06 KB

Versions: 39

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic for when a user stops following a resource.
  class DeleteFollow < Rectify::Command
    # Public: Initializes the command.
    #
    # form         - A form object with the params.
    # current_user - The current user.
    def initialize(form, current_user)
      @form = form
      @current_user = current_user
    end

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

      delete_follow!
      decrement_score

      broadcast(:ok)
    end

    private

    attr_reader :form, :current_user

    def delete_follow!
      form.follow.destroy!
    end

    def decrement_score
      followable = form.follow.followable
      return unless followable.is_a? Decidim::User

      Decidim::Gamification.decrement_score(followable, :followers)
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
decidim-core-0.26.10 app/commands/decidim/delete_follow.rb
decidim-core-0.26.9 app/commands/decidim/delete_follow.rb
decidim-core-0.26.8 app/commands/decidim/delete_follow.rb
decidim-core-0.26.7 app/commands/decidim/delete_follow.rb
decidim-core-0.26.5 app/commands/decidim/delete_follow.rb
decidim-core-0.26.4 app/commands/decidim/delete_follow.rb
decidim-core-0.26.3 app/commands/decidim/delete_follow.rb
decidim-core-0.26.2 app/commands/decidim/delete_follow.rb
decidim-core-0.26.1 app/commands/decidim/delete_follow.rb
decidim-core-0.26.0 app/commands/decidim/delete_follow.rb
decidim-core-0.26.0.rc2 app/commands/decidim/delete_follow.rb
decidim-core-0.26.0.rc1 app/commands/decidim/delete_follow.rb
decidim-core-0.25.2 app/commands/decidim/delete_follow.rb
decidim-core-0.25.1 app/commands/decidim/delete_follow.rb
decidim-core-0.25.0 app/commands/decidim/delete_follow.rb
decidim-core-0.25.0.rc4 app/commands/decidim/delete_follow.rb
decidim-core-0.25.0.rc3 app/commands/decidim/delete_follow.rb
decidim-core-0.25.0.rc2 app/commands/decidim/delete_follow.rb
decidim-core-0.25.0.rc1 app/commands/decidim/delete_follow.rb
decidim-core-0.24.3 app/commands/decidim/delete_follow.rb