Created
April 28, 2011 14:06
-
-
Save kuon/946406 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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