Module: Mongoid::Encryptable::ClassMethods
- Defined in:
- lib/mongoid/encryptable.rb
Instance Method Summary collapse
-
#embeds_encrypted?(path) ⇒ true | false
private
Whether any model reachable through this model's embeds_one relations declares encryption.
-
#encrypt_with(options = {}) ⇒ Object
Set the encryption metadata for the model.
-
#encrypted? ⇒ true | false
Whether the model is encrypted.
-
#requires_encryption_schema? ⇒ true | false
private
Whether an encryption schema has to be generated for this model.
-
#set_key_id(key_id) ⇒ Object
private
Override the key_id for the model.
Instance Method Details
#embeds_encrypted?(path) ⇒ true | false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# 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 = {}) ⇒ Object
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.
26 27 28 |
# File 'lib/mongoid/encryptable.rb', line 26 def encrypt_with( = {}) self. = end |
#encrypted? ⇒ true | false
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.
35 36 37 |
# File 'lib/mongoid/encryptable.rb', line 35 def encrypted? !.empty? || fields.any? { |_, field| field.is_a?(Mongoid::Fields::Encrypted) } end |
#requires_encryption_schema? ⇒ true | false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
53 54 55 56 57 |
# 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 |
#set_key_id(key_id) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
93 94 95 |
# File 'lib/mongoid/encryptable.rb', line 93 def set_key_id(key_id) [:key_id] = key_id end |