Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for ActiveRecord::Encryption #220

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/scoped_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class QueryNotSupported < ScopedSearch::Exception
require 'scoped_search/query_builder'
require 'scoped_search/auto_complete_builder'
require 'scoped_search/validators'
require 'scoped_search/value_translators/encrypted'

# Import the search_on method in the ActiveReocrd::Base class
ActiveRecord::Base.send(:extend, ScopedSearch::ClassMethods)
Expand Down
24 changes: 21 additions & 3 deletions lib/scoped_search/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Field

attr_reader :definition, :field, :only_explicit, :relation, :key_relation, :full_text_search,
:key_field, :complete_value, :complete_enabled, :offset, :word_size, :ext_method, :operators,
:validator, :value_translation, :special_values
:validator, :special_values

# Initializes a Field instance given the definition passed to the
# scoped_search call on the ActiveRecord-based model class.
Expand Down Expand Up @@ -129,9 +129,27 @@ def column
end
end

def encrypted?
defined?(ActiveRecord::Encryption::EncryptableRecord) &&
klass.ancestors.include?(ActiveRecord::Encryption::EncryptableRecord) &&
klass.encrypted_attributes.include?(field)
end

def value_translation
return @value_translation if @value_translation

ValueTranslators::Encrypted.new(self) if encrypted?
end

# Returns the column type of this field.
def type
@type ||= virtual? ? :virtual : column.type
@type ||= if virtual?
:virtual
elsif encrypted?
:encrypted
else
column.type
end
end

# Returns true if this field is a datetime-like column.
Expand Down Expand Up @@ -265,7 +283,7 @@ def operator_by_field_name(name)
# Returns a list of appropriate fields to search in given a search keyword and operator.
def default_fields_for(value, operator = nil)

column_types = [:virtual]
column_types = [:encrypted, :virtual]
column_types += [:string, :text, :citext] if [nil, :like, :unlike, :ne, :eq].include?(operator)
column_types += [:double, :float, :decimal] if value =~ NUMERICAL_REGXP
column_types += [:integer] if value =~ INTEGER_REGXP
Expand Down
13 changes: 13 additions & 0 deletions lib/scoped_search/value_translators/encrypted.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ScopedSearch
module ValueTranslators
class Encrypted
def initialize(field)
@type = field.klass.type_for_attribute(field.field)
end

def call(value)
@type.serialize(value)
end
end
end
end
Loading