Created
October 5, 2012 20:45
-
-
Save rafakuch/3842277 to your computer and use it in GitHub Desktop.
Possible solution for CanCan Issue #646?
This file contains hidden or 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
class CanCan::ModelAdapters::ActiveRecordAdapter | |
def conditions | |
if @rules.size == 1 && @rules.first.base_behavior | |
# Return the conditions directly if there's just one definition | |
dig(tableized_conditions(@rules.first.conditions).dup) | |
else | |
@rules.reverse.inject(false_sql) do |sql, rule| | |
merge_conditions(sql, dig(tableized_conditions(rule.conditions).dup), rule.base_behavior) | |
end | |
end | |
end | |
#dig into the hash to get conditions | |
def dig(hash, h = {}, pk = nil) | |
hash.inject(h) do |h, (k,v)| | |
if v.kind_of? Hash | |
dig(v, h, k ) | |
else | |
pk ? h[pk] = { k => v} : h[k]=v | |
h | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment