# frozen_string_literal: true module FormData class Order < Base attribute :service_id, Integer attribute :certificate_id, Integer attribute :industry_expertise_id, Integer attribute :language_from_id, Integer attribute :languages_to_ids, Array[Integer] attribute :order_package_id, Integer attribute :urgency_rate_id, Integer attribute :preferred_writers_ids, Array[Integer] attribute :discount_code, String attribute :fileupload_token, String attribute :payment_method_id, Integer attribute :currency, String attribute :features_ids, Array[Integer] attribute :other_certificate, String attribute :description, String # recived from crm only attribute :waiting_payment_token, String attribute :number, String attribute :state, String attribute :locale, String attribute :utm_source, String attribute :utm_medium, String attribute :utm_campaign, String validates :service_id, presence: true validates :fileupload_token, presence: true validates :language_from_id, presence: true, numericality: true validates :service_id, presence: true, numericality: true validates :languages_to_ids, presence: true validate :valid_languages? def payment_method retutn if payment_method_id.blank? PaymentType.find_by id: payment_method_id end protected def valid_languages? if languages_to_ids.blank? errors.add(:languages_to_ids, :blank) return false elsif languages_to_ids.detect { |lid| lid == language_from_id } errors.add(:languages_to_ids, :invalid) return false end true end end end