language_skill.rb 1.17 KB
Newer Older
1
class LanguageSkill < ApplicationRecord
2
  belongs_to :languageable, polymorphic: true, optional: true
3

4 5
  validates :level, presence: true

6
  LANGUAGE_LEVELS = [:native_speaker, :basic, :fluent, :good].freeze
7

8
  scope :german_first, (-> { order("CASE WHEN language = 'DE' THEN 1 ELSE 2 END") })
9 10

  scope :german, (-> { where(language: 'DE') })
11 12
  scope :native, (-> { where(level: 'native_speaker') })
  scope :native_languages, (-> { native.german_first })
13 14 15 16
  scope :foreign_languages, lambda {
    return none unless native_language.id
    where.not(id: native_language.id)
  }
17

18
  def self.languages
19 20
    @languages ||= I18n.t('language_names').merge(I18n.t('language_names_customizations'))
      .map { |key, lang| [lang, key.to_s] }.sort
21 22 23
  end


24 25 26
  def self.native_language
    native_languages.first || LanguageSkill.new
  end
27

28
  def self.language_name(language)
29
    return '' if language.blank?
30
    I18n.t("language_names.#{language}")
31
  end
32

33 34 35 36
  def language_name
    self.class.language_name(language)
  end

37
  def full_language_skills
38 39
    level_human = level? ? I18n.t(level, scope: [:language_level]) : ''
    [language_name, level_human].reject(&:blank?).join(', ') if language?
40
  end
41
end