keep uploads reverse index up to date
This commit is contained in:
parent
ae3543872c
commit
7bdc616040
|
@ -36,6 +36,8 @@ class CookedPostProcessor
|
|||
# retrieve the associated upload, if any
|
||||
upload = get_upload_from_url(img['src'])
|
||||
if upload.present?
|
||||
# update reverse index
|
||||
associate_to_post upload
|
||||
# create a thumbnail
|
||||
upload.create_thumbnail!
|
||||
# optimize image
|
||||
|
@ -87,6 +89,13 @@ class CookedPostProcessor
|
|||
end
|
||||
end
|
||||
|
||||
def associate_to_post(upload)
|
||||
return if PostUpload.where(post_id: @post.id, upload_id: upload.id).count > 0
|
||||
PostUpload.create({ post_id: @post.id, upload_id: upload.id })
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
# do not care if it's already associated
|
||||
end
|
||||
|
||||
def optimize_image(img)
|
||||
return img["src"]
|
||||
# 1) optimize using image_optim
|
||||
|
|
|
@ -57,6 +57,23 @@ describe CookedPostProcessor do
|
|||
|
||||
end
|
||||
|
||||
context 'with uploaded images in the post' do
|
||||
before do
|
||||
@topic = Fabricate(:topic)
|
||||
@post = Fabricate(:post_with_uploads, topic: @topic, user: @topic.user)
|
||||
@cpp = CookedPostProcessor.new(@post)
|
||||
@cpp.expects(:get_upload_from_url).returns(Fabricate(:upload))
|
||||
@cpp.expects(:get_size).returns([100,200])
|
||||
end
|
||||
|
||||
it "keeps reverse index up to date" do
|
||||
@cpp.post_process_images
|
||||
@post.uploads.reload
|
||||
@post.uploads.count.should == 1
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'with unsized images in the post' do
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:topic) { Fabricate(:topic, user: user) }
|
||||
|
|
|
@ -40,6 +40,13 @@ Fabricator(:post_with_s3_image_url, from: :post) do
|
|||
"
|
||||
end
|
||||
|
||||
Fabricator(:post_with_uploads, from: :post) do
|
||||
cooked "
|
||||
<img src='/uploads/default/1/1234567890123456.jpg' height='100' width='100'>
|
||||
"
|
||||
end
|
||||
|
||||
|
||||
Fabricator(:basic_reply, from: :post) do
|
||||
user(:coding_horror)
|
||||
reply_to_post_number 1
|
||||
|
|
|
@ -4,5 +4,5 @@ Fabricator(:upload) do
|
|||
filesize 1234
|
||||
width 100
|
||||
height 200
|
||||
url "/uploads/default/123456789.jpg"
|
||||
url "/uploads/default/1/1234567890123456.jpg"
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue