W3cubDocs

/Ruby on Rails 7.0

class ActiveRecord::Encryption::EncryptedAttributeType

Parent:
ActiveRecord::Type::Text
Included modules:
ActiveModel::Type::Helpers::Mutable

An ActiveModel::Type that encrypts/decrypts strings of text.

This is the central piece that connects the encryption system with encrypts declarations in the model classes. Whenever you declare an attribute as encrypted, it configures an EncryptedAttributeType for that attribute.

Attributes

cast_type[R]
scheme[R]

Public Class Methods

new(scheme:, cast_type: ActiveModel::Type::String.new, previous_type: false) Show source
# File activerecord/lib/active_record/encryption/encrypted_attribute_type.rb, line 23
def initialize(scheme:, cast_type: ActiveModel::Type::String.new, previous_type: false)
  super()
  @scheme = scheme
  @cast_type = cast_type
  @previous_type = previous_type
end

Options

  • :scheme - A Scheme with the encryption properties for this attribute.

  • :cast_type - A type that will be used to serialize (before encrypting) and deserialize (after decrypting). ActiveModel::Type::String by default.

Calls superclass method

Public Instance Methods

changed_in_place?(raw_old_value, new_value) Show source
# File activerecord/lib/active_record/encryption/encrypted_attribute_type.rb, line 42
def changed_in_place?(raw_old_value, new_value)
  old_value = raw_old_value.nil? ? nil : deserialize(raw_old_value)
  old_value != new_value
end
deserialize(value) Show source
# File activerecord/lib/active_record/encryption/encrypted_attribute_type.rb, line 30
def deserialize(value)
  cast_type.deserialize decrypt(value)
end
serialize(value) Show source
# File activerecord/lib/active_record/encryption/encrypted_attribute_type.rb, line 34
def serialize(value)
  if serialize_with_oldest?
    serialize_with_oldest(value)
  else
    serialize_with_current(value)
  end
end

© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.