Class: Concept::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Validations, 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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FirstLevelObjectValidations

#distinct_versions

Methods included from Validations

#exclusive_top_term, #pref_label_in_primary_thesaurus_language, #rooted_top_terms, #unique_pref_label, #unique_pref_label_language, #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?, #publish, #publish!, #publishable?, #published?, #state, #to_review, #unlock, #unpublish, #validatable_for_publishing?, #with_validations_for_publishing

Class Method Details

.broader_topsObject



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

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


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

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

.for_dashboardObject



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

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

.inline_partial_nameObject

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



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

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


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

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

.topsObject

********** Scopes



247
248
249
# File 'app/models/concept/base.rb', line 247

def self.tops
  where(top_term: true)
end

.with_associationsObject



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

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

.with_pref_labelsObject



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

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



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

def additional_info
  nil
end

#associated_objects_in_editing_modeObject



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

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



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

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



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

def concept_relations_by_id=(hash)
  @concept_relations_by_id = hash
end

#concept_relations_by_id_and_rank(relation_name) ⇒ Object



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

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

#labelings_by_text(relation_name, language) ⇒ Object



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

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

#labelings_by_text=(hash) ⇒ Object

********** Methods



290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'app/models/concept/base.rb', line 290

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



348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'app/models/concept/base.rb', line 348

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 < 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
  ((@labels && @labels[labeling_class] && @labels[labeling_class][lang]) || []).select{ |l| l.published? || !only_published }
end

#matches_for_class(match_class) ⇒ Object



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

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

#notations_for_class(notation_class) ⇒ Object



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

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

#notes_for_class(note_class) ⇒ Object



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

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



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'app/models/concept/base.rb', line 331

def pref_label
  lang = I18n.locale.to_s == 'none' ? nil : 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] = 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


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

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

#to_paramObject



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

def to_param
  "#{origin}"
end

#to_sObject



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

def to_s
  pref_label.to_s
end