lib/read_activity/reader.rb in read_activity-0.0.4 vs lib/read_activity/reader.rb in read_activity-0.1.0
- old
+ new
@@ -7,11 +7,11 @@
end
end
module ClassMethods
def find_who_read(readable)
- self.joins(:read_activity_marks).merge(ReadActivityMark.where(readable: readable))
+ self.includes(:read_activity_marks).merge(ReadActivityMark.where(readable: readable)).references(:read_activity_marks)
end
def find_who_unread(readable)
self.joins(%Q(
LEFT OUTER JOIN
@@ -38,12 +38,25 @@
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)
+ def read_at(readable = nil)
+ read_at = nil
+
+ if self.read_activity_marks.loaded?
+ read_at = self.read_activity_marks.first.try(:created_at)
+ end
+
+ if read_at.nil? && readable
+ if readable.read_activity_marks.loaded?
+ read_at = readable.read_activity_marks.first.try(:created_at)
+ else
+ read_at = self.read_activity_marks.where(readable: readable).first.try(:created_at)
+ end
+ end
+
+ return read_at
end
def method_missing(method, *arguments, &block)
if method.to_s =~ /^read_(.*)/
readables_marked_as_read($1.singularize.camelize.constantize, *arguments, &block)
\ No newline at end of file