Skip to content

Commit

Permalink
Merge pull request #340 from innoq/consistent_concept_labeling_fix
Browse files Browse the repository at this point in the history
iQvoc allows concept creation which are not consistent to skos data model
  • Loading branch information
mjansing committed May 13, 2015
2 parents 307c9a9 + 6c7270b commit 26f982a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
29 changes: 28 additions & 1 deletion app/models/concept/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Validations
validate :rooted_top_terms
validate :valid_rank_for_ranked_relations
validate :unique_pref_label
validate :exclusive_pref_label
validate :unique_alt_labels
end

# top term and broader relations are mutually exclusive
Expand Down Expand Up @@ -65,7 +67,7 @@ def unique_pref_label
# 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.
Labeling::SKOS::Base.
joins(:owner, :target).
where(labels: { value: l.value, language: l.language }).
where('labelings.owner_id != ?', id).
Expand All @@ -87,6 +89,31 @@ def unique_pref_label
end
end

def exclusive_pref_label
if validatable_for_publishing?
alt_labels = labeling_skos_alt_labels.collect { |l| l.target }

if alt_labels.include? pref_label
errors.add :base,
I18n.t('txt.models.concept.pref_label_defined_in_alt_labels',
label: pref_label.value)
end
end
end

def unique_alt_labels
if validatable_for_publishing?
alt_labels = labeling_skos_alt_labels.collect { |l| l.target }
duplicates = alt_labels.detect { |e| alt_labels.count(e) > 1 }

if duplicates.present?
errors.add :base,
I18n.t('txt.models.concept.alt_labels_not_unique',
label: pref_label.value)
end
end
end

def valid_rank_for_ranked_relations
if validatable_for_publishing?
relations.each do |relation|
Expand Down
12 changes: 12 additions & 0 deletions app/models/label/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ def <=>(other)
self.to_s.downcase <=> other.to_s.downcase
end

def ==(other)
language == other.language && value == other.value
end

def eql?(other)
self == other
end

def hash
[value, language].hash
end

def to_literal
"\"#{value}\"@#{language}"
end
Expand Down
2 changes: 2 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,5 @@ de:
no_pref_label: "-- kein PrefLabel --"
pref_label_not_unique: "Das PrefLabel %{label} ist bereits in Verwendung."
pref_labels_not_unique: "Die PrefLabels %{label} sind bereits in Verwendung."
pref_label_defined_in_alt_labels: "Das AltLabel %{label} wird bereits als PrefLabel verwendet."
alt_labels_not_unique: "Das AltLabel %{label} wird zweimal als AltLabel verwendet."
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,5 @@ en:
no_pref_label: "-- no PrefLabel --"
pref_label_not_unique: "The preferred label '%{label}' is already in use."
pref_labels_not_unique: "The preferred labels '%{label}' are already in use."
pref_label_defined_in_alt_labels: "The alternative label %{label} is already used as a preferred label."
alt_labels_not_unique: "The alternative label %{label} is used twice."
29 changes: 29 additions & 0 deletions test/models/concept_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,35 @@ class ConceptTest < ActiveSupport::TestCase
refute bear_two.publishable?
end

test 'unique alt labels' do
tiger = RDFAPI.devour 'tiger', 'a', 'skos:Concept'
RDFAPI.devour tiger, 'skos:prefLabel', '"Tiger"@en'

assert tiger.save
assert tiger.publishable?

# two identical alt labels
RDFAPI.devour tiger, 'skos:altLabel', '"Big cat"@en'
RDFAPI.devour tiger, 'skos:altLabel', '"Big cat"@en'

tiger.save!
refute tiger.publishable?, 'There should be no identical alt labels'
end

test 'distinct labels' do
monkey = RDFAPI.devour 'Monkey', 'a', 'skos:Concept'
RDFAPI.devour monkey, 'skos:prefLabel', '"Monkey"@en'

assert monkey.save
assert monkey.publishable?

# identical to pref label
RDFAPI.devour monkey, 'skos:altLabel', '"Monkey"@en'

monkey.save!
refute monkey.publishable?, 'There should be no duplicates between prefLabel/altLabel'
end

test 'multiple pref labels' do
concept = RDFAPI.devour 'bear', 'a', 'skos:Concept'
RDFAPI.devour concept, 'skos:prefLabel', '"Bear"@en'
Expand Down

0 comments on commit 26f982a

Please sign in to comment.