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

Constructor Details

#initialize(params = {}) ⇒ Base

********** Methods



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

def initialize(params = {})
  super(params)
  @full_validation = false
end

Class Method Details



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

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

.inline_partial_nameObject

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



253
254
255
# File 'app/models/concept/base.rb', line 253

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


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

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



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

def additional_info
  nil
end

#associated_objects_in_editing_modeObject



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

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



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

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



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

def concept_relations_by_id=(hash)
  @concept_relations_by_id = hash
end

#ensure_a_pref_label_in_the_primary_thesaurus_languageObject



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

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



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

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



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

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)


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

def invalid_with_full_validation?
  @full_validation = true
  invalid?
end

#labelings_by_text(relation_name, language) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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


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

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



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

def save_with_full_validation!
  @full_validation = true
  save!
end

#to_paramObject



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

def to_param
  "#{origin}"
end

#to_sObject



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

def to_s
  pref_label.to_s
end

#valid_with_full_validation?Boolean

TODO: rename to “publishable?”

Returns:

  • (Boolean)


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

def valid_with_full_validation?
  @full_validation = true
  valid?
end