Module: UnreadMongoid::Base

Defined in:
lib/unread_mongoid/base.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_readable(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/unread_mongoid/base.rb', line 24

def acts_as_readable(options={})
  class_attribute :readable_options

  self.readable_options = options

  has_many :read_marks, :as => :readable, :dependent => :destroy

  ReadMark.readable_classes ||= []
  ReadMark.readable_classes << self unless ReadMark.readable_classes.include?(self)

  include Readable::InstanceMethods
  extend Readable::ClassMethods
  extend Readable::Scopes
end

#acts_as_readerObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/unread_mongoid/base.rb', line 7

def acts_as_reader
  ReadMark.belongs_to :user, :class_name => self.to_s

  has_many :read_marks, :dependent => :destroy, :foreign_key => 'user_id', :inverse_of => :user

  after_create do |user|
    # We assume that a new user should not be tackled by tons of old messages
    # created BEFORE he signed up.
    # Instead, the new user starts with zero unread messages
    (ReadMark.readable_classes || []).each do |klass|
      klass.mark_as_read! :all, :for => user
    end
  end

  include Reader::InstanceMethods
end