Class: BoatSerializer

Inherits:
AbstractSerializer show all
Defined in:
app/serializers/boat_serializer.rb

Class Method Summary collapse

Methods inherited from AbstractSerializer

opts

Class Method Details

.available_attributesObject



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
# File 'app/serializers/boat_serializer.rb', line 4

def available_attributes
  i[
    boat_beam
    boat_beam_metrics
    boat_beam_metrics_ft
    boat_beam_metrics_meters
    boat_contacts
    boat_draft
    boat_draft_metrics
    boat_draft_metrics_ft
    boat_draft_metrics_meters
    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
    description
    exterior_color
    guest_cabins
    guests_total
    hull_material
    id
    interior_designer
    latitude
    location_address
    longitude
    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
    visits_today
    visits_total
  ].freeze
end

.boat_contacts(boat) ⇒ Object

Контакты владельцев: то, что приходит в boat_users_attributes из админской формы



515
516
517
518
519
520
521
522
# File 'app/serializers/boat_serializer.rb', line 515

def boat_contacts(boat)
  boat_contacts = boat.boat_users.map do |user|
    {
      user_id: user.user_id
    }
  end
  { boat_contacts: boat_contacts }
end

.boat_hull_id(boat) ⇒ Object

TODO
bcff00da

УДАЛИТЬ

def boat_draft_metrics_meters(boat)

{ boat_draft_metrics_meters: boat.boat_draft_metrics_meters }

end



300
301
302
# File 'app/serializers/boat_serializer.rb', line 300

def boat_hull_id(boat)
  { boat_hull_id: boat.boat_hull_id }
end

.boat_length(boat) ⇒ Object



241
242
243
244
245
246
247
248
# File 'app/serializers/boat_serializer.rb', line 241

def boat_length(boat)
  {
      boat_length: {
          value: boat.send('boat_length_metrics_%s' % @opts[:uol]),
          uol:   @opts[:uol]
      }
  }
end

.boat_length_metrics_ft(boat) ⇒ Object

TODO
bcff00da

УДАЛИТЬ - пока используется при обновлении/создании лодки в админке



264
265
266
# File 'app/serializers/boat_serializer.rb', line 264

def boat_length_metrics_ft(boat)
  { boat_length_metrics_ft: boat.boat_length_metrics_ft }
end

.boat_length_metrics_meters(boat) ⇒ Object

TODO
bcff00da

УДАЛИТЬ - пока используется при обновлении/создании лодки в админке



259
260
261
# File 'app/serializers/boat_serializer.rb', line 259

def boat_length_metrics_meters(boat)
  { boat_length_metrics_meters: boat.boat_length_metrics_meters }
end

.boat_model(boat) ⇒ Object



212
213
214
# File 'app/serializers/boat_serializer.rb', line 212

def boat_model(boat)
  { boat_model: boat.boat_model }
end

.boat_photos(boat) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
# File 'app/serializers/boat_serializer.rb', line 501

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



232
233
234
235
236
237
238
239
# File 'app/serializers/boat_serializer.rb', line 232

def boat_type(boat)
  {
      boat_type: {
          id:     boat.boat_type_id,
          title:  boat.boat_type&.name&.strip
      }
  }
end

.boat_type_id(boat) ⇒ Object



228
229
230
# File 'app/serializers/boat_serializer.rb', line 228

def boat_type_id(boat)
  { boat_type_id: boat.boat_type_id }
end

.builder(boat) ⇒ Object



116
117
118
# File 'app/serializers/boat_serializer.rb', line 116

def builder(boat)
  { builder: boat.builder&.strip }
end

.built_year(boat) ⇒ Object



203
204
205
# File 'app/serializers/boat_serializer.rb', line 203

def built_year(boat)
  { built_year: boat.built_year }
end

.crew_total(boat) ⇒ Object



199
200
201
# File 'app/serializers/boat_serializer.rb', line 199

def crew_total(boat)
  { crew_total: boat.crew_total }
end

.default_optsObject



55
56
57
58
59
60
61
62
63
# File 'app/serializers/boat_serializer.rb', line 55

def default_opts
  {
    uol:         'ft',
    currency:    ::Dicts::Currency::USD.index,
    locale:      'en',
    is_for_rent: true,  # TODO:: [bcff00da] УДАЛИТЬ
    business:    ::Dicts::Business::RENT.index
  }
end

.description(boat) ⇒ Object



