Class: BoatSerializer
- Inherits:
-
AbstractSerializer
- Object
- AbstractSerializer
- BoatSerializer
- Defined in:
- app/serializers/boat_serializer.rb
Class Method Summary collapse
- .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_photos(boat) ⇒ Object
- .boat_type(boat) ⇒ Object
- .builder(boat) ⇒ Object
- .default_opts ⇒ Object
- .equipment(boat) ⇒ Object
- .equipment_attributes ⇒ Object
- .exterior_color(boat) ⇒ Object
- .hull_material(boat) ⇒ Object
- .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
-
.sale_price(boat) ⇒ Object
noinspection RubyResolve.
- .serialize(model, attributes: available_attributes, opts: {}) ⇒ Object
- .serialize_opts ⇒ Object
- .short_description(boat) ⇒ Object
- .toys(boat) ⇒ Object
- .toys_attributes ⇒ Object
Methods inherited from AbstractSerializer
Class Method Details
.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 |
# File 'app/serializers/boat_serializer.rb', line 4 def available_attributes i[ boat_beam boat_beam_metrics boat_contacts boat_draft boat_draft_metrics boat_hull_id boat_length boat_length_metrics boat_length_metrics_ft boat_length_metrics_meters boat_model boat_photos boat_type boat_type_id builder built_year crew_total crew_cabins description exterior_color equipment guest_cabins guest_heads guest_sleeps guests_total hull_material id interior_designer latitude location_address 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 sale_price short_description state toys visits_today visits_total ].freeze end |
.boat_beam_metrics(boat) ⇒ Object
для формы редактирования в кабинете агента
255 256 257 258 259 260 261 262 |
# File 'app/serializers/boat_serializer.rb', line 255 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` из админской формы
356 357 358 359 360 361 362 363 |
# File 'app/serializers/boat_serializer.rb', line 356 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
для формы редактирования в кабинете агента
265 266 267 268 269 270 271 272 |
# File 'app/serializers/boat_serializer.rb', line 265 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-блоке на сайте
275 276 277 278 279 280 281 282 |
# File 'app/serializers/boat_serializer.rb', line 275 def boat_length(boat) { boat_length: { value: boat.send('boat_length_metrics_%s' % @opts[:uol]), uol: @opts[:uol] } } end |
.boat_length_metrics(boat) ⇒ Object
для формы редактирования в кабинете агента
285 286 287 |
# File 'app/serializers/boat_serializer.rb', line 285 def boat_length_metrics(boat) { boat_length_metrics: boat_length(boat)[:boat_length] } end |
.boat_length_metrics_ft(boat) ⇒ Object
- TODO
- bcff00da
-
УДАЛИТЬ - пока используется при обновлении/создании лодки в админке
297 298 299 |
# File 'app/serializers/boat_serializer.rb', line 297 def boat_length_metrics_ft(boat) { boat_length_metrics_ft: boat.boat_length_metrics_ft } end |
.boat_length_metrics_meters(boat) ⇒ Object
- TODO
- bcff00da
-
УДАЛИТЬ - пока используется при обновлении/создании лодки в админке
292 293 294 |
# File 'app/serializers/boat_serializer.rb', line 292 def boat_length_metrics_meters(boat) { boat_length_metrics_meters: boat.boat_length_metrics_meters } end |
.boat_photos(boat) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'app/serializers/boat_serializer.rb', line 342 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_type(boat) ⇒ Object
245 246 247 248 249 250 251 252 |
# File 'app/serializers/boat_serializer.rb', line 245 def boat_type(boat) { boat_type: { id: boat.boat_type_id, title: boat.boat_type&.name&.strip } } end |
.builder(boat) ⇒ Object
174 175 176 |
# File 'app/serializers/boat_serializer.rb', line 174 def builder(boat) { builder: boat.builder&.strip } end |
.default_opts ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'app/serializers/boat_serializer.rb', line 57 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
137 138 139 140 141 142 143 |
# File 'app/serializers/boat_serializer.rb', line 137 def equipment(boat) { equipment: equipment_attributes.map do |f| [f, boat.send(f)] end.to_h } end |
.equipment_attributes ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/serializers/boat_serializer.rb', line 115 def equipment_attributes i[ apple_tv autopilot cd_player chartplotter cockpit_loudspeakers entertainment excellent_sound_system gps internet lcd_tv radar satellite_internet satellite_phone tv vhf wi_fi ] end |
.exterior_color(boat) ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'app/serializers/boat_serializer.rb', line 184 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 |
.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
225 226 227 228 229 230 231 232 233 |
# File 'app/serializers/boat_serializer.rb', line 225 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 |
.main_boat_photo_id(boat) ⇒ Object
166 167 168 169 170 171 172 |
# File 'app/serializers/boat_serializer.rb', line 166 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
156 157 158 159 160 161 162 163 164 |
# File 'app/serializers/boat_serializer.rb', line 156 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
146 147 148 149 150 151 152 153 154 |
# File 'app/serializers/boat_serializer.rb', line 146 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
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'app/serializers/boat_serializer.rb', line 301 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
322 323 324 325 326 327 328 329 330 |
# File 'app/serializers/boat_serializer.rb', line 322 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
235 236 237 238 239 240 241 242 243 |
# File 'app/serializers/boat_serializer.rb', line 235 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 |
.sale_price(boat) ⇒ Object
noinspection RubyResolve
333 334 335 336 337 338 339 340 |
# File 'app/serializers/boat_serializer.rb', line 333 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
67 68 69 70 71 |
# File 'app/serializers/boat_serializer.rb', line 67 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
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'app/serializers/boat_serializer.rb', line 365 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
178 179 180 181 182 |
# File 'app/serializers/boat_serializer.rb', line 178 def short_description(boat) attr = 'short_description_%s' % @opts[:locale] value = boat.send(attr.to_sym) { short_description: value } end |
.toys(boat) ⇒ Object
107 108 109 110 111 112 113 |
# File 'app/serializers/boat_serializer.rb', line 107 def toys(boat) { toys: toys_attributes.map do |f| [f, boat.send(f)] end.to_h } end |
.toys_attributes ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/serializers/boat_serializer.rb', line 82 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 |