Sha256: 3cd61448e50bef35d946c369bfb9568bbc1d10a32fb9f5e122fd906a128da38d

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module UnreadMongoid
  def self.included(base)
    base.extend Base
  end

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

      has_many :read_marks, :dependent => :delete_all, :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

    def acts_as_readable(options={})
      class_attribute :readable_options

      self.readable_options = options

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

      # :(
      after_save do |doc|
        doc.read_marks.each do |mark|
          mark.readable_timestamp = doc.readable_timestamp
          mark.save!
        end
      end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unread-mongoid-0.0.2 lib/unread_mongoid/base.rb