Class: C80MapFloors::AjaxController

Inherits:
ApplicationController show all
Includes:
AjaxHelper
Defined in:
app/controllers/c80_map_floors/ajax_controller.rb

Overview

noinspection RubyResolve

Instance Method Summary collapse

Methods included from AjaxHelper

#find_areas_by_category!, #find_areas_by_shop!

Instance Method Details

#_find_shopsObject

от js пришла строка с названием категории. Необходимо найти магазины, соответствующие этой категории. noinspection RailsChecklist01



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 150

def _find_shops
  Rails.logger.debug "[TRACE] <AjaxController.find_shops> params = #{params}"
  # [TRACE] <AjaxController.find_shops> params = {"stext"=>"Хозтовары", "counter"=>"1", "controller"=>"c80_map_floors/ajax", "action"=>"find_shops"}

  # ПЕРВЫЙ ВАРИАНТ
  # result = {
  #     buildings: [
  #         {   id: 7,
  #             floors: [
  #                 {  id: 2,
  #                    areas: [3]
  #                 }
  #             ]
  #         },
  #         {
  #             id: 10,
  #             floors: [
  #                 { id: 6,
  #                   areas: [5,8]
  #                 },
  #                 { id: 48,
  #                   areas: [6]
  #                 }
  #             ]
  #         }
  #     ]
  # }

  if params[:counter].to_i == 1
    result = {
        buildings:             [7, 10],
        buildings_shops_count: [3, 12],
        floors:                [5, 7, 48, 8],
        floors_shops_count:    [2, 1, 33],
        areas:                 [3, 5, 8, 6]
    }
  else
    result = {
        buildings:             [],
        buildings_shops_count: [],
        floors:                [],
        floors_shops_count:    [],
        areas:                 []
    }

    # находим 3 рандомных полигона зданий (генерим случайное число для каждого здания)
    3.times do

      map_building       = MapBuilding.offset(rand(MapBuilding.count)).first
      map_building_count = rand(20)

      result[:buildings] << map_building.id
      result[:buildings_shops_count] << map_building_count

      # в каждом полигоне здания находим один рандомный полигон этажа (генерим случайное число для каждого этажа)
      map_floor       = map_building.floors.offset(rand(map_building.floors.count)).first
      map_floor_count = rand(20)

      result[:floors] << map_floor.id
      result[:floors_shops_count] << map_floor_count

      # просто находим 4 рандомных полигонов площадей
      4.times do
        map_area = Area.offset(rand(Area.count)).first
        result[:areas] << map_area.id
      end

    end

  end

  Rails.logger.debug "[TRACE] <AjaxController.find_shops> Отправляем ответ: result = #{result}"

  respond_to do |format|
    format.json { render json: result }
  end

end

#fetch_unlinked_areasObject

прислать ВСЕ Площади указанного Этажа



14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 14

def fetch_unlinked_areas
  Rails.logger.debug "[TRACE] <AjaxController.fetch_unlinked_areas> params = #{params}"
  # <AjaxController.fetch_unlinked_areas> params = {"sfloor_id"=>"1", "controller"=>"c80_map_floors/ajax", "action"=>"fetch_unlinked_areas"}

  # Sfloor - принадлежит моделям host-проекта (в частности, stroy101km)
  # noinspection RubyResolve
  @sfloor_areas = ::Floor.find(params[:sfloor_id].to_i).areas
  Rails.logger.debug "[TRACE] <AjaxController.fetch_unlinked_areas> @sfloor_areas.count = #{@sfloor_areas.count}"

end

#fetch_unlinked_buildingsObject



39
40
41
42
43
44
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 39

def fetch_unlinked_buildings
  Rails.logger.debug "[TRACE] <AjaxController.fetch_unlinked_buildings> params = #{params}"

  @unlinked_buildings = Building.all.order_title

end

#fetch_unlinked_floorsObject

js-кнопка “связать полигон с этажом” запрашивает список несвязанных с полигонами Этажи (указанного здания)



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 26

