Sha256: 0d093f65ad06fbdf0e01f2f710a024240d5765fd0b9e182c05dc19d109147923

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 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 < Decidim::Command
    delegate :current_user, to: :form

    # Public: Initializes the command.
    #
    # form         - A form object with the params.
    def initialize(form)
      @form = form
    end

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

      create_follow!
      increment_score

      broadcast(:ok, follow)
    end

    private

    attr_reader :follow, :form

    def create_follow!
      @follow = Follow.find_by(
        followable: form.followable,
        user: current_user
      ) || 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

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/commands/decidim/create_follow.rb
decidim-core-0.29.0 app/commands/decidim/create_follow.rb
decidim-core-0.29.0.rc4 app/commands/decidim/create_follow.rb
decidim-core-0.29.0.rc3 app/commands/decidim/create_follow.rb
decidim-core-0.29.0.rc2 app/commands/decidim/create_follow.rb
decidim-core-0.29.0.rc1 app/commands/decidim/create_follow.rb