Class: Lease::BidSerializer

Inherits:
AbstractSerializer show all
Extended by:
ActionView::Helpers::NumberHelper, Custom::CurrencyHelper, Site::TimeHelper
Defined in:
app/serializers/lease/bid_serializer.rb

Class Method Summary collapse

Methods included from Site::TimeHelper

format_date, locale_date_format, next_hour

Methods included from Custom::CurrencyHelper

currency_attribute_name, currency_for_label, currency_symbol, num_to_cur

Methods included from Custom::SelectHelper

#collection_for_select, #collection_name_by_id

Methods inherited from AbstractSerializer

opts, serialize

Class Method Details

.account_id(bid) ⇒ Object



35
36
37
# File 'app/serializers/lease/bid_serializer.rb', line 35

def (bid)
  { account_id: bid. }
end

.available_attributesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/serializers/lease/bid_serializer.rb', line 10

def available_attributes
  i[
    id
    
    inquiry_id
    boat_id
    boat
    price
    currency
    state
    from
    to
    period
    duration
    human_price
    human_price_vat_incl
    comments
    extra
  ].freeze
end

.boat(bid) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/serializers/lease/bid_serializer.rb', line 47

def boat(bid)
  boat            = bid.boat
  boat_serialized = BoatSerializer.serialize boat, attributes: i[
    name
    boat_length
    guests_total
    guest_cabins
    crew_total
    boat_stars
    builder
    built_year
  ]
  # backburner fails with "Exception ActiveJob::SerializationError -> Unsupported argument type: BigDecimal"
  boat_serialized[:boat_length][:value] = boat_serialized[:boat_length][:value].to_i
  {
    boat: {
      img_url:         (boat.boat_photo.picture.url(:thumb) if boat.boat_photo.present?),
      gallery_img_url: (boat.boat_photo.picture.url(:gallery) if boat.boat_photo.present?),
      type:            (boat.boat_type.name rescue nil),
      **boat_serialized
    }
  }
end

.boat_id(bid) ⇒ Object



43
44
45
# File 'app/serializers/lease/bid_serializer.rb', line 43

def boat_id(bid)
  { boat_id: bid.boat_id }
end

.comments(bid) ⇒ Object



125
126
127
# File 'app/serializers/lease/bid_serializer.rb', line 125

def comments(bid)
  { comments: bid.comments }
end

.currency(bid) ⇒ Object



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

def currency(bid)
  { currency: bid.currency }
end

.duration(bid) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'app/serializers/lease/bid_serializer.rb', line 101

def duration(bid)
  distance = (bid.to - bid.from).to_i / 24 / 60 / 60
  distance = distance.zero? ? 1 : distance
  uom      = I18n.t('activerecord.labels.lease/bid.day', count: distance)
  {
    duration: {
      distance: distance,
      uom:      uom
    }
  }
end

.extra(bid) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/serializers/lease/bid_serializer.rb', line 129

def extra(bid)
  extra = i[
    extra_captain
    extra_towels
    extra_hostess
    extra_cleaning
    extra_chef
    extra_fees
    extra_apa
  ].map do |attr|
    [attr, bid.send(attr)]
  end.to_h.select do |_, value|
    !value.nil?
  end.map do |a,v|
    if a == :extra_apa
      [a, '%s %' % v ]
    else
      [a, num_to_cur(v,bid.currency, @opts[:locale])]
    end
  end.to_h

  { extra: extra }
end

.from(bid) ⇒ Object



113
114
115
# File 'app/serializers/lease/bid_serializer.rb', line 113

def from(bid)
  { from: format_date(bid.from, @opts[:locale]) }
end

.human_price(bid) ⇒ Object



79
80
81
82
83
84
85
# File 'app/serializers/lease/bid_serializer.rb', line 79

def human_price(bid)
  val = nil
  if bid.price.present? && bid.currency.present?
    val = num_to_cur(bid.price, bid.currency, @opts[:locale])
  end
  { human_price: val }
end

.human_price_vat_incl(bid) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'app/serializers/lease/bid_serializer.rb', line 87

def human_price_vat_incl(bid)
  val = nil
  if bid.price.present? && bid.currency.present?
    val = bid.price / 100.0 * (100 + bid.vat)
    val = num_to_cur(val.to_i, bid.currency, @opts[:locale])
  end

  { human_price_vat_incl: val }
end

.id(bid) ⇒ Object



31
32
33
# File 'app/serializers/lease/bid_serializer.rb', line 31

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

.inquiry_id(bid) ⇒ Object



39
40
41
# File 'app/serializers/lease/bid_serializer.rb', line 39

def inquiry_id(bid)
  { inquiry_id: bid.inquiry_id }
end

.period(bid) ⇒ Object



97
98
99
# File 'app/serializers/lease/bid_serializer.rb', line 97

def period(bid)
  { period: '%s - %s' % [from(bid)[:from], to(bid)[:to]] }
end

.price(bid) ⇒ Object



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

def price(bid)
  { price: bid.price }
end

.state(bid) ⇒ Object



121
122
123
# File 'app/serializers/lease/bid_serializer.rb', line 121

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

.to(bid) ⇒ Object



117
118
119
# File 'app/serializers/lease/bid_serializer.rb', line 117

def to(bid)
  { to: format_date(bid.to, @opts[:locale]) }
end