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
# 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
    exterior_color
    guest_cabins
    guests_total
    hull_material
    id
    interior_designer
    latitude
    location_address
    longitude
    main_boat_photo_id
    motor
    name
    naval_architect
    picture_medium_url
    prices
    refit_year
    rental_type
    short_description
    state
    visits_today
    visits_total
  ].freeze
end

.boat_contacts(boat) ⇒ Object

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



487
488
489
490
491
492
493
494
# File 'app/serializers/boat_serializer.rb', line 487

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



292
293
294
# File 'app/serializers/boat_serializer.rb', line 292

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

.boat_length(boat) ⇒ Object



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

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

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



256
257
258
# File 'app/serializers/boat_serializer.rb', line 256

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

.boat_length_metrics_meters(boat) ⇒ Object

TODO
bcff00da

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



251
252
253
# File 'app/serializers/boat_serializer.rb', line 251

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

.boat_model(boat) ⇒ Object



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

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

.boat_photos(boat) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
# File 'app/serializers/boat_serializer.rb', line 473

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



224
225
226
227
228
229
230
231
# File 'app/serializers/boat_serializer.rb', line 224

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

.boat_type_id(boat) ⇒ Object



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

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

.builder(boat) ⇒ Object



112
113
114
# File 'app/serializers/boat_serializer.rb', line 112

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

.built_year(boat) ⇒ Object



195
196
197
# File 'app/serializers/boat_serializer.rb', line 195

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

.crew_total(boat) ⇒ Object



191
192
193
# File 'app/serializers/boat_serializer.rb', line 191

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

.default_optsObject



51
52
53
54
55
56
57
58
59
# File 'app/serializers/boat_serializer.rb', line 51

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

.exterior_color(boat) ⇒ Object



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

def exterior_color(boat)
  color = ::Dicts::ExteriorColor.find boat.exterior_color
  {
      exterior_color: {
          id:     (color.id    rescue -1),
          index:  (color.index rescue 'null')
      }
  }
end

.guest_cabins(boat) ⇒ Object



167
168
169
# File 'app/serializers/boat_serializer.rb', line 167

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



163
164
165
# File 'app/serializers/boat_serializer.rb', line 163

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

.hull_material(boat) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'app/serializers/boat_serializer.rb', line 171

def hull_material(boat)
  hm = ::Dicts::HullMaterial.find boat.hull_material
  {
      hull_material: {
          id:     (hm.id rescue -1),
          index:  (hm.index rescue 'null')
      }
  }
end

.id(boat) ⇒ Object



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

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

.interior_designer(boat) ⇒ Object



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

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

.latitude(boat) ⇒ Object



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

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

.location_address(boat) ⇒ Object



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

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

.longitude(boat) ⇒ Object



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

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

.main_boat_photo_id(boat) ⇒ Object



104
105
106
107
108
109
110
# File 'app/serializers/boat_serializer.rb', line 104

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



94
95
96
97
98
99
100
101
102
# File 'app/serializers/boat_serializer.rb', line 94

def motor(boat)
  m = ::Dicts::Motor.find boat.motor
  {
      motor: {
          id:     (m.id    rescue -1),
          index:  (m.index rescue 'null')
      }
  }
end

.name(boat) ⇒ Object



90
91
92
# File 'app/serializers/boat_serializer.rb', line 90

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


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

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

.picture_medium_url(boat) ⇒ Object

noinspection RubyResolve



80
81
82
83
84
85
86
87
88
# File 'app/serializers/boat_serializer.rb', line 80

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



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'app/serializers/boat_serializer.rb', line 296

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



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

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

.rental_type(boat) ⇒ Object



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

def rental_type(boat)
  hm = ::Dicts::RentSkipType.find boat.rental_type
  {
      rental_type: {
          id:     (hm.id rescue -1),
          index:  (hm.index rescue 'null')
      }
  }
end

.serialize(model, attributes: available_attributes, opts: {}) ⇒ Object



61
62
63
64
65
# File 'app/serializers/boat_serializer.rb', line 61

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



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'app/serializers/boat_serializer.rb', line 504

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



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

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



469
470
471
# File 'app/serializers/boat_serializer.rb', line 469

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

.visits_today(boat) ⇒ Object



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

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

.visits_total(boat) ⇒ Object



500
501
502
# File 'app/serializers/boat_serializer.rb', line 500

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