Skip to content

Commit 27065c6

Browse files
committed
Deduplicate subject values in GDT
Why these changes are being introduced: The same subject value can appear twice in the full record view. In some cases, this is because the value appears in the record for different subject kinds. However, it's also entirely possible for the same kind/value pair to appear multiple times [in the same record](https://timdex-ui-prod.herokuapp.com/record/alma:990005961340106761), so merely including the subject kinds as we do in other TIMDEX UI apps does not solve this problem. Relevant ticket(s): * [GDT-297](https://mitlibraries.atlassian.net/browse/GDT-297) How this addresses that need: This adds a `deduplicate_subjects` method that calls `uniq` on a record's subject values (within a given term and across all term), then returns the deduplicated, two-dimensional array of values. Side effects of this change: Case is ignored to manage known instances of the same subject value appearing as capitalized and lower-case. However, passing the `downcase` to `uniq` causes it to select the first instance it finds, meaning it may select the lower-case value if it comes first in the record. This feels like more of an annoyance than a bug, so I've chosen not to add complexity to the deduplication method to fix it. (Calling `humanize` on every subject value was tempting, but I suspect there may be subjects that are intentionally case-sensitive.)
1 parent 924da70 commit 27065c6

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

app/helpers/record_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def geospatial_coordinates?(locations)
124124
locations.any? { |location| location['geoshape'] }
125125
end
126126

127+
# It is possible for duplicate subject values to appear for the same record.
128+
def deduplicate_subjects(subjects)
129+
return if subjects.blank?
130+
131+
subjects.map { |subject| subject['value'].uniq(&:downcase) }.uniq { |values| values.map(&:downcase) }
132+
end
133+
127134
private
128135

129136
def render_kind_value(list)

app/views/record/_record_geo.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
<% if @record['subjects'].present? %>
6363
<h3 class="section-title">Subjects</h3>
6464
<ul>
65-
<% @record['subjects'].each do |subject| %>
66-
<li><%= subject['value'].join('; ') %></li>
65+
<% deduplicate_subjects(@record['subjects'])&.each do |subject| %>
66+
<li><%= subject.join('; ') %></li>
6767
<% end %>
6868
</ul>
6969
<% end %>

test/helpers/record_helper_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,32 @@ class RecordHelperTest < ActionView::TestCase
301301
locations = []
302302
assert_not geospatial_coordinates?(locations)
303303
end
304+
305+
test 'deduplicate_subjects returns only unique subjects' do
306+
# within the same subject
307+
duplicative_subject = [{ 'kind' => 'foo', 'value' => ['bar', 'bar', 'baz']}]
308+
assert_equal [['bar', 'baz']], deduplicate_subjects(duplicative_subject)
309+
310+
# across multiple subjects
311+
multiple_duplicative_subjects = [{ 'kind' => 'foo', 'value' => ['bar'] },
312+
{ 'kind' => 'baz', 'value' => ['bar'] }]
313+
assert_equal [['bar']], deduplicate_subjects(multiple_duplicative_subjects)
314+
end
315+
316+
test 'deduplicate_subjects ignores case' do
317+
# within the same subject
318+
duplicative_subject = [{ 'kind' => 'foo', 'value' => ['Bar', 'BAR', 'bar']}]
319+
assert_equal [['Bar']], deduplicate_subjects(duplicative_subject)
320+
321+
# across multiple subjects
322+
multiple_duplicative_subjects = [{ 'kind' => 'foo', 'value' => ['Bar'] },
323+
{ 'kind' => 'foo', 'value' => ['BAR'] },
324+
{ 'kind' => 'foo', 'value' => ['bar'] }]
325+
assert_equal [['Bar']], deduplicate_subjects(multiple_duplicative_subjects)
326+
end
327+
328+
test 'deduplicate_subjects returns nothing if subjects are not present' do
329+
subjects = []
330+
assert_nil deduplicate_subjects(subjects)
331+
end
304332
end

0 commit comments

Comments
 (0)