Class: RentForm

Inherits:
ApplicationForm show all
Includes:
DatesNowValidator
Defined in:
app/forms/rent_form.rb

Constant Summary collapse

FIELDS =
%i[
  address
  latitude
  longitude
  location_address
  name
  from
  to
  guests
  kids
  boat_types
  is_skippered
  is_my_price
  my_price_currency
  my_price
  watersports
  need_transfer
  comments
  email
  name
  phone_number
  password
  sms_code
  staying_overnight
  uploaded_license_file
  hours_per_day
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DatesNowValidator

#check_dates

Methods inherited from ApplicationForm

all_fields, fields, #persisted?, #to_h

Constructor Details

#initialize(params = nil, is_need_bareboat_license = true) ⇒ RentForm



61
62
63
64
65
66
# File 'app/forms/rent_form.rb', line 61

def initialize(params = nil, is_need_bareboat_license = true)
  self.send(:is_my_price=, !params[:my_price].to_i.zero?) unless params.nil?
  @is_need_bareboat_license = is_need_bareboat_license

  super params
end

Instance Attribute Details

#is_need_bareboat_licenseObject (readonly)

Returns the value of attribute is_need_bareboat_license.



59
60
61
# File 'app/forms/rent_form.rb', line 59

def is_need_bareboat_license
  @is_need_bareboat_license
end

Instance Method Details

#button_nameObject



77
78
79
# File 'app/forms/rent_form.rb', line 77

def button_name
  I18n.t(:new_rent_order)
end

#validate_addressObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/forms/rent_form.rb', line 95

def validate_address
  conditions = [
    address.empty?,
    latitude.to_f.zero?,
    longitude.to_f.zero?,
    latitude.to_i < -90,
    latitude.to_i > 90,
    longitude.to_i < -180,
    longitude.to_i > 180,
  ]

  if conditions.any?
    errors.add(:address, :invalid_address)
  end
end

#validate_boat_typesObject



85
86
87
88
89
90
91
92
93
# File 'app/forms/rent_form.rb', line 85

def validate_boat_types
  if boat_types.nil? || boat_types.empty?
    errors.add(:boat_types, :absent)
  else
    unless BoatType.where(id: boat_types.map(&:to_i)).exists?
      errors.add(:boat_types, :absent)
    end
  end
end

#validate_datesObject



81
82
83
# File 'app/forms/rent_form.rb', line 81

def validate_dates
  check_dates errors, from, to
end

#validate_licenseObject



68
69
70
71
72
73
74
75
# File 'app/forms/rent_form.rb', line 68

def validate_license
  return unless @is_need_bareboat_license
  return unless is_skippered.eql?('false')

  unless uploaded_license_file.present?
    errors.add(:uploaded_license_file, :presence)
  end
end