Sha256: e748f0871516c8b010c5250de86bc44468b7d5ec8a695dd975ff5508dcac0565

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

module Unread
  module Reader
    module Scopes
      # This class method may be overriden to restrict readers to a subset of records
      # It must return self or a ActiveRecord::Relation
      def reader_scope
        self
      end

      def join_read_marks(readable)
        assert_readable(readable)

        joins "LEFT JOIN #{ReadMark.quoted_table_name}
                ON #{ReadMark.quoted_table_name}.readable_type  = '#{readable.class.readable_parent.name}'
               AND (#{ReadMark.quoted_table_name}.readable_id   = #{readable.id} OR #{ReadMark.quoted_table_name}.readable_id IS NULL)
               AND #{ReadMark.quoted_table_name}.reader_id      = #{quoted_table_name}.#{quoted_primary_key}
               AND #{ReadMark.quoted_table_name}.reader_type    = '#{connection.quote_string base_class.name}'
               AND #{ReadMark.quoted_table_name}.timestamp     >= '#{connection.quoted_date readable.send(readable.class.readable_options[:on])}'"
      end

      def have_not_read(readable)
        join_read_marks(readable).where("#{ReadMark.quoted_table_name}.id IS NULL")
      end

      def have_read(readable)
        join_read_marks(readable).where("#{ReadMark.quoted_table_name}.id IS NOT NULL")
      end

      def with_read_marks_for(readable)
        join_read_marks(readable).select("#{quoted_table_name}.*,
                                          #{ReadMark.quoted_table_name}.id AS read_mark_id,
                                         '#{readable.class.name}' AS read_mark_readable_type,
                                          #{readable.id} AS read_mark_readable_id")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unread-0.8.1 lib/unread/reader_scopes.rb
unread-0.8.0 lib/unread/reader_scopes.rb