Skip to content

Instantly share code, notes, and snippets.

@ys
Created November 21, 2013 12:56
Show Gist options
  • Save ys/7581114 to your computer and use it in GitHub Desktop.
Save ys/7581114 to your computer and use it in GitHub Desktop.
class Follow < ActiveRecord::Base
belongs_to :follower, class_name: 'User'
belongs_to :followee, class_name: 'User'
end
class Post < ActiveRecord::Base
belongs_to :user
end
PostPolicy = Struct.new(:user, :post) do
self::Scope = Struct.new(:user, :scope) do
def resolve
scope.joins(:user)
.where(users[:private_profile].eq(false)
.or(posts[:user_id].eq(user.id))
.or(posts[:user_id].in(followers)))
end
end
def followers
follows.where(follows[:follower_id].eq(user.id))
.where(follows[:approved].eq(true)).project(:followee_id)
end
def follows
Arel::Table.new(:follows)
end
def users
Arel::Table.new(:users)
end
def posts
Arel::Table.new(:posts)
end
end
end
class User < ActiveRecord::Base
#has a private_profile flag
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment