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

#subscribed?(user) ⇒ Boolean

Returns:

  • (Boolean)

15
16
17
18
19
20
21
# File 'app/models/concerns/subscribable.rb', line 15

def subscribed?(user)
  if subscription = subscriptions.find_by_user_id(user.id)
    subscription.subscribed
  else
    subscribed_without_subscriptions?(user)
  end
end

#subscribed_without_subscriptions?(user) ⇒ Boolean

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

Returns:

  • (Boolean)

25
26
27
# File 'app/models/concerns/subscribable.rb', line 25

def subscribed_without_subscriptions?(user)
  false
end

#subscribersObject


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

def subscribers
  subscriptions.where(subscribed: true).map(&:user)
end

#toggle_subscription(user) ⇒ Object


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

def toggle_subscription(user)
  subscriptions.
    find_or_initialize_by(user_id: user.id).
    update(subscribed: !subscribed?(user))
end

#unsubscribe(user) ⇒ Object


39
40
41
42
43
# File 'app/models/concerns/subscribable.rb', line 39

def unsubscribe(user)
  subscriptions.
    find_or_initialize_by(user_id: user.id).
    update(subscribed: false)
end