Module: Concept::Validations

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
app/models/concept/validations.rb

Instance Method Summary collapse

Instance Method Details

#distinct_versionsObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/concept/validations.rb', line 17

def distinct_versions
  query = Concept::Base.by_origin(origin)
  existing_total = query.count
  if existing_total >= 2
    errors.add :base, I18n.t("txt.models.concept.version_error", :origin => origin)
  elsif existing_total == 1
    unless (query.published.count == 0 and published?) or
           (query.published.count == 1 and not published?)
      errors.add :base, I18n.t("txt.models.concept.version_error", :origin => origin)
    end
  end
end

#exclusive_top_termObject

top term and broader relations are mutually exclusive



31
32
33
34
35
36
37
# File 'app/models/concept/validations.rb', line 31

def 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

#pref_label_in_primary_thesaurus_languageObject



50
51
52
53
54
55
56
57
58
59
# File 'app/models/concept/validations.rb', line 50

def pref_label_in_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

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



41
42
43
44
45
46
47
48
# File 'app/models/concept/validations.rb', line 41

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

#unique_pref_labelObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/concept/validations.rb', line 77

def unique_pref_label
  if @full_validation
    # checks if there are any existing pref labels with the same
    # language and value
    conflicting_pref_labels = pref_labels.select do |l|
      Labeling::SKOS::PrefLabel.
        joins(:owner, :target).
        where(:labels => { :value => l.value, :language => l.language }).
        where("labelings.owner_id != ?", id).
        where("concepts.origin != ?", origin).
        any?
    end

    if conflicting_pref_labels.any?
      if conflicting_pref_labels.one?
        errors.add :base,
          I18n.t("txt.models.concept.pref_label_not_unique",
            :label => conflicting_pref_labels.last.value)
      else
        errors.add :base,
          I18n.t("txt.models.concept.pref_labels_not_unique",
            :label => conflicting_pref_labels.map(&:value).join(", "))
      end
    end
  end
end

#unique_pref_label_languageObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/concept/validations.rb', line 61

def unique_pref_label_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.compact.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

#valid_rank_for_ranked_relationsObject



104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/concept/validations.rb', line 104

def valid_rank_for_ranked_relations
  if @full_validation
    relations.each do |relation|
      if relation.class.rankable? && !(0..100).include?(relation.rank)
        errors.add :base, I18n.t("txt.models.concept.invalid_rank_for_ranked_relations",
          :relation => relation.class.model_name.human.downcase,
          :relation_target_label => relation.target.pref_label.to_s)
      end
    end
  end
end