class Country < ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: :slugged

  belongs_to :region
  has_many :cities, dependent: :restrict_with_error

  validates :name, presence: true
  validates :code, presence: true, uniqueness: true

  def to_s
    name
  end

  def localized_name
    I18n.t("countries.#{code}")
  end

  def self.ransackable_attributes(auth_object = nil)
    [ "code", "created_at", "id", "id_value", "name", "region_id", "slug", "updated_at" ]
  end

  def self.ransackable_associations(auth_object = nil)
    [ "cities", "region" ]
  end
end
