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

.update_jsonObject



5
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
# File 'app/models/c80_map/map_json.rb', line 5

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|

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

      ab = {
          id: area.id,
          object_type: 'area',
          coords: area.coords.split(','),
          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

    ob = {
        id: b.id,
        object_type: 'building',
        coords: b.coords.split(","),
        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