Skip to content

A platform for community discussion. Free, open, simple.

FIX: 'have_uploads' scope should include all uploads without multisite 'upload_path' prefix

Vinoth Kannan8e40c35eb85e
diff --git a/app/models/post.rb b/app/models/post.rb
index 98d7a821db8..f8228e5655f 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -113,7 +113,7 @@ class Post < ActiveRecord::Base
scope :have_uploads, -> {
where(
- "(posts.cooked LIKE '%<a %' OR posts.cooked LIKE '%<img %') AND posts.cooked LIKE ?",
+ "(posts.cooked LIKE '%<a %' OR posts.cooked LIKE '%<img %') AND (posts.cooked LIKE ? OR posts.cooked LIKE '%/original/%' OR posts.cooked LIKE '%/optimized/%')",
"%/uploads/#{RailsMultisite::ConnectionManagement.current_db}/%"
)
}
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index da3354ea19c..21c2e657e88 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -1341,4 +1341,18 @@ describe Post do
end
end
+ context "have_uploads" do
+ it "should find all posts with the upload" do
+ ids = []
+ ids << Fabricate(:post, cooked: "A post with upload <img src='/uploads/default/1/defghijklmno.png'>").id
+ ids << Fabricate(:post, cooked: "A post with optimized image <img src='/uploads/default/_optimized/601/961/defghijklmno.png'>").id
+ Fabricate(:post)
+ ids << Fabricate(:post, cooked: "A post with upload <img src='/uploads/default/original/1X/abc/defghijklmno.png'>").id
+ ids << Fabricate(:post, cooked: "A post with upload link <a href='https://cdn.example.com/original/1X/abc/defghijklmno.png'>").id
+ ids << Fabricate(:post, cooked: "A post with optimized image <img src='https://cdn.example.com/bucket/optimized/1X/abc/defghijklmno.png'>").id
+ Fabricate(:post, cooked: "A post with external link <a href='https://example.com/wp-content/uploads/abcdef.gif'>")
+ expect(Post.have_uploads.order(:id).pluck(:id)).to eq(ids)
+ end
+ end
+
end