Class: Concept::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Iqvoc::Versioning
Defined in:
app/models/concept/base.rb

Overview

Copyright 2011 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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Iqvoc::Versioning

#branch, #editor_selectable?, #in_review?, #lock_by_user, #locked?, #publish, #published?, #state, #to_review, #unlock, #unpublish

Class Method Details

.broader_topsObject



261
262
263
264
265
266
# File 'app/models/concept/base.rb', line 261

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


294
295
296
# File 'app/models/concept/base.rb', line 294

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

.for_dashboardObject



280
281
282
# File 'app/models/concept/base.rb', line 280

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

.inline_partial_nameObject

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



286
287
288
# File 'app/models/concept/base.rb', line 286

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


290
291
292
# File 'app/models/concept/base.rb', line 290

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

.topsObject

********** Scopes



257
258
259
# File 'app/models/concept/base.rb', line 257

def self.tops
  where(:top_term => true)
end

.with_associationsObject



268
269
270
271
272
# File 'app/models/concept/base.rb', line 268

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

.with_pref_labelsObject



274
275
276
277
278
# File 'app/models/concept/base.rb', line 274

def self.with_pref_labels
  includes(:pref_labels).
  order("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



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

def additional_info
  nil
end

#associated_objects_in_editing_modeObject



413
414
415
416
417
# File 'app/models/concept/base.rb', line 413

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

#concept_relations_by_id(relation_name) ⇒ Object



321
322
323
324
325
# File 'app/models/concept/base.rb', line 321

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(Iqvoc::InlineDataHelper::Joiner)
end

#concept_relations_by_id=(hash) ⇒ Object



317
318
319
# File 'app/models/concept/base.rb', line 317

def concept_relations_by_id=(hash)
  @concept_relations_by_id = hash
end

#concept_relations_by_id_and_rank(relation_name) ⇒ Object



327
328
329
330
# File 'app/models/concept/base.rb', line 327

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

#ensure_a_pref_label_in_the_primary_thesaurus_languageObject



458
459
460
461
462
463
464
465
466
467
# File 'app/models/concept/base.rb', line 458

def ensure_a_pref_label_in_the_primary_thesaurus_language
  if @full_validation
    labels = pref_labels.select{|l| l.published?}
    if labels.count == 0
      errors.add :base, I18n.t("txt.models.concept.no_pref_label_error")
    elsif not labels.map(&:language).map(&:to_s).include?(Iqvoc::Concept.pref_labeling_languages.first.to_s)
      errors.add :base, I18n.t("txt.models.concept.main_pref_label_language_missing_error")
    end
  end
end

#ensure_distinct_versionsObject

validates that a) no more than one concept with the same origin already exists b) if a concept with the same origin exists, its publication state differs

from the to-be-saved one's


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

def ensure_distinct_versions
  query = Concept::Base.by_origin(origin)
  existing_total = query.count
  if existing_total >= 2
    errors.add :base, I18n.t("txt.models.concept.version_error")
  elsif existing_total == 1
    unless (query.published.count == 0 and published?) or
           (query.published.count == 1 and not published?)
      errors.add :base, I18n.t("txt.models.concept.version_error")
    end
  end
end

#ensure_exclusive_top_termObject

top term and broader relations are mutually exclusive



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

def ensure_exclusive_top_term
  if @full_validation
    if top_term && broader_relations.any?
      errors.add :base, I18n.t("txt.models.concept.top_term_exclusive_error")
    end
  end
end

#ensure_no_pref_labels_share_the_same_languageObject



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'app/models/concept/base.rb', line 469

def ensure_no_pref_labels_share_the_same_language
  # We have many sources a prefLabel can be defined in
  pls = pref_labelings.map(&:target) +
    send(Iqvoc::Concept.pref_labeling_class_name.to_relation_name).map(&:target) +
    labelings.select{|l| l.is_a?(Iqvoc::Concept.pref_labeling_class)}.map(&:target)
  languages = {}
  pls.compact.each do |pref_label|
    lang = pref_label.language.to_s
    origin = (pref_label.origin || pref_label.id || pref_label.value).to_s
    if (languages.keys.include?(lang) && languages[lang] != origin)
      errors.add :pref_labelings, I18n.t("txt.models.concept.pref_labels_with_same_languages_error")
    end
    languages[lang] = origin
  end
end

#ensure_rooted_top_termsObject

top terms must never be used as descendants (narrower relation targets) NB: for top terms themselves, this is covered by ‘ensure_exclusive_top_term`



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

def ensure_rooted_top_terms
  if @full_validation
    if narrower_relations.includes(:target). # XXX: inefficient?
        select { |rel| rel.target && rel.target.top_term? }.any?
      errors.add :base, I18n.t("txt.models.concept.top_term_rooted_error")
    end
  end
end

#ensure_valid_rank_for_ranked_relationsObject



485
486
487
488
489
490
491
492
493
494
495
# File 'app/models/concept/base.rb', line 485

def ensure_valid_rank_for_ranked_relations
  if @full_validation
    relations.each do |relation|
      if relation.class.rankable? && !(0..100).include?(relation.rank)
        errors.add :base, I18n.t("txt.models.concept.invalid_rank_for_ranked_relations",
          :relation => relation.class.model_name.human.downcase,
          :relation_target_label => relation.target.pref_label.to_s)
      end
    end
  end
end

#invalid_with_full_validation?Boolean

TODO: remove

Returns:

  • (Boolean)


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

def invalid_with_full_validation?
  @full_validation = true
  invalid?
end

#labelings_by_text(relation_name, language) ⇒ Object



311
312
313
314
315
# File 'app/models/concept/base.rb', line 311

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

#labelings_by_text=(hash) ⇒ Object

********** Methods



300
301
302
303
304
305
306
307
308
309
# File 'app/models/concept/base.rb', line 300

def labelings_by_text=(hash)
  @labelings_by_text = hash

   # For language = nil: <input name=bla[labeling_class][]> => Results in an Array!
  @labelings_by_text.each do |relation_name, array_or_hash|
    @labelings_by_text[relation_name] = {nil => array_or_hash.first} if array_or_hash.is_a?(Array)
  end

  @labelings_by_text
end

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



354
355
356
357
358
359
360
361
362
363
# File 'app/models/concept/base.rb', line 354

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.
  lang = lang.to_s unless lang.nil?
  labeling_class = labeling_class.name if labeling_class < ActiveRecord::Base # 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
  return ((@labels && @labels[labeling_class] && @labels[labeling_class][lang]) || []).select{|l| l.published? || !only_published}
end

#matches_for_class(match_class) ⇒ Object



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

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

#notes_for_class(note_class) ⇒ Object



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

def notes_for_class(note_class)
  note_class = note_class.name if note_class < ActiveRecord::Base # 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!)



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'app/models/concept/base.rb', line 337

def pref_label
  lang = I18n.locale.to_s
  @cached_pref_labels ||= pref_labels.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.to_s] = label
  end
  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


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

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

#save_with_full_validation!Object

TODO: rename to “publish!”



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

def save_with_full_validation!
  @full_validation = true
  save!
end

#to_paramObject



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

def to_param
  "#{origin}"
end

#to_sObject



391
392
393
# File 'app/models/concept/base.rb', line 391

def to_s
  pref_label.to_s
end

#valid_with_full_validation?Boolean

TODO: rename to “publishable?”

Returns:

  • (Boolean)


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

def valid_with_full_validation?
  @full_validation = true
  valid?
end