Class: Concept::Base

Inherits:
ApplicationRecord show all
Includes:
Validations, FirstLevelObjectScopes, FirstLevelObjectValidations, Versioning
Defined in:
app/models/concept/base.rb

Overview

Copyright 2011-2013 innoQ Deutschland GmbH

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Direct Known Subclasses

Collection::Base, SKOS::Base, SKOS::Scheme

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FirstLevelObjectValidations

#distinct_versions, #origin_has_to_be_escaped

Methods included from Validations

#exclusive_broader_and_narrower_concepts, #exclusive_pref_and_alt_labels_per_concept, #exclusive_top_term, #no_self_reference_concept_relation, #pref_label_in_primary_thesaurus_language, #rooted_top_terms, #unique_alt_labels, #unique_pref_label_language, #unique_pref_labels, #valid_rank_for_ranked_relations

Methods included from Versioning

#branch, #disable_validations_for_publishing, #editor_selectable?, #enable_validations_for_publishing, #in_review?, #lock_by_user, #locked?, #never_published?, #publish, #publish!, #publishable?, #published?, #state, #to_review, #unlock, #unpublish, #unpublished?, #validatable_for_publishing?, #with_validations_for_publishing

Instance Attribute Details

#reverse_match_serviceObject

Returns the value of attribute reverse_match_service.



18
19
20
# File 'app/models/concept/base.rb', line 18

def reverse_match_service
  @reverse_match_service
end

Class Method Details

.broader_topsObject



374
375
376
377
378
379
# File 'app/models/concept/base.rb', line 374

def self.broader_tops
  includes(:narrower_relations, :pref_labels).
  where(concept_relations: { id: nil },
    labelings: { type: Iqvoc::Concept.pref_labeling_class_name }).
  order(Arel.sql("LOWER(#{Label::Base.table_name}.value)"))
end

.dashboard_pathObject



416
417
418
# File 'app/models/concept/base.rb', line 416

def self.dashboard_path
  'dashboard_path'
end


412
413
414
# File 'app/models/concept/base.rb', line 412

def self.edit_link_partial_name
  'partials/concept/edit_link_base'
end

.expired(time = Time.now) ⇒ Object

********** Scopes



361
362
363
# File 'app/models/concept/base.rb', line 361

def self.expired(time = Time.now)
  where(arel_table[:expired_at].lt(time))
end

.for_dashboardObject



393
394
395
# File 'app/models/concept/base.rb', line 393

def self.for_dashboard
  unpublished_or_follow_up.includes(:pref_labels, :locking_user)
end

.inline_partial_nameObject

********** Class methods



404
405
406
# File 'app/models/concept/base.rb', line 404

def self.inline_partial_name
  'partials/concept/inline_base'
end


408
409
410
# File 'app/models/concept/base.rb', line 408

def self.new_link_partial_name
  'partials/concept/new_link_base'
end

.not_expired(time = Time.now) ⇒ Object



365
366
367
368
# File 'app/models/concept/base.rb', line 365

def self.not_expired(time = Time.now)
  col = arel_table[:expired_at]
  where((col.eq(nil)).or(col.gteq(time)))
end

.parentlessObject



397
398
399
400
401
# File 'app/models/concept/base.rb', line 397

def self.parentless
  includes(:broader_relations)
    .where(concept_relations: {owner_id: nil}, top_term: false)
    .references(:concept_relations)
end

.topsObject



370
371
372
# File 'app/models/concept/base.rb', line 370

def self.tops
  where(top_term: true)
end

.with_associationsObject



381
382
383
384
385
# File 'app/models/concept/base.rb', line 381

def self.with_associations
  includes([
    { labelings: :target }, :relations, :matches, :notes, :notations
  ])
end

.with_pref_labelsObject



387
388
389
390
391
# File 'app/models/concept/base.rb', line 387

def self.with_pref_labels
  includes(:pref_labels).
  order(Arel.sql("LOWER(#{Label::Base.table_name}.value)")).
  where(labelings: { type: Iqvoc::Concept.pref_labeling_class_name }) # This line is just a workaround for a Rails Bug. TODO: Delete it when the Bug is fixed
end

Instance Method Details

#additional_infoObject

This shows up (in brackets) to the right of a concept link if it doesn’t return nil



544
545
546
# File 'app/models/concept/base.rb', line 544

def additional_info
  nil
end

#assigned_collection_originsObject



467
468
469
# File 'app/models/concept/base.rb', line 467

def assigned_collection_origins
  @assigned_collection_origins || collections.map(&:origin).uniq
end

#assigned_collection_origins=(origins) ⇒ Object



462
463
464
465
# File 'app/models/concept/base.rb', line 462

def assigned_collection_origins=(origins)
  @assigned_collection_origins= origins.to_s
    .split(InlineDataHelper::SPLITTER).map(&:strip)
end

#associated_objects_in_editing_modeObject



556
557
558
559
560
# File 'app/models/concept/base.rb', line 556

def associated_objects_in_editing_mode
  {
    concept_relations: Concept::Relation::Base.by_owner(id).target_in_edit_mode,
  }
end

#class_pathObject

********** Methods



422
423
424
# File 'app/models/concept/base.rb', line 422

def class_path
  'concept_path'
end

#concept_relations_by_id(relation_name) ⇒ Object



451
452
453
454
455
# File 'app/models/concept/base.rb', line 451

def concept_relations_by_id(relation_name)
  (@concept_relations_by_id && @concept_relations_by_id[relation_name]) ||
    self.send(relation_name).map { |l| l.target.origin }.
    join(InlineDataHelper::JOINER)
end

#concept_relations_by_id=(hash) ⇒ Object



447
448
449
# File 'app/models/concept/base.rb', line 447

