Module: MassiveRecord::ORM::Persistence::Operations::Embedded::OperationHelpers

Included in:
Destroy, Insert, Reload, Update
Defined in:
lib/massive_record/orm/persistence/operations/embedded/operation_helpers.rb

Instance Method Summary collapse

Instance Method Details

#embedded_in_proxiesObject



7
8
9
# File 'lib/massive_record/orm/persistence/operations/embedded/operation_helpers.rb', line 7

def embedded_in_proxies
  record.relation_proxies.select { |proxy| proxy..embedded_in? }
end

#embedded_in_proxy_targetsObject



11
12
13
# File 'lib/massive_record/orm/persistence/operations/embedded/operation_helpers.rb', line 11

def embedded_in_proxy_targets
  embedded_in_proxies.collect(&:load_proxy_target).compact
end

#inverse_proxy_for(proxy) ⇒ Object



47
48
49
# File 'lib/massive_record/orm/persistence/operations/embedded/operation_helpers.rb', line 47

def inverse_proxy_for(proxy)
  proxy.load_proxy_target.send(:relation_proxy, proxy..inverse_of)
end

#raise_error_if_embedded_in_proxy_targets_are_missingObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/massive_record/orm/persistence/operations/embedded/operation_helpers.rb', line 15

def raise_error_if_embedded_in_proxy_targets_are_missing
  relations_not_assigned = []
  relations_assigned = []

  embedded_in_proxies.each do |proxy|
    if proxy.load_proxy_target.nil? 
      relations_not_assigned << proxy..name
    else
      relations_assigned << proxy..name
    end
  end

  if relations_assigned.empty?
    raise MassiveRecord::ORM::NotAssignedToEmbeddedCollection.new(record, relations_not_assigned)
  end
end

#row_for_record(record) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
# File 'lib/massive_record/orm/persistence/operations/embedded/operation_helpers.rb', line 52

def row_for_record(record)
  raise IdMissing.new("You must set an ID before save.") if record.id.blank?

  MassiveRecord::Wrapper::Row.new({
    :id => record.id,
    :table => record.class.table
  })
end

#update_embedded(relation_proxy, value) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/massive_record/orm/persistence/operations/embedded/operation_helpers.rb', line 32

def update_embedded(relation_proxy, value)
  row = row_for_record(relation_proxy)
  row.values = {
    inverse_proxy_for(relation_proxy)..store_in => {
      record.database_id => value
    }
  }
  row.save
end