lib/lockbox/model.rb in lockbox-0.3.0 vs lib/lockbox/model.rb in lockbox-0.3.1

- old
+ new

@@ -1,38 +1,7 @@ module Lockbox module Model - def attached_encrypted(attribute, **options) - warn "[lockbox] DEPRECATION WARNING: Use encrypts_attached instead" - encrypts_attached(attribute, **options) - end - - def encrypts_attached(*attributes, **options) - attributes.each do |name| - name = name.to_sym - - class_eval do - @lockbox_attachments ||= {} - - if @lockbox_attachments.empty? - def self.lockbox_attachments - parent_attachments = - if superclass.respond_to?(:lockbox_attachments) - superclass.lockbox_attachments - else - {} - end - - parent_attachments.merge(@lockbox_attachments || {}) - end - end - - raise "Duplicate encrypted attachment: #{name}" if lockbox_attachments[name] - @lockbox_attachments[name] = options - end - end - end - def encrypts(*attributes, **options) # support objects # case options[:type] # when Date # options[:type] = :date @@ -269,10 +238,11 @@ # 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.4.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? @@ -325,10 +295,11 @@ 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.4.0 case options[:type] when :boolean message = message == "t" when :date message = ActiveRecord::Type::Date.new.deserialize(message) @@ -359,9 +330,43 @@ before_validation do send("#{name}=", send(original_name)) if send("#{original_name}_changed?") end end end + end + end + + module Attached + def encrypts_attached(*attributes, **options) + attributes.each do |name| + name = name.to_sym + + class_eval do + @lockbox_attachments ||= {} + + if @lockbox_attachments.empty? + def self.lockbox_attachments + parent_attachments = + if superclass.respond_to?(:lockbox_attachments) + superclass.lockbox_attachments + else + {} + end + + parent_attachments.merge(@lockbox_attachments || {}) + end + end + + raise "Duplicate encrypted attachment: #{name}" if lockbox_attachments[name] + @lockbox_attachments[name] = options + end + end + end + + # TODO remove in future version + def attached_encrypted(attribute, **options) + warn "[lockbox] DEPRECATION WARNING: Use encrypts_attached instead" + encrypts_attached(attribute, **options) end end end end