Sha256: e052c7fc41936bdaed3737f3ce8af751c8add336654f854e02c6bf4dc502e261

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

class RegistrationForm < ApplicationForm
  attribute :first_name, String
  attribute :last_name, String
  attribute :email, String
  attribute :company_name, String
  attribute :country_code, String
  attribute :phone_code, String
  attribute :phone_area, String
  attribute :phone_number, String
  attribute :password, String
  attribute :time_zone, String

  # validations
  validates :time_zone, length: { maximum: 100 }, allow_blank: true
  validates :country_code, presence: true, length: { maximum: 3 }
  validate :valid_country_code?

  validates :first_name, presence: true, length: { maximum: 130 }
  validates :last_name, presence: true, length: { maximum: 130 }

  validates :email, presence: true, length: { maximum: 130 }, email: true
  validates :phone_code, presence: true, numericality: true, length: { minimum: 1, maximum: 10 }
  validates :phone_area, numericality: true, length: { maximum: 6 }, allow_blank: true
  validates :phone_number, presence: true, numericality: true, length: { minimum: 6, maximum: 12 }
  validates :password, presence: true, length: { minimum: 6, maximum: 128 }, confirmation: true

  validates :company_name, length: { maximum: 130 }, allow_blank: true

  def submit(is_translator = false)
    api_answer = if is_translator
                   TranslationCms::Api::Translator.create(attributes)
                 else
                   TranslationCms::Api::Customer.create(attributes)
                 end

    merge_responce! api_answer
    errors.empty?
  end

  def session
    @session ||= TranslationCms::Api::Customer.authenticate!(email: email, password: password)
    @session = nil unless @session.nil? || @session.errors.blank?
    @session
  end

  protected

  def valid_country_code?
    errors.add(:country_code, :invalid) if ISO3166::Country.find_country_by_alpha3(country_code).nil?
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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