Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions app/models/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ def links
delegate :keys, to: :links

def unique_links
links.uniq do |_short, long|
send(long)
seen_urls = {}
links.select do |short, long|
url = send(long)
# always include 'code' (source_code_uri) even if URL is duplicate
if short == "code"
true
elsif seen_urls[url]
false
else
seen_urls[url] = true
true
end
Comment on lines +38 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slightly confusing to me since there's a lot of booleans and conditionals, how about this?

Have REQUIRED_INDEXED_LINK_KEYS

Suggested change
seen_urls = {}
links.select do |short, long|
url = send(long)
# always include 'code' (source_code_uri) even if URL is duplicate
if short == "code"
true
elsif seen_urls[url]
false
else
seen_urls[url] = true
true
end
unique_links = links.uniq do |_short, long|
send(long)
end.to_h
# always include certain URLs even if URL is a duplicate
links.select do |short, _long|
unique_links.key?(short) || REQUIRED_INDEXED_LINK_KEYS.include?(short)
end

end
end

Expand Down
13 changes: 13 additions & 0 deletions test/models/links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ class LinksTest < ActiveSupport::TestCase
assert_equal([["home", "https://example.code"], ["download", "/downloads/.gem"]], enumerated)
end

should "always include source code even when duplicate" do
metadata = { "homepage_uri" => "https://example.com", "source_code_uri" => "https://example.com" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
metadata = { "homepage_uri" => "https://example.com", "source_code_uri" => "https://example.com" }
metadata = { "homepage_uri" => "https://example.code", "source_code_uri" => "https://example.code" }

I think you mean this?

version = build(:version, indexed: true, metadata: metadata)
rubygem = build(:rubygem, linkset: build(:linkset), versions: [version])
links = rubygem.links(version)

enumerated = links.map do |short, value|
[short, value]
end

assert_equal([["home", "https://example.code"], ["code", "https://example.code"], ["download", "/downloads/.gem"]], enumerated)
end

context "metadata includes unknown uri key" do
setup do
metadata = {
Expand Down
Loading