forked from ifmeorg/ifme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallyship_creator.rb
64 lines (49 loc) · 1.19 KB
/
allyship_creator.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
class AllyshipCreator
attr_reader :ally_id, :current_user, :notifier
def initialize(args)
@ally_id = args[:ally_id]
@current_user = args[:current_user]
@notifier = args[:notifier] || Allyships::AllianceNotifier
end
def self.perform(args = {})
new(args).perform
end
def perform
return unless valid?
setup_allyship
remove_request
notifier.perform(pusher_type: pusher_type,
current_user: current_user,
ally_id: ally_id)
end
private
def allyship
return @allyship if defined?(@allyship)
@allyship = Allyship.find_by(ally_id: ally_id, user_id: current_user.id)
end
def valid?
ally_id && current_user
end
def pusher_type
return 'accepted_ally_request' if allyship
'new_ally_request'
end
def create_allyship
Allyship.create(
user_id: current_user.id,
ally_id: ally_id,
status: :pending_from_ally
)
end
def setup_allyship
return allyship.accepted! if allyship
create_allyship
end
def remove_request
current_user
.notifications
.where(uniqueid: "new_ally_request_#{ally_id}")
.destroy_all
end
end