Module: Subscribable

Extended by:
ActiveSupport::Concern
Included in:
Issuable, Label
Defined in:
app/models/concerns/subscribable.rb

Overview

Subscribable concern

Users can subscribe to these models.

Used by Issue, MergeRequest, Label

Instance Method Summary collapse

Instance Method Details

#lazy_subscription(user, project = nil) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/concerns/subscribable.rb', line 27

def lazy_subscription(user, project = nil)
  return unless user

  # handle project and group labels as well as issuable subscriptions
  subscribable_type = self.class.ancestors.include?(Label) ? 'Label' : self.class.name
  BatchLoader.for(id: id, subscribable_type: subscribable_type, project_id: project&.id).batch do |items, loader|
    values = items.each_with_object({ ids: Set.new, subscribable_types: Set.new, project_ids: Set.new }) do |item, result|
      result[:ids] << item[:id]
      result[:subscribable_types] << item[:subscribable_type]
      result[:project_ids] << item[:project_id]
    end

    subscriptions = Subscription.where(subscribable_id: values[:ids], subscribable_type: values[:subscribable_types], project_id: values[:project_ids], user: user)

    subscriptions.each do |subscription|
      loader.call({
        id: subscription.subscribable_id,
        subscribable_type: subscription.subscribable_type,
        project_id: subscription.project_id
        }, subscription)
    end
  end
end

#set_subscription(user, desired_state, project = nil) ⇒ Object


88
89
90
91
92
93
94
# File 'app/models/concerns/subscribable.rb', line 88

def set_subscription(user, desired_state, project = nil)
  if desired_state
    subscribe(user, project)
  else
    unsubscribe(user, project)
  end
end

#subscribe(user, project = nil) ⇒ Object


74
75
76
77
78
79
# File 'app/models/concerns/subscribable.rb', line 74

def subscribe(user, project = nil)
  unsubscribe_from_other_levels(user, project)

  find_or_initialize_subscription(user, project)
    .update(subscribed: true)
end

#subscribed?(user, project = nil) ⇒ Boolean

Returns:

  • (Boolean)

17
18
19
20
21
22
23
24
25
# File 'app/models/concerns/subscribable.rb', line 17

def subscribed?(user, project = nil)
  return false unless user

  if (subscription = lazy_subscription(user, project)&.itself)
    subscription.subscribed
  else
    subscribed_without_subscriptions?(user, project)
  end
end

#subscribed_without_subscriptions?(user, project) ⇒ Boolean

Override this method to define custom logic to consider a subscribable as subscribed without an explicit subscription record.

Returns:

  • (Boolean)

53
54
55
# File 'app/models/concerns/subscribable.rb', line 53

def subscribed_without_subscriptions?(user, project)
  false
end

#subscribers(project) ⇒ Object


57
58
59
60
61
62
63
# File 'app/models/concerns/subscribable.rb', line 57

def subscribers(project)
  relation = subscriptions_available(project)
               .where(subscribed: true)
               .select(:user_id)

  User.where(id: relation)
end

#toggle_subscription(user, project = nil) ⇒ Object


65
66
67
68
69
70
71
72
# File 'app/models/concerns/subscribable.rb', line 65

def toggle_subscription(user, project = nil)
  unsubscribe_from_other_levels(user, project)

  new_value = !subscribed?(user, project)

  find_or_initialize_subscription(user, project)
    .update(subscribed: new_value)
end

#unsubscribe(user, project = nil) ⇒ Object


81
82
83
84
85
86
# File 'app/models/concerns/subscribable.rb', line 81

def unsubscribe(user, project = nil)
  unsubscribe_from_other_levels(user, project)

  find_or_initialize_subscription(user, project)
    .update(subscribed: false)
end