Class: ActiveRecord::Embedded::Association::Many

Inherits:
ActiveRecord::Embedded::Association show all
Defined in:
lib/active_record/embedded/association/many.rb

Overview

Represents a one-to-many association between an ActiveRecord model and an ActiveRecord::Embedded::Model. Invoked by calling embeds_many on a parent model, and similar to a one-to-one relationship, it stores its data on the model as a Hash, but renders the data as an Enumerable object that behaves more like an array, or more accurately an ActiveRecord::Relation.

Instance Attribute Summary

Attributes inherited from ActiveRecord::Embedded::Association

#class_name, #name

Instance Method Summary collapse

Methods inherited from ActiveRecord::Embedded::Association

#build, #create, #embedded_class, #initialize

Constructor Details

This class inherits a constructor from ActiveRecord::Embedded::Association

Instance Method Details

#assign(model, items) ⇒ Hash

Mass-assign all records to the given data.

Parameters:

  • model (ActiveRecord::Base)
    • parent model to save into.

  • items (Enumerable)
    • Parameters to save

Returns:

  • (Hash)

    params saved into the parent model.


39
40
41
42
43
44
45
46
# File 'lib/active_record/embedded/association/many.rb', line 39

def assign(model, items)
  model[name] ||= { 'data': [], 'index': {} }
  model[name]['data'] = items.map do |item|
    embedded = build(model, item.to_h)
    embedded.attributes
  end
  index(model)
end

#destroy(model, id:) ⇒ Boolean

Destroy a single record in place.

Parameters:

  • model (ActiveRecord::Base)
    • persistence model

  • id (String)
    • ID of element to destroy

Returns:

  • (Boolean)

    whether the operation succeeded


64
65
66
67
68
# File 'lib/active_record/embedded/association/many.rb', line 64

def destroy(model, id:)
  model[name] ||= { 'data': [], 'index': {} }
  model[name]['data'].reject! { |item| item['id'] == id }
  index(model)
end

#find(model, id) ⇒ ActiveRecord::Embedded::Model

Return a given model by its ID.

Parameters:

  • model (ActiveRecord::Base)
    • parent model to find from.

  • id (String)
    • ID of embedded record

Returns:


30
31
32
# File 'lib/active_record/embedded/association/many.rb', line 30

def find(model, id)
  query(model).find!(id)
end

#index(model) ⇒ Object

Reindex all data on this model.


71
72
73
74
75
76
77
# File 'lib/active_record/embedded/association/many.rb', line 71

def index(model)
  data = model[name]['data']
  model[name]['index'] = indexes.each_with_object({}) do |index, json|
    json[index.name] = index.build(data)
  end
  true
end

#query(model) ⇒ ActiveRecord::Embedded::Relation

Query for finding models in a one-to-many relationship.

Parameters:

  • model (ActiveRecord::Base)
    • parent model to find from.

Returns:


18
19
20
21
22
23
# File 'lib/active_record/embedded/association/many.rb', line 18

def query(model)
  Relation.new(
    association: self,
    model: model
  )
end

#update(model, item) ⇒ Boolean

Update a single record in place.

Parameters:

  • model (ActiveRecord::Base)
    • parent model to save into.

  • item (Hash)
    • Parameters to save

Returns:

  • (Boolean)

    whether the operation succeeded


53
54
55
56
57
# File 'lib/active_record/embedded/association/many.rb', line 53

def update(model, item)
  model[name] ||= { 'data': [], 'index': {} }
  model[name]['data'] << item.stringify_keys
  index(model)
end