Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added scope "with_read_marks_for"

  • Loading branch information...
commit aeab7674592550fb54a8240a45e736bb9cea1254 1 parent a06f3a0
Georg Ledermann authored
9 README.md
View
@@ -71,6 +71,7 @@ Step 2: Add this migration:
message1 = Message.create!
message2 = Message.create!
+ ## Get unread messages for a given user
Message.unread_by(current_user)
# => [ message1, message2 ]
@@ -78,6 +79,14 @@ Step 2: Add this migration:
Message.unread_by(current_user)
# => [ message2 ]
+ ## Get all messages including the read status for a given user
+ messages = Message.with_read_marks_for(current_user)
+ # => [ message1, message2 ]
+ messages[0].unread?(current_user)
+ # => false
+ messages[1].unread?(current_user)
+ # => true
+
Message.mark_as_read! :all, :for => current_user
Message.unread_by(current_user)
# => [ ]
4 changelog.md
View
@@ -1,3 +1,7 @@
+0.1.0 - unreleased
+
+* Added scope "with_read_marks_for"
+
0.0.7 - 2012/02/29
* Cleanup files
22 lib/unread/acts_as_readable.rb
View
@@ -49,6 +49,15 @@ def acts_as_readable(options={})
result
}
+ send scope_method, :with_read_marks_for, lambda { |user|
+ assert_reader(user)
+
+ { :select => "#{self.table_name}.*, read_marks.id AS read_mark_id",
+ :joins => "LEFT JOIN read_marks ON read_marks.readable_type = '#{self.base_class.name}'
+ AND read_marks.readable_id = #{self.table_name}.id
+ AND read_marks.user_id = #{user.id}
+ AND read_marks.timestamp >= #{self.table_name}.#{readable_options[:on]}" }
+ }
extend ReadableClassMethods
include ReadableInstanceMethods
end
@@ -149,7 +158,18 @@ def assert_reader(user=nil)
module ReadableInstanceMethods
def unread?(user)
- self.class.unread_by(user).exists?(self)
+ if self.respond_to?(:read_mark_id)
+ # For use with scope "with_read_marks_for"
+ return false if self.read_mark_id
+
+ if global_timestamp = user.read_mark_global(self.class).try(:timestamp)
+ self.send(readable_options[:on]) > global_timestamp
+ else
+ true
+ end
+ else
+ self.class.unread_by(user).exists?(self)
+ end
end
def mark_as_read!(options)
12 test/unread_test.rb
View
@@ -40,6 +40,18 @@ def test_scope
}
end
+ def test_with_read_marks_for
+ @email1.mark_as_read! :for => @reader
+
+ emails = Email.with_read_marks_for(@reader).all
+
+ assert emails[0].read_mark_id.present?
+ assert emails[1].read_mark_id.nil?
+
+ assert_equal false, emails[0].unread?(@reader)
+ assert_equal true, emails[1].unread?(@reader)
+ end
+
def test_scope_after_reset
@email1.mark_as_read! :for => @reader
Please sign in to comment.
Something went wrong with that request. Please try again.