Skip to content

Instantly share code, notes, and snippets.

@reu
Created September 27, 2011 01:41
Show Gist options
  • Save reu/1244065 to your computer and use it in GitHub Desktop.
Save reu/1244065 to your computer and use it in GitHub Desktop.
AR::Relation with attr_encrypted monkey patch
module AttrEncryptedRelation
def self.included(base)
base.class_eval do
alias_method_chain :build_where, :attr_encrypted
end
end
private
def build_where_with_attr_encrypted(opts, other = [])
if opts.is_a? Hash
@klass.encrypted_attributes.each do |attribute, encryption_information|
if opts[attribute]
opts[attribute] = @klass.encrypt(attribute, opts[attribute])
opts[encryption_information[:attribute]] = opts.delete(attribute)
end
end
end
build_where_without_attr_encrypted(opts, other)
end
end
ActiveRecord::Relation.send :include, AttrEncryptedRelation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment