lib/lockbox/model.rb in lockbox-0.6.3 vs lib/lockbox/model.rb in lockbox-0.6.4

- old
+ new

@@ -1,8 +1,8 @@ module Lockbox module Model - def encrypts(*attributes, **options) + def lockbox_encrypts(*attributes, **options) # support objects # case options[:type] # when Date # options[:type] = :date # when Time @@ -147,17 +147,39 @@ end # needed for in-place modifications # assigned attributes are encrypted on assignment # and then again here - before_save do + def lockbox_sync_attributes self.class.lockbox_attributes.each do |_, lockbox_attribute| attribute = lockbox_attribute[:attribute] - if attribute_changed_in_place?(attribute) + if attribute_changed_in_place?(attribute) || (send("#{attribute}_changed?") && !send("#{lockbox_attribute[:encrypted_attribute]}_changed?")) send("#{attribute}=", send(attribute)) end end + end + + # safety check + [:_create_record, :_update_record].each do |method_name| + unless private_method_defined?(method_name) || method_defined?(method_name) + raise Lockbox::Error, "Expected #{method_name} to be defined. Please report an issue." + end + end + + def _create_record(*) + lockbox_sync_attributes + super + end + + def _update_record(*) + lockbox_sync_attributes + super + end + + def [](attr_name) + send(attr_name) if self.class.lockbox_attributes.any? { |_, la| la[:attribute] == attr_name.to_s } + super end def update_columns(attributes) return super unless attributes.is_a?(Hash)