app/models/itinerary.rb
# frozen_string_literal: true
class Itinerary < ApplicationRecord
include GeoItinerary
extend FriendlyId
DAYNAME = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].freeze
MAX_DESCRIPTION_LENGTH = 1000
friendly_id :name, use: :slugged
def name
[start_address, end_address].join ' '
end
has_many :conversations, as: :conversable, dependent: :destroy
belongs_to :user
delegate :name, to: :user, prefix: true
delegate :first_name, to: :user, prefix: true
validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }, presence: true
validates :num_people, numericality: { only_integer: true, greater_than: 0, less_than: 10 }, allow_blank: true
validates :fuel_cost, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than: 10_000 }
validates :tolls, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than: 10_000 }
validates :leave_date, comparison: { greater_than_or_equal_to: -> { Time.current } }, if: :will_save_change_to_leave_date?
validates :return_date, presence: true, comparison: { greater_than_or_equal_to: :leave_date }, if: :round_trip?
validate :driver_is_female, if: -> { pink }
def driver_is_female
errors.add(:pink, :driver_must_be_female) unless user.female?
end
before_create do
self.driver_gender = user.gender
true
end
def title
[start_address, end_address].join ' - '
end
def to_s
title || id
end
end