Class: Boats::BoatPricesSaveService

Inherits:
AbstractPricesService show all
Defined in:
app/services/boats/boat_prices_save_service.rb

Overview

spec/services/boat/boat_prices_save_service_spec.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractPricesService

#built_prices

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractPricesService

#initialize

Constructor Details

This class inherits a constructor from AbstractPricesService

Instance Attribute Details

#is_for_rentObject (readonly)

Returns the value of attribute is_for_rent.



7
8
9
# File 'app/services/boats/boat_prices_save_service.rb', line 7

def is_for_rent
  @is_for_rent
end

Class Method Details

.perform(boat, params) ⇒ Object



9
10
11
# File 'app/services/boats/boat_prices_save_service.rb', line 9

def self.perform(boat, params)
  new.perform(boat, params)
end

Instance Method Details

#perform(boat, prices = nil) ⇒ Object



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
# File 'app/services/boats/boat_prices_save_service.rb', line 13

def perform(boat, prices = nil)
  _reset_ivars
  return false if prices.nil?

  boat_prices_params = prices.deep_dup                                                                              # Rails deep_dup https://apidock.com/rails/Hash/deep_dup
  boat_prices_params = boat_prices_params.map { |bp| bp.symbolize_keys }
  filtered           = _delete_bad_values boat_prices_params
  return false if filtered.size.zero?                                                                               # взаимодействия с базой не будет, если все цены с "плохими" значениями

  filtered.each do |bp|                                                                                             # bp = {uom_id, season_id, currency_id, duration, value, discount}
    bp[:boat_id]    = boat.id
    bp[:is_orig]    = true
    bp[:value]      = bp[:value].is_a?(String) ? bp[:value].split(/[, ]/).join : bp[:value]                         # с формы может прийти строка, сгруппированная по разрадям, вида "100,100,222"
    bp[:duration]   = format('%.1f', bp[:duration])
    bp[:discount]   = bp[:discount].to_f                                                                            # с формы может прийти пустая строка ''
    bp[:created_at] = '\'%s\'' % Time.now.to_s(:db)
    _build_other bp
  end

  filtered2 = _del_duplicates(filtered + @built_prices)
  return false if filtered2.size.zero?

  saved = nil
  ActiveRecord::Base.transaction do
    _delete_existing_records boat.id
    saved = _save filtered2
  end

  @is_for_rent = _detect_for_rent saved
  true
end