Sha256: 3787bc2455052027689394464dc756917af29076a5f442f6e9cc2e044ca18b47

Contents?: true

Size: 886 Bytes

Versions: 3

Compression:

Stored size: 886 Bytes

Contents

module WithDiscussionCreation::Subscription
  extend ActiveSupport::Concern

  included do
    has_many :subscriptions
    has_many :watched_discussions, through: :subscriptions, source: :discussion
    organic_on :watched_discussions
  end

  def subscriptions_in_organization
    subscriptions.joins(:discussion).where(discussion: discussions_in_organization)
  end

  def subscribed_to?(discussion)
    discussion.subscription_for(self).present?
  end

  def subscribe_to!(discussion)
    watched_discussions << discussion
  end

  def unsubscribe_to!(discussion)
    watched_discussions.delete(discussion)
  end

  def toggle_subscription!(discussion)
    if subscribed_to?(discussion)
      unsubscribe_to!(discussion)
    else
      subscribe_to!(discussion)
    end
  end

  def unread_discussions
    subscriptions_in_organization.where(read: false).map(&:discussion)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mumuki-domain-6.6.2 app/models/concerns/with_discussion_creation/subscription.rb
mumuki-domain-6.6.1 app/models/concerns/with_discussion_creation/subscription.rb
mumuki-domain-6.6.0 app/models/concerns/with_discussion_creation/subscription.rb