Class: C80MapFloors::MapBuilding

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveModel::Dirty
Defined in:
app/models/c80_map_floors/map_building.rb

Overview

noinspection RubyResolve

Instance Method Summary collapse

Instance Method Details

#areasObject



23
24
25
# File 'app/models/c80_map_floors/map_building.rb', line 23

def areas
  C80MapFloors::Areas.joins(:c80_map_floors_floors).where(:building_id => self.if)
end

#calc_coords_imgObject

def serializable_hash(options = nil)

  super({
            :except => [:created_at,:updated_at,:building_representator_type, :building_representator_id],
            :methods => [:class_name],
            :include => [
                :floors => {
                    :except => [:created_at,:updated_at],
                    :methods => [:class_name, :img_bg_width, :img_bg_height],
                    :include => [
                        :areas => {
                            :except => [:created_at,:updated_at,:area_representator_type],
                            :methods => :class_name
                        }
                    ]
                }
            ]
        }.merge(options || {} ))
end


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
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/c80_map_floors/map_building.rb', line 79

def calc_coords_img

  cs = self.coords.split(',') # 511,71,511,71,497,88,497,110,865,196,865,172,876,155

  #-> Рассчитаем весь bounding box, но вернём только верхний левый угол

  xmin = Integer::MAX.to_f
  ymin = Integer::MAX.to_f
  xmax = Integer::MIN.to_f
  ymax = Integer::MIN.to_f

  (0..cs.count-1).step(2) do |i|

    ix = cs[i].to_f
    iy = cs[i+1].to_f

    # Rails.logger.debug "[TRACE] <map_building.calc> #{ix}, #{iy}"

    xmin = ix < xmin ? ix : xmin
    ymin = iy < ymin ? iy : ymin

    xmax = ix > xmax ? ix : xmax
    ymax = iy > ymax ? iy : ymax

  end

  # bbox = {
  #     xmin: xmin,
  #     ymin: ymin,
  #     xmax: xmax,
  #     ymax: ymax
  # }

  str = [
      '%.01f' % xmin,
      '%.01f' % ymin
  ].join(',')

  self.update_column(:coords_img, str)

end

#my_as_json5Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/c80_map_floors/map_building.rb', line 27

def my_as_json5

  result = {
      id: self.id,
      title: self.title,
      tag: self.tag,
      class_name: self.class_name,
      coords_img: self.coords_img,
      coords: self.coords,
      floors: [],
      data: nil
  }

  self.floors.each do |floor|
    result[:floors] << floor.my_as_json
  end

  if self.building_representator.present?
    result[:data] = self.building_representator.my_as_json6
  end

  result.as_json

end