def fetch_unlinked_floors
  Rails.logger.debug "[TRACE] <AjaxController.fetch_unlinked_floors> params = #{params}"
  # [TRACE] <AjaxController.fetch_unlinked_floors> params = {"building_id"=>"1", "controller"=>"c80_map_floors/ajax", "action"=>"fetch_unlinked_floors"}

  # меняем логику - делаем менее строгой, теперь присылаются ВСЕ этажи указанного Здания
  # @unlinked_floors = Sfloor.unlinked_floors(params[:building_id]) # Sfloor - Этаж из host app, который еще и floor_representator

  # Building - принадлежит моделям host-проекта (в частности, stroy101km)
  # noinspection RubyResolve
  @building_sfloors = Building.find(params[:building_id].to_i).sfloors

end

#find_shopsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 120

def find_shops
  Rails.logger.debug "[TRACE] <AjaxController.find_shops> params = #{params}"
  # [TRACE] <AjaxController.find_shops> params = {"stext"=>"Ванны акриловые", "counter"=>"1", }

  # result = {
  #     buildings:             [],
  #     buildings_shops_count: [],
  #     floors:                [],    #
  #     floors_shops_count:    [],
  #     areas:                 []     # сюда собираем айдишники полигонов площадей
  # }

  result = SearchResult.new

  if params[:stext][' (магазин)']
    find_areas_by_shop!(result, params[:stext])
  else
    find_areas_by_category!(result, params[:stext])
  end

  Rails.logger.debug "[TRACE] <AjaxController.find_shops> Отправляем ответ: result = #{result.data}"

  respond_to do |format|
    format.json { render json: result.data }
  end

end

связать Rent::Area и Map::Area в процессе произойдёт обновление json-файла с данными noinspection RubyResolve



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 49

def link_area
  Rails.logger.debug "[TRACE] <AjaxController.link_area> params = #{params}"
  # [TRACE] <AjaxController.link_area> params = {"area_id"=>"364", "apolygon_id"=>"3", "controller"=>"c80_map_floors/ajax", "action"=>"link_area"}

  # фиксируем участников
  rent_area = ::Area.find(params[:area_id])
  area      = C80MapFloors::Area.find(params[:apolygon_id])

  # rent_area has_one area(полигон)
  rent_area.area.delete if rent_area.area.present?
  rent_area.area = area
  rent_area.save

  result = {
      updated_locations_json: C80MapFloors::MapJson.fetch_json
  }

  respond_to do |format|
    format.json { render json: result }
  end

end

noinspection RubyResolve



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 74

def link_building

  Rails.logger.debug "[TRACE] <AjaxController.link_building> params = #{params}"
  # [TRACE] <AjaxController.link_building> params = {"rent_building_id"=>"29", "map_building_id"=>"10", "controller"=>"c80_map_floors/ajax", "action"=>"link_building"}
  rent_building = ::Building.find(params[:building_id])

  map_building = C80MapFloors::MapBuilding.find(params[:map_building_id])

  rent_building.map_building.delete if rent_building.map_building.present?
  rent_building.map_building = map_building
  rent_building.save

  result = {
      updated_locations_json: C80MapFloors::MapJson.fetch_json
  }

  respond_to do |format|
    format.json { render json: result }
  end

end

связать Этаж и Картинку Этажа (обновится JSON карты)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 97

def link_floor
  Rails.logger.debug "[TRACE] <AjaxController.link_floor> params = #{params}"
  # <AjaxController.link_floor> params = {"sfloor_id"=>"3", "floor_id"=>"2", "controller"=>"c80_map_floors/ajax", "action"=>"link_floor"}

  # фиксируем участников
  sfloor = ::Floor.find(params[:sfloor_id])
  floor  = C80MapFloors::Floor.find(params[:floor_id])

  # sfloor has_one floor
  sfloor.floor.delete if sfloor.floor.present?
  sfloor.floor = floor
  sfloor.save

  result = {
      updated_locations_json: C80MapFloors::MapJson.fetch_json
  }

  respond_to do |format|
    format.json { render json: result }
  end

end

#map_edit_buttonsObject



9
10
11
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 9

def map_edit_buttons
  # puts "<AjaxController.map_edit_buttons> #{params}"
end

#update_map_jsonObject

перегенерировать locations.json



230
231
232
# File 'app/controllers/c80_map_floors/ajax_controller.rb', line 230

def update_map_json
  MapJson.update_json
end