Module: Unread::Readable::Scopes

Defined in:
lib/unread/readable_scopes.rb

Instance Method Summary collapse

Instance Method Details

#join_read_marks(reader) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/unread/readable_scopes.rb', line 4

def join_read_marks(reader)
  assert_reader(reader)

  joins "LEFT JOIN #{ReadMark.quoted_table_name}
          ON #{ReadMark.quoted_table_name}.readable_type  = '#{readable_parent.name}'
         AND #{ReadMark.quoted_table_name}.readable_id    = #{quoted_table_name}.#{quoted_primary_key}
         AND #{ReadMark.quoted_table_name}.reader_id      = #{quote_bound_value(reader.id)}
         AND #{ReadMark.quoted_table_name}.reader_type    = #{quote_bound_value(reader.class.base_class.name)}
         AND #{ReadMark.quoted_table_name}.timestamp     >= #{quoted_table_name}.#{connection.quote_column_name(readable_options[:on])}"
end

#read_by(reader) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/unread/readable_scopes.rb', line 26

def read_by(reader)
  result = join_read_marks(reader)

  if global_time_stamp = reader.read_mark_global(self).try(:timestamp)
    result.where("#{ReadMark.quoted_table_name}.id IS NOT NULL
                  OR #{quoted_table_name}.#{connection.quote_column_name(readable_options[:on])} <= ?", global_time_stamp)
  else
    result.where("#{ReadMark.quoted_table_name}.id IS NOT NULL")
  end
end

#unread_by(reader) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/unread/readable_scopes.rb', line 15

def unread_by(reader)
  result = join_read_marks(reader)

  if global_time_stamp = reader.read_mark_global(self).try(:timestamp)
    result.where("#{ReadMark.quoted_table_name}.id IS NULL
                  AND #{quoted_table_name}.#{connection.quote_column_name(readable_options[:on])} > ?", global_time_stamp)
  else
    result.where("#{ReadMark.quoted_table_name}.id IS NULL")
  end
end

#with_read_marks_for(reader) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/unread/readable_scopes.rb', line 37

def with_read_marks_for(reader)
  postgresql_string_cast = using_postgresql? ? '::varchar' : ''

  join_read_marks(reader).select("#{quoted_table_name}.*,
                                  #{ReadMark.quoted_table_name}.id AS read_mark_id,
                                  #{quote_bound_value(reader.class.base_class.name)}#{postgresql_string_cast} AS read_mark_reader_type,
                                  #{quote_bound_value(reader.id)} AS read_mark_reader_id")
end