Class: Schemas::CentralAgent::SaveBoatSchema

Inherits:
BaseBoatSchema show all
Defined in:
app/getboat/schemas/central_agent/save_boat_schema.rb

Instance Attribute Summary

Attributes included from Dry::Schema

#attributes, #errors

Instance Method Summary collapse

Methods inherited from BaseBoatSchema

#rule_boat_photos_attributes, #rule_boat_type_id, #rule_maximum_guests_during_cruise, #rule_name, #validate_crew_total, #validate_guest_cabins, #validate_guests_total, #validate_location, #validate_prices_and_rent_prices, #validate_rent_prices

Methods inherited from Base

#clear_html, #forced_array, #forced_hash, #normalize_integer, #normalize_url, #nullify_empty, #strip_string, #to_big_decimal, #to_bool, #to_date, #to_float

Methods included from Dry::Schema

#[], #initialize, #valid?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dry::Schema

Instance Method Details

#schemaObject



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
# File 'app/getboat/schemas/central_agent/save_boat_schema.rb', line 4

def schema
  boat_schema = super
  boat_schema[:required] = %i[
    name
    boat_type_id
  ]

  boat_schema[:properties][:boat_photos_attributes] = {
      type: [:null, :hash],
      cast: ->(value) { nullify_empty(value) }
  }

  # второй вариант реализации property boat_length, который присутствует в базовой схеме
  %i[boat_length_metrics boat_beam_metrics boat_draft_metrics].product([:meters, :ft]).map { |f,d| '%s_%s' % [f,d] }.each do |prop|
    boat_schema[:properties][prop.to_sym] = {
        type: %i[null string number],
        cast: ->(value) do
          val = value.is_a?(String) ? value.split(/[, ]/).join : value
          to_float val
        end
    }
  end

  boat_schema
end

#validate_additionalObject



31
32
33
34
35
36
37
38
39
40
# File 'app/getboat/schemas/central_agent/save_boat_schema.rb', line 31

def validate_additional
  validate_boat_photos
  validate_prices_and_rent_prices
  validate_rent_prices
  validate_length
  validate_location
  validate_guests_total
  # validate_guest_cabins
  validate_crew_total
end

#validate_boat_photosObject



42
43
44
# File 'app/getboat/schemas/central_agent/save_boat_schema.rb', line 42

def validate_boat_photos
  errors.add :boat_photos, I18n.t('errors.boat.boat_photos_attributes.blank') unless boat_photos_attributes.present?
end

#validate_lengthObject



47
48
49
50
51
52
# File 'app/getboat/schemas/central_agent/save_boat_schema.rb', line 47

def validate_length
  val = boat_length_metrics_meters || boat_length_metrics_ft
  unless val.present? && val > 0 && val < 1000
    errors.add(:boat_length_metrics, I18n.t('errors.messages.invalid'))
  end
end