Skip to content

Instantly share code, notes, and snippets.

@the8472
Created June 18, 2012 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the8472/2948257 to your computer and use it in GitHub Desktop.
Save the8472/2948257 to your computer and use it in GitHub Desktop.
MonkeyPatch for cancan#646
require "cancan/model_adapters/active_record_adapter"
class CanCan::ModelAdapters::ActiveRecordAdapter
def merge_conditions(sql, conditions_hash, behavior)
conditions_hash = Squeel::Nodes::Literal.new(conditions_hash) if conditions_hash.is_a? String
if conditions_hash.blank?
Squeel::Nodes::Literal.new(behavior ? true_sql : false_sql)
else
case sql
when true_sql
behavior ? Squeel::Nodes::Literal.new(true_sql) : Squeel::Nodes::Not.new(conditions_hash)
when false_sql
behavior ? conditions_hash : Squeel::Nodes::Literal.new(false_sql)
else
sql = Squeel::Nodes::Literal.new(sql) if sql.is_a? String
behavior ? Squeel::Nodes::Or.new(conditions_hash,sql) : Squeel::Nodes::And.new([Squeel::Nodes::Not.new(conditions_hash),sql])
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment