Sha256: 1386595362970732b8d28910052660d7b3952363500c14e1e70fc5db48f8cd5f

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 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
  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.where(:value => value).none?
      inflectionals.create(:value => value)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iqvoc_inflectionals-2.9.0 app/models/concerns/inflectionable.rb
iqvoc_inflectionals-2.8.0 app/models/concerns/inflectionable.rb