DEV: Replace #pluck_first freedom patch with AR #pick in core (#19893)

The #pluck_first freedom patch, first introduced by @danielwaterworth has served us well, and is used widely throughout both core and plugins. It seems to have been a common enough use case that Rails 6 introduced it's own method #pick with the exact same implementation. This allows us to retire the freedom patch and switch over to the built-in ActiveRecord method.

There is no replacement for #pluck_first!, but a quick search shows we are using this in a very limited capacity, and in some cases incorrectly (by assuming a nil return rather than an exception), which can quite easily be replaced with #pick plus some extra handling.
This commit is contained in:
Ted Johansson
2023-02-13 12:39:45 +08:00
committed by GitHub
parent a90ad52dff
commit 25a226279a
56 changed files with 112 additions and 123 deletions

View File

@@ -254,9 +254,7 @@ class TopicEmbed < ActiveRecord::Base
def self.topic_id_for_embed(embed_url)
embed_url = normalize_url(embed_url).sub(%r{\Ahttps?\://}, "")
TopicEmbed.where("embed_url ~* ?", "^https?://#{Regexp.escape(embed_url)}$").pluck_first(
:topic_id,
)
TopicEmbed.where("embed_url ~* ?", "^https?://#{Regexp.escape(embed_url)}$").pick(:topic_id)
end
def self.first_paragraph_from(html)
@@ -281,7 +279,7 @@ class TopicEmbed < ActiveRecord::Base
Discourse
.cache
.fetch("embed-topic:#{post.topic_id}", expires_in: 10.minutes) do
url = TopicEmbed.where(topic_id: post.topic_id).pluck_first(:embed_url)
url = TopicEmbed.where(topic_id: post.topic_id).pick(:embed_url)
response = TopicEmbed.find_remote(url)
body = response.body