120
121
122
# File 'app/serializers/boat_serializer.rb', line 120

def description(boat)
  { description: boat.description }
end

.exterior_color(boat) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'app/serializers/boat_serializer.rb', line 130

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

.guest_cabins(boat) ⇒ Object



175
176
177
# File 'app/serializers/boat_serializer.rb', line 175

def guest_cabins(boat)
  { guest_cabins: boat.guest_cabins }
end

.guests_total(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



171
172
173
# File 'app/serializers/boat_serializer.rb', line 171

def guests_total(boat)
  { guests_total: boat.guests_total }
end

.hull_material(boat) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'app/serializers/boat_serializer.rb', line 179

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

.id(boat) ⇒ Object



71
72
73
# File 'app/serializers/boat_serializer.rb', line 71

def id(boat)
  { id: boat.id }
end

.interior_designer(boat) ⇒ Object



75
76
77
# File 'app/serializers/boat_serializer.rb', line 75

def interior_designer(boat)
  { interior_designer: boat.interior_designer }
end

.latitude(boat) ⇒ Object



220
221
222
# File 'app/serializers/boat_serializer.rb', line 220

def latitude(boat)
  { latitude: boat.latitude }
end

.location_address(boat) ⇒ Object



216
217
218
# File 'app/serializers/boat_serializer.rb', line 216

def location_address(boat)
  { location_address: boat.location_address }
end

.longitude(boat) ⇒ Object



224
225
226
# File 'app/serializers/boat_serializer.rb', line 224

def longitude(boat)
  { longitude: boat.longitude }
end

.main_boat_photo_id(boat) ⇒ Object



108
109
110
111
112
113
114
# File 'app/serializers/boat_serializer.rb', line 108

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



98
99
100
101
102
103
104
105
106
# File 'app/serializers/boat_serializer.rb', line 98

def motor(boat)
  m = ::Dicts::Motor.find boat.motor
  {
      motor: {
          id:     (m.id    rescue nil),
          index:  (m.index rescue 'null')                                                                           # 'null' - индекс в локалях
      }
  }
end

.name(boat) ⇒ Object



94
95
96
# File 'app/serializers/boat_serializer.rb', line 94

def name(boat)
  { name: boat.name }
end


79
80
81
# File 'app/serializers/boat_serializer.rb', line 79

def naval_architect(boat)
  { naval_architect: boat.naval_architect }
end

.picture_medium_url(boat) ⇒ Object

noinspection RubyResolve



84
85
86
87
88
89
90
91
92
# File 'app/serializers/boat_serializer.rb', line 84

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



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'app/serializers/boat_serializer.rb', line 304

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

.refit_year(boat) ⇒ Object



207
208
209
210
# File 'app/serializers/boat_serializer.rb', line 207

def refit_year(boat)
  value = boat.refit_year
  { refit_year: value }
end

.rent_prices(boat) ⇒ Object



325
326
327
328
329
330
331
332
333
# File 'app/serializers/boat_serializer.rb', line 325

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



189
190
191
192
193
194
195
196
197
# File 'app/serializers/boat_serializer.rb', line 189

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



336
337
338
339
340
341
342
343
# File 'app/serializers/boat_serializer.rb', line 336

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



65
66
67
68
69
# File 'app/serializers/boat_serializer.rb', line 65

def serialize(model, attributes: available_attributes, opts: {})
  result = super(model, attributes: attributes, opts: default_opts.merge(opts))
  result[:opts] = serialize_opts
  result
end

.serialize_optsObject



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'app/serializers/boat_serializer.rb', line 532

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



124
125
126
127
128
# File 'app/serializers/boat_serializer.rb', line 124

def short_description(boat)
  attr  = 'short_description_%s' % @opts[:locale]
  value = boat.send(attr.to_sym)
  { short_description: value }
end

.state(boat) ⇒ Object

def sale_price_discount(boat)

{ sale_price_discount: boat.sale_price_discount }

end



497
498
499
# File 'app/serializers/boat_serializer.rb', line 497

def state(boat)
  { state: boat.state }
end

.visits_today(boat) ⇒ Object



524
525
526
# File 'app/serializers/boat_serializer.rb', line 524

def visits_today(boat)
  { visits_today: boat.visits_today }
end

.visits_total(boat) ⇒ Object



528
529
530
# File 'app/serializers/boat_serializer.rb', line 528

def visits_total(boat)
  { visits_total: boat.visits_total }
end