Skip to content

Instantly share code, notes, and snippets.

@morokhovets
Created October 20, 2011 19:22
Show Gist options
  • Save morokhovets/1302040 to your computer and use it in GitHub Desktop.
Save morokhovets/1302040 to your computer and use it in GitHub Desktop.
named_scope :unfiltered, { }
named_scope :not_hidden, :conditions => { :hidden => false }
named_scope :not_private, :conditions => { :private => false }
named_scope :not_not_private, :conditions => { :private => true }
named_scope :restricted, :conditions => { :restricted => true }
named_scope :unrestricted, :conditions => { :restricted => false, :initiation => false }
named_scope :interesting, lambda { |user|
{ :conditions => { :user_id => user.friends.map(&:id) } }
}
named_scope :my, lambda { |user|
{
:select => 'DISTINCT posts.*',
:joins => [:post_views, :comments],
:conditions => [ 'comments.user_id = :uid OR posts.user_id = :uid', {:uid => user.id} ]
}
}
named_scope :has_unread_comments, lambda { |user|
{
:joins => %Q(LEFT JOIN post_views pw ON pw.post_id = posts.id AND pw.user_id = #{user.id}
LEFT JOIN comments unread_comments ON unread_comments.post_id = posts.id),
:conditions => 'unread_comments.created_at > pw.last_viewed',
:group => 'unread_comments.post_id',
:having => 'count(unread_comments.post_id) > 0'
}
}
named_scope :unread, lambda { |user|
{
:select => "distinct posts.*",
:joins => %Q(LEFT OUTER JOIN post_views pv_unread ON pv_unread.post_id = posts.id AND pv_unread.user_id = #{user.id}
LEFT OUTER JOIN comments c_unread ON c_unread.post_id = posts.id),
:conditions => '(c_unread.created_at > pv_unread.last_viewed OR (c_unread.id IS NULL AND pv_unread.id IS NULL)) OR pv_unread.id IS NULL'
}
}
named_scope :descend_by_created_at, { :order => 'posts.created_at DESC' }
named_scope :accessible_by, lambda { |user|
{
:select => "DISTINCT posts.*",
:joins => "LEFT OUTER JOIN post_accesses ON post_accesses.post_id = posts.id",
:conditions => ['post_accesses.user_id = ? OR posts.user_id = ?', user.id, user.id ]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment