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

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


62
63
64
65
66
67
68
# File 'app/models/concerns/subscribable.rb', line 62

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


48
49
50
51
52
53
# File 'app/models/concerns/subscribable.rb', line 48

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 = subscriptions.find_by(user: user, project: project)
    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)

29
30
31
# File 'app/models/concerns/subscribable.rb', line 29

def subscribed_without_subscriptions?(user, project)
  false
end

#subscribers(project) ⇒ Object


33
34
35
36
37
38
39
# File 'app/models/concerns/subscribable.rb', line 33

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

  User.where(id: relation)
end

#toggle_subscription(user, project = nil) ⇒ Object


41
42
43
44
45
46
# File 'app/models/concerns/subscribable.rb', line 41

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

  find_or_initialize_subscription(user, project)
    .update(subscribed: !subscribed?(user, project))
end

#unsubscribe(user, project = nil) ⇒ Object


55
56
57
58
59
60
# File 'app/models/concerns/subscribable.rb', line 55

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

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