Module: Mongoid::Encryptable::ClassMethods
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Extended In:
| |
| Defined in: | lib/mongoid/encryptable.rb |
Instance Attribute Summary
-
#encrypted? ⇒ true | false
readonly
Whether the model is encrypted.
-
#requires_encryption_schema? ⇒ true | false
readonly
Internal use only
Whether an encryption schema has to be generated for this model.
Instance Method Summary
-
#embeds_encrypted?(path) ⇒ true | false
Internal use only
Whether any model reachable through this model's embeds_one relations declares encryption.
-
#encrypt_with(options = {})
::Setthe encryption metadata for the model. -
#set_key_id(key_id)
Internal use only
Override the key_id for the model.
Instance Attribute Details
#encrypted? ⇒ true | false (readonly)
Whether the model is encrypted. It means that either the encrypt_with method was called on the model, or at least one of the fields is encrypted.
#requires_encryption_schema? ⇒ true | false (readonly)
Whether an encryption schema has to be generated for this model.
True when the model declares encryption itself, and also when any model reachable through its embeds_one relations does. A model in the second group has no encrypted field of its own, but its collection still needs a schema, otherwise the embedded fields are written in plaintext.
The answer is memoized, since this runs on the persistence path. Declaring encryption on a model after it has already been persisted is not supported.
# File 'lib/mongoid/encryptable.rb', line 53
def requires_encryption_schema? return @requires_encryption_schema if defined?(@requires_encryption_schema) @requires_encryption_schema = encrypted? || ([ self ]) end
Instance Method Details
#embeds_encrypted?(path) ⇒ true | false
Whether any model reachable through this model's embeds_one relations declares encryption.
embeds_many is not considered: libmongocrypt cannot express per-field encryption under array items, so those fields are never mapped.
# File 'lib/mongoid/encryptable.rb', line 71
def (path) relations.each_value.any? do |relation| next false unless relation.is_a?(Association::Embedded::EmbedsOne) klass = relation.try_relation_class # An association target does not have to be a Mongoid document, and # the class it names does not have to exist. next false unless klass.respond_to?(:encrypted?) next false if path.include?(klass) klass.encrypted? || klass.(path + [ klass ]) end end
#encrypt_with(options = {})
::Set the encryption metadata for the model. Parameters set here will be
used to encrypt the fields of the model, unless overridden on the
field itself.
is deterministic or not.
# File 'lib/mongoid/encryptable.rb', line 26
def encrypt_with( = {}) self. = end
#set_key_id(key_id)
Override the key_id for the model.
This method is solely for testing purposes and should not be used in the application code. The schema_map is generated very early in the application lifecycle, and overriding the key_id after that will not have any effect.
# File 'lib/mongoid/encryptable.rb', line 93
def set_key_id(key_id) [:key_id] = key_id end