Sha256: 4dad69c912db2c15a9ca2f4cb0ef55c2972725d56a7306c59f5e5bae276b9b46

Contents?: true

Size: 1.16 KB

Versions: 10

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Talkie
  module Mentionable
    extend ActiveSupport::Concern

    included do
      attr_writer :mention_tokens

      before_create :check_for_mentions!, if: :mentions?
      after_create :notify_mentionees!, if: :enabled_notifications?
    end

    def check_for_mentions!
      subscribers.each do |subscriber|
        self.subscriptions.build(subscriber: subscriber)
      end
    end

    def mentions?
      mention_tokens.present?
    end

    def notify_mentionees!
      NotificationsMailer.with(comment: self, recipients: subscribers.pluck(:email)).mentioned.deliver_now
    end

    def mention_tokens
      @mention_tokens || Talkie::NilMentionTokens.new.to_s
    end

    private

    def subscribers
      @subscribers ||= mentionee_klass.find(subscriber_ids)
    end

    def subscriber_ids
      @subscriber_ids ||= mentions.map { |mention| mention["id"] }
    end

    def mentionee_klass
      @mentionee_klass ||= mentions.first["type"]&.constantize
    end

    def mentions
      @mentions ||= JSON.parse(mention_tokens)
    end

    def enabled_notifications?
      Talkie.notifications_mentions_enabled?
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
talkie-0.5.1 app/models/concerns/talkie/mentionable.rb
talkie-0.5.0 app/models/concerns/talkie/mentionable.rb
talkie-0.4.1 app/models/concerns/talkie/mentionable.rb
talkie-0.4.0 app/models/concerns/talkie/mentionable.rb
talkie-0.3.5 app/models/concerns/talkie/mentionable.rb
talkie-0.3.4 app/models/concerns/talkie/mentionable.rb
talkie-0.3.3 app/models/concerns/talkie/mentionable.rb
talkie-0.3.2 app/models/concerns/talkie/mentionable.rb
talkie-0.3.1 app/models/concerns/talkie/mentionable.rb
talkie-0.3.0 app/models/concerns/talkie/mentionable.rb