Class: Concept::Base
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Concept::Base
- 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
Class Method Summary collapse
- .edit_link_partial_name ⇒ Object
-
.inline_partial_name ⇒ Object
********** Class methods.
- .new_link_partial_name ⇒ Object
Instance Method Summary collapse
-
#additional_info ⇒ Object
This shows up (in brackets) to the right of a concept link if it doesn’t return nil.
- #associated_objects_in_editing_mode ⇒ Object
- #concept_relations_by_id(relation_name) ⇒ Object
- #concept_relations_by_id=(hash) ⇒ Object
- #ensure_a_pref_label_in_the_primary_thesaurus_language ⇒ Object
-
#ensure_exclusive_top_term ⇒ Object
top term and broader relations are mutually exclusive.
-
#ensure_maximum_two_versions_of_a_concept ⇒ Object
********** Validation methods.
- #ensure_no_pref_labels_share_the_same_language ⇒ Object
-
#ensure_rooted_top_terms ⇒ Object
top terms must never be used as descendants (narrower relation targets) NB: for top terms themselves, this is covered by ‘ensure_exclusive_top_term`.
-
#invalid_with_full_validation? ⇒ Boolean
TODO: remove.
- #labelings_by_text(relation_name, language) ⇒ Object
-
#labelings_by_text=(hash) ⇒ Object
********** Methods.
- #labels_for_labeling_class_and_language(labeling_class, lang = :en, only_published = true) ⇒ Object
- #matches_for_class(match_class) ⇒ Object
- #notes_for_class(note_class) ⇒ Object
-
#pref_label ⇒ Object
returns the (one!) preferred label of a concept for the requested language.
- #related_concepts_for_relation_class(relation_class, only_published = true) ⇒ Object
-
#save_with_full_validation! ⇒ Object
TODO: rename to “publish!”.
- #to_param ⇒ Object
- #to_s ⇒ Object
-
#valid_with_full_validation? ⇒ Boolean
TODO: rename to “publishable?”.
Methods included from Iqvoc::Versioning
#branch, #editor_selectable?, #in_review?, #lock_by_user, #locked?, #publish, #published?, #state, #to_review, #unlock, #unpublish
Class Method Details
.edit_link_partial_name ⇒ Object
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_name ⇒ Object
********** Class methods
259 260 261 |
# File 'app/models/concept/base.rb', line 259 def self.inline_partial_name "partials/concept/inline_base" end |
.new_link_partial_name ⇒ Object
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_info ⇒ Object
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_mode ⇒ Object
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_language ⇒ Object
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_term ⇒ Object
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_concept ⇒ Object
********** 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_language ⇒ Object
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_terms ⇒ Object
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
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_label ⇒ Object
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 |
#related_concepts_for_relation_class(relation_class, only_published = true) ⇒ Object
333 334 335 336 337 |
# File 'app/models/concept/base.rb', line 333 def (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_param ⇒ Object
355 356 357 |
# File 'app/models/concept/base.rb', line 355 def to_param "#{origin}" end |
#to_s ⇒ Object
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?”
370 371 372 373 |
# File 'app/models/concept/base.rb', line 370 def valid_with_full_validation? @full_validation = true valid? end |