Class: BoatSerializer
- Inherits:
-
AbstractSerializer
- Object
- AbstractSerializer
- BoatSerializer
- Defined in:
- app/serializers/boat_serializer.rb
Class Method Summary collapse
- .amenities(boat) ⇒ Object
- .amenities_attributes ⇒ Object
- .available_attributes ⇒ Object
-
.boat_beam_metrics(boat) ⇒ Object
для формы редактирования в кабинете агента.
-
.boat_contacts(boat) ⇒ Object
Контакты владельцев: то, что приходит в
boat_users_attributesиз админской формы. -
.boat_draft_metrics(boat) ⇒ Object
для формы редактирования в кабинете агента.
-
.boat_length(boat) ⇒ Object
используется в списке лодок агента, в preview-блоке на сайте.
-
.boat_length_metrics(boat) ⇒ Object
для формы редактирования в кабинете агента.
-
.boat_length_metrics_ft(boat) ⇒ Object
- TODO
- bcff00da
-
УДАЛИТЬ - пока используется при обновлении/создании лодки в админке.
-
.boat_length_metrics_meters(boat) ⇒ Object
- TODO
- bcff00da
-
УДАЛИТЬ - пока используется при обновлении/создании лодки в админке.
-
.boat_locations_attributes(boat) ⇒ Object
используется для валидации лодки при обновлении стейта.
- .boat_photos(boat) ⇒ Object
-
.boat_photos_attributes(boat) ⇒ Object
используется для валидации лодки при обновлении стейта.
-
.boat_stars(boat) ⇒ Object
выдаем 1 самое свежее значение.
-
.boat_thumbups(boat) ⇒ Object
выдаем 1 самое свежее значение.
- .boat_type(boat) ⇒ Object
- .builder(boat) ⇒ Object
- .charter_deposit(boat) ⇒ Object
- .charter_details(boat) ⇒ Object
- .default_opts ⇒ Object
- .equipment(boat) ⇒ Object
- .equipment_attributes ⇒ Object
- .exterior_color(boat) ⇒ Object
- .for_rent(boat) ⇒ Object
- .for_sale(boat) ⇒ Object
- .fuel_type(boat) ⇒ Object
- .hull_material(boat) ⇒ Object
-
.latitude(boat) ⇒ Object
под видом этого атрибута будет приходить число - latitude главной локации.
-
.length(boat) ⇒ Object
используется для валидации лодки при обновлении стейта.
-
.location_address(boat) ⇒ Object
под видом этого атрибута будет приходить текст - название главной локации.
- .locations(boat) ⇒ Object
-
.longitude(boat) ⇒ Object
под видом этого атрибута будет приходить число - longitude главной локации.
- .main_boat_photo_id(boat) ⇒ Object
- .motor(boat) ⇒ Object
-
.picture_medium_url(boat) ⇒ Object
noinspection RubyResolve.
- .prices(boat) ⇒ Object
- .rent_prices(boat) ⇒ Object
- .rental_type(boat) ⇒ Object
-
.reviews(boat) ⇒ Object
- TODO
-
impelement it.
- .safety(boat) ⇒ Object
- .safety_attributes ⇒ Object
-
.sale_price(boat) ⇒ Object
noinspection RubyResolve.
- .serialize(model, attributes: available_attributes, opts: {}) ⇒ Object
- .serialize_opts ⇒ Object
- .short_description(boat) ⇒ Object
- .state(boat) ⇒ Object
- .toys(boat) ⇒ Object
- .toys_attributes ⇒ Object
Methods inherited from AbstractSerializer
Class Method Details
.amenities(boat) ⇒ Object
211 212 213 214 215 216 217 |
# File 'app/serializers/boat_serializer.rb', line 211 def amenities(boat) { amenities: amenities_attributes.map do |f| [f, boat.send(f)] end.to_h } end |
.amenities_attributes ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'app/serializers/boat_serializer.rb', line 161 def amenities_attributes %i[ air_conditioner bow_thruster electric_toilet essentials family_kid_friendly gym hardtop heating helipad hot_cold_cockpit_shower jacuzzi_on_deck kitchen pet_allowed sauna soft_top stabilisers_underway stabilizers_at_anchor steam_room toilet ].freeze end |
.available_attributes ⇒ Object
4 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 |
# File 'app/serializers/boat_serializer.rb', line 4 def available_attributes %i[ amenities boat_beam boat_beam_metrics boat_contacts boat_cruise_speed boat_draft boat_draft_metrics boat_engines boat_hull_id boat_generators boat_length boat_length_metrics boat_length_metrics_ft boat_length_metrics_meters boat_locations_attributes boat_max_speed boat_model boat_photos boat_photos_attributes boat_range boat_stars boat_thumbups boat_type boat_type_id builder built_year charter_details charter_deposit crew_total crew_cabins description engine_hours exterior_color equipment for_rent for_sale fuel_type guest_cabins guest_heads guests_total hull_material id interior_designer length latitude location_address locations longitude maximum_guests_during_cruise main_boat_photo_id motor motor_id name naval_architect picture_medium_url prices refit_year rent_prices rental_type reviews safety sale_price short_description state toys visits_today visits_total ].freeze end |
.boat_beam_metrics(boat) ⇒ Object
для формы редактирования в кабинете агента
343 344 345 346 347 348 349 350 |
# File 'app/serializers/boat_serializer.rb', line 343 def boat_beam_metrics(boat) { boat_beam_metrics: { value: boat.send('boat_beam_metrics_%s' % @opts[:uol]), uol: @opts[:uol] } } end |
.boat_contacts(boat) ⇒ Object
Контакты владельцев: то, что приходит в boat_users_attributes из админской формы
463 464 465 466 467 468 469 470 |
# File 'app/serializers/boat_serializer.rb', line 463 def boat_contacts(boat) boat_contacts = boat.boat_users.map do |user| { user_id: user.user_id } end { boat_contacts: boat_contacts } end |
.boat_draft_metrics(boat) ⇒ Object
для формы редактирования в кабинете агента
353 354 355 356 357 358 359 360 |
# File 'app/serializers/boat_serializer.rb', line 353 def boat_draft_metrics(boat) { boat_draft_metrics: { value: boat.send('boat_draft_metrics_%s' % @opts[:uol]), uol: @opts[:uol] } } end |
.boat_length(boat) ⇒ Object
используется в списке лодок агента, в preview-блоке на сайте
363 364 365 366 367 368 369 370 |
# File 'app/serializers/boat_serializer.rb', line 363 def boat_length(boat) { boat_length: { value: boat.send('boat_length_metrics_%s' % @opts[:uol]), uol: @opts[:uol] } } end |
.boat_length_metrics(boat) ⇒ Object
для формы редактирования в кабинете агента
383 384 385 |
# File 'app/serializers/boat_serializer.rb', line 383 def boat_length_metrics(boat) { boat_length_metrics: boat_length(boat)[:boat_length] } end |
.boat_length_metrics_ft(boat) ⇒ Object
- TODO
- bcff00da
-
УДАЛИТЬ - пока используется при обновлении/создании лодки в админке
395 396 397 |
# File 'app/serializers/boat_serializer.rb', line 395 def boat_length_metrics_ft(boat) { boat_length_metrics_ft: boat.boat_length_metrics_ft } end |
.boat_length_metrics_meters(boat) ⇒ Object
- TODO
- bcff00da
-
УДАЛИТЬ - пока используется при обновлении/создании лодки в админке
390 391 392 |
# File 'app/serializers/boat_serializer.rb', line 390 def boat_length_metrics_meters(boat) { boat_length_metrics_meters: boat.boat_length_metrics_meters } end |
.boat_locations_attributes(boat) ⇒ Object
используется для валидации лодки при обновлении стейта
504 505 506 507 508 509 510 |
# File 'app/serializers/boat_serializer.rb', line 504 def boat_locations_attributes(boat) { boat_locations_attributes: boat.boat_locations.map do |location| ::BoatLocationSerializer.serialize location end } end |
.boat_photos(boat) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'app/serializers/boat_serializer.rb', line 440 def boat_photos(boat) # FIXME:: выяснить, нужно ли всётаки использовать .gsub('\'', ''') для строк с путями к фоткам? boat_photos = boat.boat_photos.map do |boat_photo| { id: boat_photo.id, gallery_path: boat_photo.picture.url(:gallery), medium_path: boat_photo.picture.url(:medium), thumb_path: boat_photo.picture.url(:thumb) } end { boat_photos: boat_photos } end |
.boat_photos_attributes(boat) ⇒ Object
используется для валидации лодки при обновлении стейта
454 455 456 457 458 459 460 |
# File 'app/serializers/boat_serializer.rb', line 454 def boat_photos_attributes(boat) { boat_photos_attributes: boat.boat_photos.map do |boat_photo| { id: boat_photo.id } end } end |
.boat_stars(boat) ⇒ Object
выдаем 1 самое свежее значение
529 530 531 |
# File 'app/serializers/boat_serializer.rb', line 529 def boat_stars(boat) { boat_stars: boat.boat_stars.max_by(&:created_at)&.value } end |
.boat_thumbups(boat) ⇒ Object
выдаем 1 самое свежее значение
534 535 536 |
# File 'app/serializers/boat_serializer.rb', line 534 def boat_thumbups(boat) { boat_thumbups: boat.boat_thumbups.max_by(&:created_at)&.value } end |
.boat_type(boat) ⇒ Object
333 334 335 336 337 338 339 340 |
# File 'app/serializers/boat_serializer.rb', line 333 def boat_type(boat) { boat_type: { id: boat.boat_type_id, title: boat.boat_type&.name&.strip } } end |
.builder(boat) ⇒ Object
256 257 258 |
# File 'app/serializers/boat_serializer.rb', line 256 def builder(boat) { builder: boat.builder&.strip } end |
.charter_deposit(boat) ⇒ Object
524 525 526 |
# File 'app/serializers/boat_serializer.rb', line 524 def charter_deposit(boat) { charter_deposit: boat.charter_deposit } end |
.charter_details(boat) ⇒ Object
520 521 522 |
# File 'app/serializers/boat_serializer.rb', line 520 def charter_details(boat) { charter_details: boat.charter_details } end |
.default_opts ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'app/serializers/boat_serializer.rb', line 76 def default_opts { uol: 'ft', currency: ::Dicts::Currency::USD.index, locale: 'en', is_for_rent: true, # TODO:: [bcff00da] УДАЛИТЬ business: ::Dicts::Business::RENT.index } end |
.equipment(boat) ⇒ Object
203 204 205 206 207 208 209 |
# File 'app/serializers/boat_serializer.rb', line 203 def equipment(boat) { equipment: equipment_attributes.map do |f| [f, boat.send(f)] end.to_h } end |
.equipment_attributes ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'app/serializers/boat_serializer.rb', line 139 def equipment_attributes %i[ apple_tv autopilot cd_player chartplotter cockpit_loudspeakers entertainment excellent_sound_system gps internet lcd_tv navigation radar satellite_internet satellite_phone tv vhf wi_fi ].freeze end |
.exterior_color(boat) ⇒ Object
266 267 268 269 270 271 272 273 274 |
# File 'app/serializers/boat_serializer.rb', line 266 def exterior_color(boat) color = ::Dicts::ExteriorColor.find boat.exterior_color { exterior_color: { id: (color.id rescue nil), index: (color.index rescue 'null') # 'null' - индекс в локалях } } end |
.for_rent(boat) ⇒ Object
512 513 514 |
# File 'app/serializers/boat_serializer.rb', line 512 def for_rent(boat) { for_rent: boat.for_rent.to_i == 1 } end |
.for_sale(boat) ⇒ Object
516 517 518 |
# File 'app/serializers/boat_serializer.rb', line 516 def for_sale(boat) { for_sale: boat.for_sale.to_i == 1 } end |
.fuel_type(boat) ⇒ Object
327 328 329 330 331 |
# File 'app/serializers/boat_serializer.rb', line 327 def fuel_type(boat) { fuel_type: boat.fuel_type } end |
.hull_material(boat) ⇒ Object
12% def price_discount(boat)
value = if @opts[:is_for_rent]
::Lib::Boats::RentPricePerSeason.price_discount(boat, @opts[:currency])
else
::Lib::Boats::SalePrice.price_discount(boat)
end
{ price_discount: value }
end
307 308 309 310 311 312 313 314 315 |
# File 'app/serializers/boat_serializer.rb', line 307 def hull_material(boat) hm = ::Dicts::HullMaterial.find boat.hull_material { hull_material: { id: (hm.id rescue nil), index: (hm.index rescue 'null') # 'null' - индекс в локалях } } end |
.latitude(boat) ⇒ Object
под видом этого атрибута будет приходить число - latitude главной локации
473 474 475 476 477 |
# File 'app/serializers/boat_serializer.rb', line 473 def latitude(boat) loc = boat.boat_locations.select { |bl| bl.is_main }.first loc ||= boat.boat_locations.first { latitude: loc&.latitude || 0 } end |
.length(boat) ⇒ Object
используется для валидации лодки при обновлении стейта
373 374 375 376 377 378 379 380 |
# File 'app/serializers/boat_serializer.rb', line 373 def length(boat) { length: { value: boat.send('boat_length_metrics_%s' % @opts[:uol]), length_id: ::Dicts::Length.find_by_index(@opts[:uol]).id } } end |
.location_address(boat) ⇒ Object
под видом этого атрибута будет приходить текст - название главной локации
487 488 489 490 491 492 493 |
# File 'app/serializers/boat_serializer.rb', line 487 def location_address(boat) loc = boat.boat_locations.select { |bl| bl.is_main }.first # если не применяется поиск по адресу loc ||= boat.boat_locations.first # если поиск по адресу применён -- в результат поиска может не попасть главная локация { location_address: loc&.address || '' } end |
.locations(boat) ⇒ Object
495 496 497 498 499 500 501 |
# File 'app/serializers/boat_serializer.rb', line 495 def locations(boat) { locations: boat.boat_locations.map do |location| ::BoatLocationSerializer.serialize location end } end |
.longitude(boat) ⇒ Object
под видом этого атрибута будет приходить число - longitude главной локации
480 481 482 483 484 |
# File 'app/serializers/boat_serializer.rb', line 480 def longitude(boat) loc = boat.boat_locations.select { |bl| bl.is_main }.first loc ||= boat.boat_locations.first { longitude: loc&.longitude || 0 } end |
.main_boat_photo_id(boat) ⇒ Object
248 249 250 251 252 253 254 |
# File 'app/serializers/boat_serializer.rb', line 248 def main_boat_photo_id(boat) boat_photo_id = if boat.boat_photo.present? boat.boat_photo.id end { main_boat_photo_id: boat_photo_id } end |
.motor(boat) ⇒ Object
238 239 240 241 242 243 244 245 246 |
# File 'app/serializers/boat_serializer.rb', line 238 def motor(boat) m = ::Dicts::Motor.find boat.motor { motor: { id: (m.id rescue nil), index: (m.index rescue 'null') # 'null' - индекс в локалях } } end |
.picture_medium_url(boat) ⇒ Object
noinspection RubyResolve
228 229 230 231 232 233 234 235 236 |
# File 'app/serializers/boat_serializer.rb', line 228 def picture_medium_url(boat) url = if boat.boat_photo.present? boat.boat_photo.picture.url(:medium) else '' end { picture_medium_url: url } end |
.prices(boat) ⇒ Object
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'app/serializers/boat_serializer.rb', line 399 def prices(boat) currency_id = ::Dicts::Currency.find_by_index(@opts[:currency]).id business = ::Dicts::Business.find_by_index @opts[:business] prices = if business.index == ::Dicts::Business::RENT.index boat.boat_prices.select { |bp| bp.currency_id == currency_id }.map { |bp| ::BoatPriceSerializer.serialize bp } else # noinspection RubyResolve boat.boat_sale_prices.select { |bp| bp.currency_id == currency_id }.map { |bps| ::BoatSalePriceSerializer.serialize bps } end { prices: { list: prices, business: { index: business.index, id: business.id } } } end |
.rent_prices(boat) ⇒ Object
420 421 422 423 424 425 426 427 428 |
# File 'app/serializers/boat_serializer.rb', line 420 def rent_prices(boat) currency_id = ::Dicts::Currency.find_by_index(@opts[:currency]).id { rent_prices: boat.boat_prices .select { |bp| bp.currency_id == currency_id } .map.each_with_index { |bp, index| ::BoatPriceSerializer.serialize(bp).merge(id:index) } } end |
.rental_type(boat) ⇒ Object
317 318 319 320 321 322 323 324 325 |
# File 'app/serializers/boat_serializer.rb', line 317 def rental_type(boat) hm = ::Dicts::RentSkipType.find boat.rental_type { rental_type: { id: (hm.id rescue nil), index: (hm.index rescue 'null') # 'null' - индекс в локалях } } end |
.reviews(boat) ⇒ Object
- TODO
-
impelement it
539 540 541 |
# File 'app/serializers/boat_serializer.rb', line 539 def reviews(boat) { reviews: [] } end |
.safety(boat) ⇒ Object
219 220 221 222 223 224 225 |
# File 'app/serializers/boat_serializer.rb', line 219 def safety(boat) { safety: safety_attributes.map do |f| [f, boat.send(f)] end.to_h } end |
.safety_attributes ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'app/serializers/boat_serializer.rb', line 185 def safety_attributes %i[ carbon_detector epirb fire_extinguisher first_aid_kit flares horn lifebuoy life_jackets personal_flotation_devices raft smoke_detector vessel_lighting visual_distress_signals ].freeze end |
.sale_price(boat) ⇒ Object
noinspection RubyResolve
431 432 433 434 435 436 437 438 |
# File 'app/serializers/boat_serializer.rb', line 431 def sale_price(boat) currency_id = ::Dicts::Currency.find_by_index(@opts[:currency]).id sale_price = boat.boat_sale_prices.select { |bp| bp.currency_id == currency_id }.first { sale_price: (::BoatSalePriceSerializer.serialize sale_price rescue nil) } end |
.serialize(model, attributes: available_attributes, opts: {}) ⇒ Object
86 87 88 89 90 |
# File 'app/serializers/boat_serializer.rb', line 86 def serialize(model, attributes: available_attributes, opts: {}) result = super(model, attributes: attributes, opts: default_opts.merge(opts)) result[:opts] = serialize_opts result end |
.serialize_opts ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'app/serializers/boat_serializer.rb', line 543 def serialize_opts locale = ::Dicts::Locale.find_by_index @opts[:locale].to_s uol = ::Dicts::Length.find_by_index @opts[:uol] business = ::Dicts::Business.find_by_index @opts[:business] currency = ::Dicts::Currency.find_by_index @opts[:currency] { locale: { index: locale.index, id: locale.id }, uol: { index: uol.index, id: uol.id }, business: { index: business.index, id: business.id }, currency: { index: currency.index, id: currency.id, symbol: currency.symbol } } end |
.short_description(boat) ⇒ Object
260 261 262 263 264 |
# File 'app/serializers/boat_serializer.rb', line 260 def short_description(boat) attr = 'short_description_%s' % @opts[:locale] value = boat.send(attr.to_sym) { short_description: value } end |
.state(boat) ⇒ Object
102 103 104 |
# File 'app/serializers/boat_serializer.rb', line 102 def state(boat) { state: I18n.t('attributes.state_%s' % boat.state) } end |
.toys(boat) ⇒ Object
131 132 133 134 135 136 137 |
# File 'app/serializers/boat_serializer.rb', line 131 def toys(boat) { toys: toys_attributes.map do |f| [f, boat.send(f)] end.to_h } end |
.toys_attributes ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/serializers/boat_serializer.rb', line 106 def toys_attributes %i[ banana beach_kids_toys donut exercise_equipment fishing_gear flyboard inflatable_sailboat jet_ski kayak kneeboard paddle_boards sailing_boat scuba_tanks skiboard snorkelling_gear stand_up_jet_ski underwater_gear wakeboard water_ski windsurfing_board ].freeze end |