Class: NetworkManager::RegionHandler

Inherits:
NetworkManager show all
Defined in:
app/services/network_manager/region_handler.rb

Overview

Synchronizing regions with central site

Constant Summary

Constants inherited from NetworkManager

MAIN_HOST

Instance Attribute Summary

Attributes inherited from NetworkManager

#data, #entity

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NetworkManager

ignored_attributes, permitted_attributes

Class Method Details

.relationship_data(entity, wrap = false) ⇒ Object

Parameters:

  • entity (Region)
  • wrap (TrueClass|FalseClass) (defaults to: false)


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/network_manager/region_handler.rb', line 7

def self.relationship_data(entity, wrap = false)
  return nil if entity.nil?

  data = {
    id: entity.id,
    type: entity.class.table_name,
    attributes: {
      slug: entity.slug
    }
  }

  wrap ? { data: data } : data
end

Instance Method Details

#create_localObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/network_manager/region_handler.rb', line 21

def create_local
  id = @data[:id]

  log_event "[I] Creating local region #{id}"

  @region = Region.new(id: id)

  apply_for_create

  new_children = [@region.id] + @region.children_cache

  @region.parent&.cache_children!(new_children) if @region.save
  @region
end

#update_localObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/network_manager/region_handler.rb', line 36

def update_local
  id = @data[:id]

  log_event "[I] Updating local region #{id}"

  @region = Region.find_by(id: id)

  if @region.nil?
    log_event "[E] Cannot find region #{id}"
    false
  else
    apply_for_update
    @region.save
  end
end