Sha256: 1c42cdfde90f08a29f94cace28b86196f7843ad4c4a52886c2b45e8538997955

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

# encoding: utf-8
module Inflectionable
  extend ActiveSupport::Concern

  included do
    attr_reader :inflectionals_attributes
    after_save :overwrite_inflectionals!
    after_save :create_default_inflectional

    has_many :inflectionals,
             class_name: "Inflectional::Base",
             foreign_key: "label_id",
             dependent: :destroy,
             inverse_of: :label

    # overwrite class method to query labels by all existing inflectionals
    # instead of basic xl-label value attribute
    def self.by_query_value(query)
      Iqvoc::XLLabel.base_class
                    .references(:inflectionals)
                    .joins(:inflectionals)
                    .where('inflectionals.value ILIKE ?', query.mb_chars.downcase.to_s)
    end
  end

  def endings
    Inflectional::Base.for_language_and_code(language, inflectional_code)
  end

  def candidates
    Inflectional::Base.candidates_for(base_form, language, inflectional_code)
  end

  def generate_inflectionals!
    return send(Inflectional::Base.name.to_relation_name) if base_form.blank?

    candidates.each do |candidate|
      if candidate && inflectionals.where(value: candidate).none?
        send(Inflectional::Base.name.to_relation_name).create!(value: candidate)
      end
    end

    # self.base_form = new_base_form
    save(validate: false)

    inflectionals
  end

  def inflectionals_attributes=(str)
    @inflectionals_attributes = str.split("\r\n").uniq
  end

  def overwrite_inflectionals!
    return unless inflectionals_attributes
    transaction do
      inflectionals.delete_all
      inflectionals_attributes.each do |value|
        inflectionals.create!(value: value)
      end
    end
  end

  def create_default_inflectional
    # ensure a matching inflectional exists
    if value && inflectionals.empty?
      inflectionals.create(:value => value)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iqvoc_inflectionals-2.11.4 app/models/concerns/inflectionable.rb
iqvoc_inflectionals-2.11.3 app/models/concerns/inflectionable.rb