/
delete_follow.rb
44 lines (36 loc) · 1.05 KB
/
delete_follow.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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