[Git][noosfero/noosfero][master] 2 commits: highlights_block: keep image_src for block images

Leandro Nunes gitlab at mg.gitlab.com
Fri Dec 23 16:56:36 BRST 2016


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
823b8b45 by Victor Costa at 2016-12-23T09:18:18-03:00
highlights_block: keep image_src for block images

- - - - -
73c5c09b by Leandro Nunes at 2016-12-23T18:56:30+00:00
Merge branch 'highlights_block' into 'master'

highlights_block: keep image_src for block images

See merge request !1074
- - - - -


2 changed files:

- app/models/highlights_block.rb
- test/unit/highlights_block_test.rb


Changes:

=====================================
app/models/highlights_block.rb
=====================================
--- a/app/models/highlights_block.rb
+++ b/app/models/highlights_block.rb
@@ -19,12 +19,8 @@ class HighlightsBlock < Block
       end
       i[:new_window] = i[:new_window] == '1' ? true : false
 
-      begin
-        file = UploadedFile.find(i[:image_id])
-        i[:image_src] = file.public_filename
-      rescue
-        i[:image_src] = nil
-      end
+      uploaded_file = UploadedFile.find_by(id: i[:image_id])
+      i[:image_src] = uploaded_file.public_filename if uploaded_file.present?
     end
   end
 


=====================================
test/unit/highlights_block_test.rb
=====================================
--- a/test/unit/highlights_block_test.rb
+++ b/test/unit/highlights_block_test.rb
@@ -54,7 +54,7 @@ class HighlightsBlockTest < ActiveSupport::TestCase
   should 'remove images with blank fields' do
     h = HighlightsBlock.new(:block_images => [{:image_id => 1, :address => '/address', :position => 1, :title => 'address'}, {:image_id => '', :address => '', :position => '', :title => ''}])
     h.save!
-    assert_equal [{:image_id => 1, :address => '/address', :position => 1, :title => 'address', :new_window => false, :image_src => nil}], h.block_images
+    assert_equal [{:image_id => 1, :address => '/address', :position => 1, :title => 'address', :new_window => false}], h.block_images
   end
 
   should 'replace 1 and 0 by true and false in new_window attribute' do
@@ -91,9 +91,9 @@ class HighlightsBlockTest < ActiveSupport::TestCase
 
   should 'not list non existent image' do
     file = mock()
-    UploadedFile.expects(:find).with(1).returns(file)
+    UploadedFile.expects(:find_by).with({id: 1}).returns(file)
     file.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(0).returns(nil)
+    UploadedFile.expects(:find_by).with({id: 0}).returns(nil)
     block = HighlightsBlock.new(:block_images => [{:image_id => 1, :address => '/address', :position => 1, :title => 'address'}, {:image_id => '', :address => 'some', :position => '2', :title => 'Some'}])
     block.save!
     block.reload
@@ -104,13 +104,13 @@ class HighlightsBlockTest < ActiveSupport::TestCase
   should 'list images in order' do
     f1 = mock()
     f1.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(1).returns(f1)
+    UploadedFile.expects(:find_by).with({id: 1}).returns(f1)
     f2 = mock()
     f2.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(2).returns(f2)
+    UploadedFile.expects(:find_by).with({id: 2}).returns(f2)
     f3 = mock()
     f3.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(3).returns(f3)
+    UploadedFile.expects(:find_by).with({id: 3}).returns(f3)
     block = HighlightsBlock.new
     i1 = {:image_id => 1, :address => '/address', :position => 3, :title => 'address'}
     i2 = {:image_id => 2, :address => '/address', :position => 1, :title => 'address'}
@@ -137,7 +137,7 @@ class HighlightsBlockTest < ActiveSupport::TestCase
     Noosfero.stubs(:root).returns("/social")
     f1 = mock()
     f1.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(1).returns(f1)
+    UploadedFile.expects(:find_by).with({id: 1}).returns(f1)
     block = HighlightsBlock.new
     i1 = {:image_id => 1, :address => '/address', :position => 3, :title => 'address'}
     block.block_images = [i1]
@@ -150,7 +150,7 @@ class HighlightsBlockTest < ActiveSupport::TestCase
     Noosfero.stubs(:root).returns("/social")
     f1 = mock()
     f1.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(1).returns(f1)
+    UploadedFile.expects(:find_by).with({id: 1}).returns(f1)
     block = HighlightsBlock.new
     i1 = {:image_id => 1, :address => '/social/address', :position => 3, :title => 'address'}
     block.block_images = [i1]
@@ -163,7 +163,7 @@ class HighlightsBlockTest < ActiveSupport::TestCase
     Noosfero.stubs(:root).returns("/social")
     f1 = mock()
     f1.expects(:public_filename).returns('/img_address')
-    UploadedFile.expects(:find).with(1).returns(f1)
+    UploadedFile.expects(:find_by).with({id: 1}).returns(f1)
     block = HighlightsBlock.new
     i1 = {:image_id => 1, :address => '/address'}
     block.block_images = [i1]
@@ -208,4 +208,13 @@ class HighlightsBlockTest < ActiveSupport::TestCase
     block.images.first.destroy
     assert_nil block.api_content[:slides].first[:image_id]
   end
+
+  should 'keep image_src for images that was not found as uploaded_file' do
+    block = create(HighlightsBlock, images_builder: [{
+      uploaded_data: fixture_file_upload('/files/rails.png', 'image/png')
+    }])
+    block.block_images = [{image_id: block.images.first.id, image_src: block.images.first.public_filename}]
+    block.save!
+    assert block.block_images.first[:image_src].present?
+  end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/e6d3e9bac8c01d5f255547a93a1c8b5277b0926f...73c5c09b0ccfd663e58f163afb1e6a956afd31bf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20161223/3432623f/attachment-0001.html>


More information about the Noosfero-dev mailing list