Sha256: a776218715ec71a284b3e2931a38b680e883ce35f12de1266322ca70b814d3c2

Contents?: true

Size: 1.44 KB

Versions: 15

Compression:

Stored size: 1.44 KB

Contents

require 'mongoid'

class TaxonomyTerm
  include Mongoid::Document
  include Mongoid::Timestamps

  field :label, type: String
  field :term_id, type: String
  field :term_type, :type => Hash, :default => {}

  key :term_id
  has_and_belongs_to_many :child_terms, :class_name => 'TaxonomyTerm', :inverse_of => :parent_term
  belongs_to :parent_term, :class_name => 'TaxonomyTerm', :inverse_of => :child_terms

  #validations
  validates_presence_of :term_id, :label
  validates_uniqueness_of :term_id

  scope :term_id_is, ->(term_id) { where(:term_id => term_id) }
  scope :parent_term_in, ->(parent_term_id) { where(:parent_term.in => parent_term_id)}
  scope :label_is, ->(label) {where(:label => label)}
  scope :fund_code_is, ->(fund_code) { where(:'term_type.FUND_CODE' => fund_code.gsub(/^0*/, ""))}

  def self.purge!
    TaxonomyTerm.destroy_all
  end

  def self.label_for_term(term_id)
    where(term_id: term_id).try(:first).try(:label) if term_id
  end

  def fund_code
    term_type['FUND_CODE']
  end

  def ancestor?(term)
    if(parent_term)
      (parent_term == term) or parent_term.ancestor?(term)
    else
      false
    end
  end

  def descendant?(term)
    # accept either an ID string or a term
    term_id = term.is_a?(String) ? term : term.try(:term_id)
    # loop thru child terms and look for the id
    child_term_ids.index(term_id) or child_terms.detect {|c| c.descendant? term_id }
  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
daengine-0.4.0.4 app/models/taxonomy_term.rb
daengine-0.4.0.3 app/models/taxonomy_term.rb
daengine-0.4.0.2 app/models/taxonomy_term.rb
daengine-0.4.0.1 app/models/taxonomy_term.rb
daengine-0.4.0.0 app/models/taxonomy_term.rb
daengine-0.3.9.9 app/models/taxonomy_term.rb
daengine-0.3.9.8 app/models/taxonomy_term.rb
daengine-0.3.9.7 app/models/taxonomy_term.rb
daengine-0.3.9.6 app/models/taxonomy_term.rb
daengine-0.3.9.5 app/models/taxonomy_term.rb
daengine-0.3.9.4 app/models/taxonomy_term.rb
daengine-0.3.9.3 app/models/taxonomy_term.rb
daengine-0.3.9.2 app/models/taxonomy_term.rb
daengine-0.3.9.1 app/models/taxonomy_term.rb
daengine-0.3.9 app/models/taxonomy_term.rb