Class: ActiveMocker::SingleRelation

Inherits:
Object
  • Object
show all
Defined in:
lib/active_mocker/mock/single_relation.rb

Direct Known Subclasses

BelongsTo, HasOne

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, child_self:, foreign_key:) ⇒ SingleRelation

Returns a new instance of SingleRelation.



6
7
8
9
# File 'lib/active_mocker/mock/single_relation.rb', line 6

def initialize(item, child_self:, foreign_key:)
  @item = item
  assign_associations(child_self, item) if item.class <= Base
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



4
5
6
# File 'lib/active_mocker/mock/single_relation.rb', line 4

def item
  @item
end

Instance Method Details

#assign_associations(child_self, item) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_mocker/mock/single_relation.rb', line 11

def assign_associations(child_self, item)
  [*item.class._find_associations_by_class(child_self.class.send('mocked_class'))].each do |type, relations|
    relations.each do |relation|
      if item.send(relation).class <= Collection
        item.send(relation) << child_self
      else
        item.send(:write_association, relation, child_self)
      end
    end
  end
end