Sha256: 8a0d35f6a7890b78ba4f88d6a50e882cbb8f7af255cce490a2f9bae2b380f82e

Contents?: true

Size: 1.12 KB

Versions: 20

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic for when a user starts following a resource.
  class CreateFollow < 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?

      create_follow!
      increment_score

      broadcast(:ok, follow)
    end

    private

    attr_reader :follow, :form, :current_user

    def create_follow!
      @follow = Follow.create!(
        followable: form.followable,
        user: current_user
      )
    end

    def increment_score
      return unless form.followable.is_a? Decidim::User

      Decidim::Gamification.increment_score(form.followable, :followers)
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
decidim-core-0.24.3 app/commands/decidim/create_follow.rb
decidim-core-0.23.6 app/commands/decidim/create_follow.rb
decidim-core-0.24.2 app/commands/decidim/create_follow.rb
decidim-core-0.23.5 app/commands/decidim/create_follow.rb
decidim-core-0.24.1 app/commands/decidim/create_follow.rb
decidim-core-0.24.0 app/commands/decidim/create_follow.rb
decidim-core-0.24.0.rc2 app/commands/decidim/create_follow.rb
decidim-core-0.23.4 app/commands/decidim/create_follow.rb
decidim-core-0.24.0.rc1 app/commands/decidim/create_follow.rb
decidim-core-0.23.3 app/commands/decidim/create_follow.rb
decidim-core-0.23.2 app/commands/decidim/create_follow.rb
decidim-core-0.23.1 app/commands/decidim/create_follow.rb
decidim-core-0.23.1.rc1 app/commands/decidim/create_follow.rb
decidim-core-0.23.0 app/commands/decidim/create_follow.rb
decidim-core-0.22.0 app/commands/decidim/create_follow.rb
decidim-core-0.21.0 app/commands/decidim/create_follow.rb
decidim-core-0.20.1 app/commands/decidim/create_follow.rb
decidim-core-0.20.0 app/commands/decidim/create_follow.rb
decidim-core-0.19.1 app/commands/decidim/create_follow.rb
decidim-core-0.19.0 app/commands/decidim/create_follow.rb