Sha256: acf39b612043553a0fb01b56defe7b9b1fa3fcb0a387d6d12a19d9fcbe444bb8

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module ReadActivity
  module Reader
    class << self
      attr_accessor :klass
      def register_klass(klass)
        @klass = klass
      end
    end

    module ClassMethods
      def find_who_read(readable)
        self.joins(:read_activity_marks).merge(ReadActivityMark.where(readable: readable))
      end

      def find_who_unread(readable)
        self.joins(%Q(
          LEFT OUTER JOIN
          (SELECT * FROM read_activity_marks WHERE readable_id = #{readable.id}) AS readable_marks
          ON readable_marks.reader_id = #{self.table_name}.id
        )).where("readable_marks.reader_id IS NULL")
      end
    end

    module InstanceMethods
      # inverse of Readable#read_by!
      def read!(readable)
        readable.read_by!(self)
      end

      # inverse of Readable#read_by?
      def read?(readable)
        readable.read_by?(self)
      end

      def readables_marked_as_read(klass)
        klass.send(:find_read_by, self)
      end

      def readables_unmarked_as_read(klass)
        klass.send(:find_unread_by, self)
      end

      # inverse of Readable#read_by_at
      def read_at(readable)
        readable.read_by_at(self)
      end

      def method_missing(method, *arguments, &block)
        if method.to_s =~ /^read_(.*)/
          readables_marked_as_read($1.singularize.camelize.constantize, *arguments, &block)
        elsif method.to_s =~ /^unread_(.*)/
          readables_unmarked_as_read($1.singularize.camelize.constantize, *arguments, &block)
        else
          super
        end
      end

      def respond_to_missing?(method, include_private = false)
        method_name = method.to_s
        if method_name.start_with?("read_") or method_name.start_with?("unread_")
          true
        else
          super
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
read_activity-0.0.4 lib/read_activity/reader.rb
read_activity-0.0.3.1 lib/read_activity/reader.rb
read_activity-0.0.3 lib/read_activity/reader.rb