Skip to content

Commit

Permalink
Small tweaks to support the new Magnus-based enquo-core
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalmer committed Apr 18, 2023
1 parent 467b315 commit 9812609
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion active_enquo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |s|

s.required_ruby_version = ">= 2.7.0"

s.add_runtime_dependency "enquo-core", "~> 0.7"
s.add_runtime_dependency "enquo-core", "~> 0.8"
s.add_runtime_dependency "activerecord", ">= 6"

s.add_development_dependency "bundler"
Expand Down
24 changes: 16 additions & 8 deletions lib/active_enquo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def enquo(attr_name, value_or_meta_id, maybe_value = nil)

t = self.attribute_types[attr_name.to_s]
if t.is_a?(::ActiveEnquo::Type)
relation = self.arel_table.name
field = ::ActiveEnquo.root.field(relation, attr_name)
relation = self.arel_table.name.to_s
field = ::ActiveEnquo.root.field(relation, attr_name.to_s)
if meta_id.nil?
t.encrypt(value, "", field, enable_reduced_security_operations: true)
else
Expand All @@ -140,8 +140,8 @@ def enquo(attr_name, value_or_meta_id, maybe_value = nil)
def unenquo(attr_name, value, ctx)
t = self.attribute_types[attr_name.to_s]
if t.is_a?(::ActiveEnquo::Type)
relation = self.arel_table.name
field = ::ActiveEnquo.root.field(relation, attr_name)
relation = self.arel_table.name.to_s
field = ::ActiveEnquo.root.field(relation, attr_name.to_s)
begin
t.decrypt(value, ctx, field)
rescue Enquo::Error
Expand Down Expand Up @@ -270,7 +270,7 @@ def encrypt(value, context, field, enable_reduced_security_operations: false, no
if value.nil? || value.is_a?(::ActiveRecord::StatementCache::Substitute)
value
else
field.encrypt_boolean(value, context, safety: enable_reduced_security_operations ? :unsafe : true, no_query: no_query)
field.encrypt_boolean(value, context, unsafe: enable_reduced_security_operations, no_query: no_query)
end
end

Expand All @@ -288,7 +288,7 @@ def encrypt(value, context, field, enable_reduced_security_operations: false, no
if value.nil? || value.is_a?(::ActiveRecord::StatementCache::Substitute)
value
else
field.encrypt_i64(value, context, safety: enable_reduced_security_operations ? :unsafe : true, no_query: no_query)
field.encrypt_i64(value, context, unsafe: enable_reduced_security_operations, no_query: no_query)
end
end

Expand All @@ -307,7 +307,7 @@ def encrypt(value, context, field, enable_reduced_security_operations: false, no
if value.nil?
value
else
field.encrypt_date(value, context, safety: enable_reduced_security_operations ? :unsafe : true, no_query: no_query)
field.encrypt_date(value, context, unsafe: enable_reduced_security_operations, no_query: no_query)
end
end

Expand Down Expand Up @@ -343,7 +343,15 @@ def encrypt(value, context, field, enable_reduced_security_operations: false, no
if value.nil? || value.is_a?(::ActiveRecord::StatementCache::Substitute)
value
else
field.encrypt_text(value.respond_to?(:encode) ? value.encode("UTF-8") : value, context, safety: enable_reduced_security_operations ? :unsafe : true, no_query: no_query, order_prefix_length: enable_ordering ? 8 : nil)
opts = {
unsafe: enable_reduced_security_operations,
no_query: no_query,
}
if enable_ordering
opts[:order_prefix_length] = 8
end

field.encrypt_text(value.respond_to?(:encode) ? value.encode("UTF-8") : value, context, **opts)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/bigint/insertion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def bind_param(value)
let(:value) { v }

it "explodes" do
expect { Bigint.new(value: v).save! }.to raise_error(ArgumentError)
expect { Bigint.new(value: v).save! }.to raise_error(TypeError)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/boolean/insertion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def bind_param(value)
let(:value) { v }

it "explodes" do
expect { Boolean.new(value: v).save! }.to raise_error(ArgumentError)
expect { Boolean.new(value: v).save! }.to raise_error(TypeError)
end
end
end
Expand Down
18 changes: 15 additions & 3 deletions spec/text/insertion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,31 @@ def bind_param(value)
end
end

{
"non-UTF8 string" => "\0\xff".force_encoding("BINARY"),
"invalid UTF8 string" => "\0\xff".force_encoding("UTF-8"),
}.each do |desc, v|
context "storing #{desc}" do
let(:model) { Text }
let(:value) { v }

it "explodes" do
expect { Text.new(value: v).save! }.to raise_error(EncodingError)
end
end
end

{
"a float" => 4.2,
"an integer" => 42,
"non-UTF8 string" => "\0\0\0\0",
"invalid UTF8 string" => "\0\0\0\0".force_encoding("UTF-8"),
"a random object" => Object.new,
}.each do |desc, v|
context "storing #{desc}" do
let(:model) { Text }
let(:value) { v }

it "explodes" do
expect { Text.new(value: v).save! }.to raise_error(ArgumentError)
expect { Text.new(value: v).save! }.to raise_error(TypeError)
end
end
end
Expand Down

0 comments on commit 9812609

Please sign in to comment.