Class: ActiveRecord::Embedded::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/embedded/association.rb,
lib/active_record/embedded/association/one.rb,
lib/active_record/embedded/association/many.rb,
lib/active_record/embedded/association/parent.rb

Overview

Base class for all embedded associations, such as embeds_one and embeds_many. Defines the interface that such associations must implement in order to be compatible with the rest of the ActiveRecord::Embedded API.

Direct Known Subclasses

Many, One, Parent

Defined Under Namespace

Classes: Many, One, Parent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, class_name: nil, **options) ⇒ Association


14
15
16
17
18
# File 'lib/active_record/embedded/association.rb', line 14

def initialize(name:, class_name: nil, **options)
  @name = name
  @class_name = class_name || name.to_s.classify
  options.each { |key, value| instance_variable_set "@#{key}", value }
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name


10
11
12
# File 'lib/active_record/embedded/association.rb', line 10

def class_name
  @class_name
end

#nameObject (readonly)

Returns the value of attribute name


10
11
12
# File 'lib/active_record/embedded/association.rb', line 10

def name
  @name
end

Instance Method Details

#assign(_model, _params) ⇒ Object

Raises:

  • (NotImplementedError)

28
29
30
# File 'lib/active_record/embedded/association.rb', line 28

def assign(_model, _params)
  raise NotImplementedError, "#{self.class.name}#find"
end

#build(model, value = {}) ⇒ Object


48
49
50
51
52
53
54
55
56
# File 'lib/active_record/embedded/association.rb', line 48

def build(model, value = {})
  return value if value.is_a? embedded_class

  embedded_class.new(
    _parent: model,
    _association: self,
    **value.symbolize_keys
  )
end

#create(_model, _params) ⇒ Object

Raises:

  • (NotImplementedError)

32
33
34
# File 'lib/active_record/embedded/association.rb', line 32

def create(_model, _params)
  raise NotImplementedError, "#{self.class.name}#create"
end

#destroy(_model, **_params) ⇒ Object

Raises:

  • (NotImplementedError)

40
41
42
# File 'lib/active_record/embedded/association.rb', line 40

def destroy(_model, **_params)
  raise NotImplementedError, "#{self.class.name}#destroy"
end

#embedded_classObject


58
59
60
# File 'lib/active_record/embedded/association.rb', line 58

def embedded_class
  class_name.constantize
end

#find(_model, _id) ⇒ Object

Raises:

  • (NotImplementedError)

24
25
26
# File 'lib/active_record/embedded/association.rb', line 24

def find(_model, _id)
  raise NotImplementedError, "#{self.class.name}#find"
end

#index(_model, _data = []) ⇒ Object

Raises:

  • (NotImplementedError)

44
45
46
# File 'lib/active_record/embedded/association.rb', line 44

def index(_model, _data = [])
  raise NotImplementedError, "#{self.class.name}#index"
end

#query(_model) ⇒ Object

Raises:

  • (NotImplementedError)

20
21
22
# File 'lib/active_record/embedded/association.rb', line 20

def query(_model)
  raise NotImplementedError, "#{self.class.name}#find"
end

#update(_model, _params) ⇒ Object

Raises:

  • (NotImplementedError)

36
37
38
# File 'lib/active_record/embedded/association.rb', line 36

def update(_model, _params)
  raise NotImplementedError, "#{self.class.name}#update"
end