Sha256: 6f0a8cd38446d6f29df72fc362049da40519f1cdbe72dc353ed5f2f70632c4b7

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

class FreeQuoteForm < ApplicationForm
  extend EnumField::EnumeratedAttribute

  attribute :customer_name, String
  attribute :customer_email, String
  attribute :customer_country_code, String
  attribute :customer_phone_code, String
  attribute :customer_phone_number, String
  attribute :description, String
  attribute :fileupload_token, String
  attribute :utm_source, String
  attribute :utm_medium, String
  attribute :utm_campaign, String

  attribute :customer_mode_id, Integer
  enumerated_attribute :customer_mode

  validates :customer_mode_id,
            presence: true,
            inclusion: { in: CustomerMode.all.map(&:id) }

  validates :description,
            presence: true, length: { maximum: 1000 }
  validates :customer_name,
            presence: true, length: { maximum: 130 },
            unless: 'customer_mode.signed?'
  validates :customer_email,
            presence: true, email: true, length: { maximum: 130 },
            unless: 'customer_mode.signed?'

  validates :customer_country_code, presence: true, length: { maximum: 3 }
  validate :valid_country_code?
  validates :customer_phone_code,
            presence: true, length: { minimum: 1, maximum: 6 },
            unless: 'customer_mode.signed?'
  validates :customer_phone_number,
            presence: true, length: { minimum: 6, maximum: 14 },
            unless: 'customer_mode.signed?'

  def submit
    if valid?
      return false if customer_mode.nil?

      api_answer = TranslationCms::Api::FreeQuote.create(params)
      merge_responce! api_answer
    end

    errors.empty?
  end

  protected

  def valid_country_code?
    return true if customer_country_code.blank?
    return true unless ISO3166::Country.find_country_by_alpha3(customer_country_code).nil?

    errors.add(:customer_country_code, :invalid)
  end

  def params
    attributes
  end

  def read_attribute(attr)
    self[attr]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translation_cms-0.1.5 app/forms/free_quote_form.rb