Skip to content

Instantly share code, notes, and snippets.

@kuon
Created April 28, 2011 14:06
Show Gist options
  • Save kuon/946406 to your computer and use it in GitHub Desktop.
Save kuon/946406 to your computer and use it in GitHub Desktop.
def find_readable(*args)
type = args[0].to_s.capitalize
args.shift
cond = priv_conditions(type, :read => true)
type.constantize.find(*args).where(*cond)
end
private
def priv_conditions(type, rw)
# Pure ruby way
#privileges.find_all_by_target_type_and_read(type.to_s.capitalize, true, :include => :target).map { |p| p.target}
["id in (select target_id from privileges where group_id = ? and target_type = ? #{'and read = ?' if rw.has_key?(:read)} #{'and write = ?' if rw.has_key?(:write)})", id, type, rw[:read], rw[:write]]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment