Class: C80Map::MapJson

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/c80_map/map_json.rb

Class Method Summary collapse

Class Method Details

.fetch_jsonObject



109
110
111
112
113
# File 'app/models/c80_map/map_json.rb', line 109

def self.fetch_json
  locations_path = Rails.root.join("public", "locations.json")
  locs = File.read(locations_path)
  JSON.parse(locs)
end

.update_jsonObject

этот метод вызовается после update Area



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/c80_map/map_json.rb', line 6

def self.update_json
  locations_path = Rails.root.join("public", "locations.json")
  locs = File.read(locations_path)
  # puts "<MapJson.update_json> #{ Rails.root.join("public", "locations.json") }"

  locs_hash = JSON.parse(locs)

  # обходим все Building и составляем массив вида
  # [{id, object_type=object_type, coords, building_hash, img, childs:[<areas>]},..]
  buildings_to_location_json = []
  Building.all.each do |b|
    # Rails.logger.debug "[TRACE] <MapJson.update_json> building: #{b}; building_representator: #{b.building_representator}"

    # сначала соберём детей - Area
    childs = []
    b.areas.each do |area|
      # Rails.logger.debug "[TRACE] <MapJson.update_json> [1] area #{area}; area_representator: #{area.area_representator}"

      # соберём хэш привязанной к полигону площади
      har = {}
      if area.area_representator.present?
        # Rails.logger.debug "[TRACE] <MapJson.update_json> [2] area #{area}; area_representator: #{area.area_representator}"
        har = area.area_representator.to_hash_a
        har["is_free"] = area.area_representator.is_free?
      end

      ab = {
          id: area.id,
          object_type: 'area',
          coords: area.coords.split(','),
          area_hash: har
          # area_hash: {
          #     id: 2,
          #     title: "Площадь #{area.id}.#{area.id}",
          #     is_free: true,
          #     props: {
          #         square: "124 кв.м.",
          #         floor_height: "6 кв. м",
          #         column_step: "2 м",
          #         gate_type: "распашные",
          #         communications: "Интернет, электричество, водоснабжение",
          #         price: "от 155 руб/кв.м в месяц"
          #     }
          # }
      }
      childs << ab
    end

    # соберём хэш привязанного к полигону здания
    hbu = {}
    if b.building_representator.present?
      hbu = b.building_representator.to_hash
      # har["is_free"] = area.area_representator.is_free?
    end

    cc = nil
    if b.coords.present?
      cc = b.coords.split(",")
    else
      Rails.logger.debug "[TRACE] <Map_json.update_json> nil! #{b.id}"
    end

    ob = {
        id: b.id,
        object_type: 'building',
        coords: cc,
        building_hash: hbu,
        # building_hash: {
        #     id: 2,
        #     title: "Здание 2",
        #     props: {
        #         square: "1234 кв.м.",
        #         square_free: "124 кв. м",
        #         floor_height: "6 кв. м",
        #         column_step: "2 м",
        #         gate_type: "рaспашные",
        #         communications: "Интернет, электричество, водоснабжение",
        #         price: "от 155 руб/кв.м в месяц"
        #     }
        # },
        img: {
            bg: {
                src: b.img_bg.url
            },
            overlay: {
                src: b.img_overlay.url
            }
        },
        childs: childs
    }
    buildings_to_location_json << ob
  end

  # просто заменяем всех детей
  locs_hash["childs"] = buildings_to_location_json

  Rails.logger.debug "<MapJson.update_json> #{locs_hash}"

  File.open(locations_path, 'w') do |f|
    f.write(locs_hash.to_json)
  end
end