-
-
Save vollnhals/4031511 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
# overwrite conditions method to work on rails >= 3.2.6 | |
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 | |
if pk | |
h[pk] ||= {} | |
h[pk][k] = v | |
else | |
h[k] = v | |
end | |
h | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment