Method: ActiveModel::Datastore#build_entity

Defined in:
lib/active_model/datastore.rb

#build_entity(parent = nil) ⇒ Entity

Builds the Cloud Datastore entity with attributes from the Model object.

Parameters:

  • parent (Google::Cloud::Datastore::Key) (defaults to: nil)

    An optional parent Key of the entity.

Returns:

  • (Entity)

    The updated Google::Cloud::Datastore::Entity.


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/active_model/datastore.rb', line 153

def build_entity(parent = nil)
  entity = CloudDatastore.dataset.entity self.class.name, id
  if parent.present?
    raise ArgumentError, 'Must be a Key' unless parent.is_a? Google::Cloud::Datastore::Key

    entity.key.parent = parent
  elsif parent?
    entity.key.parent = self.class.parent_key(parent_key_id)
  end
  entity_properties.each do |attr|
    entity[attr] = instance_variable_get("@#{attr}")
    entity.exclude_from_indexes!(attr, true) if no_index_attributes.include? attr
  end
  entity
end