Skip to content

Instantly share code, notes, and snippets.

@rafakuch
Created October 5, 2012 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rafakuch/3842277 to your computer and use it in GitHub Desktop.
Save rafakuch/3842277 to your computer and use it in GitHub Desktop.
Possible solution for CanCan Issue #646?
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