lib/lockbox/model.rb in lockbox-0.6.8 vs lib/lockbox/model.rb in lockbox-1.0.0
- old
+ new
@@ -1,8 +1,8 @@
module Lockbox
module Model
- def lockbox_encrypts(*attributes, **options)
+ def has_encrypted(*attributes, **options)
# support objects
# case options[:type]
# when Date
# options[:type] = :date
# when Time
@@ -239,10 +239,27 @@
raise "Duplicate encrypted attribute: #{original_name}" if lockbox_attributes[original_name]
raise "Multiple encrypted attributes use the same column: #{encrypted_attribute}" if lockbox_attributes.any? { |_, v| v[:encrypted_attribute] == encrypted_attribute }
@lockbox_attributes[original_name] = options
if activerecord
+ # warn on default attributes
+ if attributes_to_define_after_schema_loads.key?(name.to_s)
+ opt = attributes_to_define_after_schema_loads[name.to_s][1]
+
+ has_default =
+ if ActiveRecord::VERSION::MAJOR >= 7
+ # not ideal, since NO_DEFAULT_PROVIDED is private
+ opt != ActiveRecord::Attributes::ClassMethods.const_get(:NO_DEFAULT_PROVIDED)
+ else
+ opt.is_a?(Hash) && opt.key?(:default)
+ end
+
+ if has_default
+ warn "[lockbox] WARNING: attributes with `:default` option are not supported. Use `after_initialize` instead."
+ end
+ end
+
# preference:
# 1. type option
# 2. existing virtual attribute
# 3. default to string (which can later be overridden)
if options[:type]
@@ -301,15 +318,13 @@
define_method("restore_#{name}!") do
super()
send("restore_#{encrypted_attribute}!")
end
- if ActiveRecord::VERSION::STRING >= "5.1"
- define_method("#{name}_in_database") do
- send(name) # writes attribute when not already set
- super()
- end
+ define_method("#{name}_in_database") do
+ send(name) # writes attribute when not already set
+ super()
end
define_method("#{name}?") do
# uses public_send, so we don't need to preload attribute
query_attribute(name)
@@ -441,11 +456,10 @@
# for fixtures
define_singleton_method encrypt_method_name do |message, **opts|
table = activerecord ? table_name : collection_name.to_s
unless message.nil?
- # TODO use attribute type class in 0.7.0
case options[:type]
when :boolean
message = ActiveRecord::Type::Boolean.new.serialize(message)
message = nil if message == "" # for Active Record < 5.2
message = message ? "t" : "f" unless message.nil?
@@ -504,11 +518,10 @@
table = activerecord ? table_name : collection_name.to_s
Lockbox::Utils.build_box(opts[:context], options, table, encrypted_attribute).decrypt(ciphertext)
end
unless message.nil?
- # TODO use attribute type class in 0.7.0
case options[:type]
when :boolean
message = message == "t"
when :date
message = ActiveRecord::Type::Date.new.deserialize(message)
@@ -560,9 +573,14 @@
end
prepend m
end
end
end
+ end
+
+ def lockbox_encrypts(*attributes, **options)
+ ActiveSupport::Deprecation.warn("`#{__callee__}` is deprecated in favor of `has_encrypted`")
+ has_encrypted(*attributes, **options)
end
module Attached
def encrypts_attached(*attributes, **options)
attributes.each do |name|