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
-
#embedded_encryption_scan(path) ⇒ Array<true | false>
Internal use only
Walks the models reachable through this model's embeds_one relations, looking for one that 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.
This runs on the persistence path, so the answer is memoized. A true answer is always memoized. A false answer is memoized only when every embeds_one target in the walk resolved to a class; if one could not be resolved yet, the answer is recomputed next time, so a class loaded later is still detected. Declaring encryption on a model after it has already been persisted is not supported.
# File 'lib/mongoid/encryptable.rb', line 56
def requires_encryption_schema? return @requires_encryption_schema if defined?(@requires_encryption_schema) return @requires_encryption_schema = true if encrypted? found, complete = ([ self ]) @requires_encryption_schema = found if found || complete found end
Instance Method Details
#embedded_encryption_scan(path) ⇒ Array<true | false>
Walks the models reachable through this model's embeds_one relations, looking for one that 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 80
def (path) complete = true relations.each_value do |relation| next unless relation.is_a?(Association::Embedded::EmbedsOne) klass = relation.try_relation_class # The class an association names does not have to exist yet. if klass.nil? complete = false next end # An association target does not have to be a Mongoid document. next unless klass.respond_to?(:encrypted?) next if path.include?(klass) return [ true, true ] if klass.encrypted? found, nested_complete = klass.(path + [ klass ]) return [ true, true ] if found complete &&= nested_complete end [ false, complete ] 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 115
def set_key_id(key_id) [:key_id] = key_id end