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



265
266
267
# File 'app/models/concept/base.rb', line 265

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

.inline_partial_nameObject

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



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

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


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

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



349
350
351
# File 'app/models/concept/base.rb', line 349

def additional_info
  nil
end

#associated_objects_in_editing_modeObject



379
380
381
382
383
# File 'app/models/concept/base.rb', line 379

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



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

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



288
289
290
# File 'app/models/concept/base.rb', line 288

def concept_relations_by_id=(hash)
  @concept_relations_by_id = hash
end

#ensure_a_pref_label_in_the_primary_thesaurus_languageObject



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

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_maximum_two_versions_of_a_conceptObject

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



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

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



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'app/models/concept/base.rb', line 404

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

#invalid_with_full_validation?Boolean

TODO: remove

Returns:

  • (Boolean)


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

def invalid_with_full_validation?
  @full_validation = true
  invalid?
end

#labelings_by_text(relation_name, language) ⇒ Object



282
283
284
285
286
# File 'app/models/concept/base.rb', line 282

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



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

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



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

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



337
338
339
340
# File 'app/models/concept/base.rb', line 337

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



342
343
344
345
# File 'app/models/concept/base.rb', line 342

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



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

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


331
332
333
334
335
# File 'app/models/concept/base.rb', line 331

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



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

def save_with_full_validation!
  @full_validation = true
  save!
end

#to_paramObject



353
354
355
# File 'app/models/concept/base.rb', line 353

def to_param
  "#{origin}"
end

#to_sObject



357
358
359
# File 'app/models/concept/base.rb', line 357

def to_s
  pref_label.to_s
end

#valid_with_full_validation?Boolean

TODO: rename to “publishable?”

Returns:

  • (Boolean)


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

def valid_with_full_validation?
  @full_validation = true
  valid?
end