def concept_relations_by_id=(hash)
  @concept_relations_by_id = hash
end

#concept_relations_by_id_and_rank(relation_name) ⇒ Object



457
458
459
460
# File 'app/models/concept/base.rb', line 457

def concept_relations_by_id_and_rank(relation_name)
  self.send(relation_name).each_with_object({}) { |rel, hsh| hsh[rel.target] = rel.rank }
  # self.send(relation_name).map { |l| "#{l.target.origin}:#{l.rank}" }
end

#expired?Boolean

Returns:

  • (Boolean)


567
568
569
# File 'app/models/concept/base.rb', line 567

def expired?
  self.expired_at && self.expired_at < Date.today
end

#jobsObject



562
563
564
565
# File 'app/models/concept/base.rb', line 562

def jobs
  gid = self.to_global_id.to_s
  Delayed::Backend::ActiveRecord::Job.where(delayed_global_reference_id: gid)
end

#labelings_by_text(relation_name, language) ⇒ Object



440
441
442
443
444
445
# File 'app/models/concept/base.rb', line 440

def labelings_by_text(relation_name, language)
  (@labelings_by_text && @labelings_by_text[relation_name] &&
      @labelings_by_text[relation_name][language]) ||
      InlineDataHelper.generate_inline_values(self.send(relation_name).
          by_label_language(language).map { |l| l.target.value })
end

#labelings_by_text=(hash) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'app/models/concept/base.rb', line 426

def labelings_by_text=(hash)
  @labelings_by_text = hash

  @labelings_by_text.each do |relation_name, labels_by_lang|
    # if `language` is `nil`, the respective HTML form field returns an array
    # instead of a hash (`<input name=bla[labeling_class][]>`)
    if labels_by_lang.is_a?(Array)
      @labelings_by_text[relation_name] = { nil => labels_by_lang.first }
    end
  end

  @labelings_by_text
end

#labels_for_labeling_class_and_language(labeling_class, lang = :en, only_published = true) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'app/models/concept/base.rb', line 506

def labels_for_labeling_class_and_language(labeling_class, lang = :en, only_published = true)
  # Convert lang to string in case it's not nil.
  # nil values play their own role for labels without a language.
  if lang == 'none'
    lang = nil
  elsif lang
    lang = lang.to_s
  end
  labeling_class = labeling_class.name if labeling_class < ApplicationRecord # Use the class name string
  @labels ||= labelings.each_with_object({}) do |labeling, hash|
    ((hash[labeling.class.name.to_s] ||= {})[labeling.target.language] ||= []) << labeling.target if labeling.target
  end
  ((@labels && @labels[labeling_class] && @labels[labeling_class][lang]) || []).select{ |l| l.published? || !only_published }
end

#matches_for_class(match_class) ⇒ Object



527
528
529
530
# File 'app/models/concept/base.rb', line 527

def matches_for_class(match_class)
  match_class = match_class.name if match_class < ApplicationRecord # Use the class name string
  matches.select{ |match| match.class.name == match_class }
end

#notations_for_class(notation_class) ⇒ Object



537
538
539
540
# File 'app/models/concept/base.rb', line 537

def notations_for_class(notation_class)
  notation_class = notation_class.name if notation_class < ApplicationRecord # Use the class name string
  notations.select{ |notation| notation.class.name == notation_class }
end

#notes_for_class(note_class) ⇒ Object



532
533
534
535
# File 'app/models/concept/base.rb', line 532

def notes_for_class(note_class)
  note_class = note_class.name if note_class < ApplicationRecord # Use the class name string
  notes.select{ |note| note.class.name == note_class }
end

#pref_labelObject

returns the (one!) preferred label of a concept for the requested language. lang can either be a (lowercase) string or symbol with the (ISO .…) two letter code of the language (e.g. :en for English, :fr for French, :de for German). If no prefLabel for the requested language exists, a new label will be returned (if you modify it, don’t forget to save it afterwards!)



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'app/models/concept/base.rb', line 476

def pref_label
  if pref_labels.loaded?
    # use select if association is already loaded
    @cached_pref_labels ||= pref_labels.select(&:published?).each_with_object({}) do |label, hash|
      if hash[label.language]
        Rails.logger.warn("Two pref_labels (#{hash[label.language]}, #{label}) for one language (#{label.language}). Taking the second one.")
      end
      hash[label.language] = label
    end
  else
    # use scope otherwise
    @cached_pref_labels ||= pref_labels.published.each_with_object({}) do |label, hash|
      if hash[label.language]
        Rails.logger.warn("Two pref_labels (#{hash[label.language]}, #{label}) for one language (#{label.language}). Taking the second one.")
      end
      hash[label.language] = label
    end
  end

  lang = I18n.locale.to_s == 'none' ? nil : I18n.locale.to_s

  if @cached_pref_labels[lang].nil?
    # Fallback to the main language
    @cached_pref_labels[lang] = pref_labels.select{ |l|
        l.language.to_s == Iqvoc::Concept.pref_labeling_languages.first.to_s
    }.first
  end
  @cached_pref_labels[lang]
end


521
522
523
524
525
# File 'app/models/concept/base.rb', line 521

def related_concepts_for_relation_class(relation_class, only_published = true)
  relation_class = relation_class.name if relation_class < ApplicationRecord # Use the class name string
  relations.select { |rel| rel.class.name == relation_class }.map(&:target).
      select { |c| c.published? || !only_published }.sort_by(&:pref_label)
end

#to_paramObject



548
549
550
# File 'app/models/concept/base.rb', line 548

def to_param
  "#{origin}"
end

#to_sObject



552
553
554
# File 'app/models/concept/base.rb', line 552

def to_s
  pref_label.to_s
end