Module: DeepUnrest::Concerns::MapTempIds

Extended by:
ActiveSupport::Concern
Defined in:
lib/deep_unrest/concerns/map_temp_ids.rb

Instance Method Summary collapse

Instance Method Details

#attribute_diffObject



59
60
61
62
63
# File 'lib/deep_unrest/concerns/map_temp_ids.rb', line 59

def attribute_diff
  saved_changes.each_with_object({}) do |(attr_name, (_old, val)), diff|
    diff[attr_name] = val
  end
end

#map_temp_idObject



21
22
23
24
25
26
27
# File 'lib/deep_unrest/concerns/map_temp_ids.rb', line 21

def map_temp_id
  temp_id_map = DeepUnrest::ApplicationController.class_variable_get(
    '@@temp_ids'
  )
  return unless temp_id_map && @deep_unrest_temp_id
  temp_id_map[@deep_unrest_context][@deep_unrest_temp_id] = pk
end

#pkObject



17
18
19
# File 'lib/deep_unrest/concerns/map_temp_ids.rb', line 17

def pk
  send self.class.primary_key
end

#track_changesObject

the client needs to know which items were charged so it can keep its local sync in store with the db



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/deep_unrest/concerns/map_temp_ids.rb', line 46

def track_changes
  changed = DeepUnrest::ApplicationController.class_variable_get(
    '@@changed_entities'
  )
  return unless changed && saved_changes?
  changed << {
    klass: self.class,
    id: pk,
    attributes: attribute_diff,
    query_uuid: @deep_unrest_query_uuid
  }
end

#track_destructionObject

the client needs to know which items were destroyed so it can clean up the dead entities from its local store



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/deep_unrest/concerns/map_temp_ids.rb', line 31

def track_destruction
  destroyed = DeepUnrest::ApplicationController.class_variable_get(
    '@@destroyed_entities'
  )
  return unless destroyed
  destroyed << {
    type: self.class.to_s.pluralize.camelize(:lower),
    id: pk,
    destroyed: true,
    query_uuid: @deep_unrest_query_uuid
  }
end