FIX: Incorrect/missing extension in short_url fails to map to upload.

`Hash#invert` causes us to lose keys if the hash contains similar
values.
This commit is contained in:
Guo Xiang Tan 2018-09-05 21:46:43 +08:00
parent 3134dd4763
commit f3aef2cc83
2 changed files with 11 additions and 3 deletions

View File

@ -63,13 +63,18 @@ module PrettyText
end
if map.length > 0
reverse_map = map.invert
reverse_map = {}
map.each do |key, value|
reverse_map[value] ||= []
reverse_map[value] << key
end
Upload.where(sha1: map.values).pluck(:sha1, :url).each do |row|
sha1, url = row
if short_url = reverse_map[sha1]
result[short_url] = url
if short_urls = reverse_map[sha1]
short_urls.each { |short_url| result[short_url] = url }
end
end
end

View File

@ -1228,6 +1228,8 @@ HTML
- test
- ![upload](#{upload.short_url})
![upload](#{upload.short_url.gsub!(".png", "")})
RAW
cooked = <<~HTML
@ -1243,6 +1245,7 @@ HTML
</ul>
</li>
</ul>
<p><img src="#{upload.url}" alt="upload"></p>
HTML
expect(PrettyText.cook(raw)).to eq(cooked.strip)