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



267
268
269
# File 'app/models/concept/base.rb', line 267

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

.inline_partial_nameObject

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



259
260
261
# File 'app/models/concept/base.rb', line 259

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


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

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

Instance Method Details

#additional_infoObject

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



351
352
353
# File 'app/models/concept/base.rb', line 351

def additional_info
  nil
end

#associated_objects_in_editing_modeObject



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

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



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

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



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

def concept_relations_by_id=(hash)
  @concept_relations_by_id = hash
end

#ensure_a_pref_label_in_the_primary_thesaurus_languageObject



415
416
417
418
419
420
421
422
423
424
# File 'app/models/concept/base.rb', line 415

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_exclusive_top_termObject

top term and broader relations are mutually exclusive



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

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_maximum_two_versions_of_a_conceptObject

********** Validation methods



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

def ensure_maximum_two_versions_of_a_concept
  if Concept::Base.by_origin(origin).count >= 2
    errors.add :base, I18n.t("txt.models.concept.version_error")
  end
end

#ensure_no_pref_labels_share_the_same_languageObject



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

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.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`



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

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

#invalid_with_full_validation?Boolean

TODO: remove

Returns:

  • (Boolean)


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

def invalid_with_full_validation?
  @full_validation = true
  invalid?
end

#labelings_by_text(relation_name, language) ⇒ Object



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

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



273
274
275
276
277
278
279
280
281
282
# File 'app/models/concept/base.rb', line 273

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



322
323
324
325
326
327
328
329
330
331
# File 'app/models/concept/base.rb', line 322

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



339
340
341
342
# File 'app/models/concept/base.rb', line 339

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



344
345
346
347
# File 'app/models/concept/base.rb', line 344

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!)



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/models/concept/base.rb', line 305

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


333
334
335
336
337
# File 'app/models/concept/base.rb', line 333

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!”



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

def save_with_full_validation!
  @full_validation = true
  save!
end

#to_paramObject



355
356
357
# File 'app/models/concept/base.rb', line 355

def to_param
  "#{origin}"
end

#to_sObject



359
360
361
# File 'app/models/concept/base.rb', line 359

def to_s
  pref_label.to_s
end

#valid_with_full_validation?Boolean

TODO: rename to “publishable?”

Returns:

  • (Boolean)


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

def valid_with_full_validation?
  @full_validation = true
  valid?
end