[Git][noosfero/noosfero][master] 2 commits: highlights_block: improvements for usage in api

Leandro Nunes gitlab at mg.gitlab.com
Fri Dec 23 08:44:28 BRST 2016


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
55c2a187 by Victor Costa at 2016-12-22T17:19:04-03:00
highlights_block: improvements for usage in api

- Return slides in api_content
- Remove unused images associated to a highlights block

- - - - -
e6d3e9ba by Leandro Nunes at 2016-12-23T10:44:15+00:00
Merge branch 'api-highlights-block' into 'master'

highlights_block: improvements for usage in api

See merge request !1073
- - - - -


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
@@ -7,6 +7,8 @@ class HighlightsBlock < Block
   settings_items :shuffle, :type => 'boolean', :default => false
   settings_items :navigation, :type => 'boolean', :default => false
 
+  before_save :remove_unused_images
+
   before_save do |block|
     block.block_images = block.block_images.delete_if { |i| i[:image_id].blank? and i[:address].blank? and i[:position].blank? and i[:title].blank? }
     block.block_images.each do |i|
@@ -47,4 +49,26 @@ class HighlightsBlock < Block
     owner.image_galleries
   end
 
+  def display_api_content_by_default?
+    true
+  end
+
+  def api_content
+    slides = self.block_images
+    slides.each do |slide|
+      image = self.images.find_by(id: slide[:image_id])
+      if image.present?
+        slide[:image_src] = image.public_filename
+      else
+        slide[:image_id] = nil
+      end
+    end
+    { slides: slides }
+  end
+
+  def remove_unused_images
+    image_ids = self.block_images.map { |slide| slide[:image_id] }
+    images.where.not(id: image_ids).destroy_all
+  end
+
 end


=====================================
test/unit/highlights_block_test.rb
=====================================
--- a/test/unit/highlights_block_test.rb
+++ b/test/unit/highlights_block_test.rb
@@ -183,4 +183,29 @@ class HighlightsBlockTest < ActiveSupport::TestCase
     end
   end
 
+  should 'remove unused images when save' do
+    block = create(HighlightsBlock, images_builder: [{
+      uploaded_data: fixture_file_upload('/files/rails.png', 'image/png')
+    }])
+    assert_equal 1, block.images.size
+    block.save!
+    assert_equal 0, block.images.size
+  end
+
+  should 'return slides in api_content' 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}]
+    assert_equal 1, block.api_content[:slides].size
+  end
+
+  should 'not return image_id for images that does not exists anymore' 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}]
+    block.images.first.destroy
+    assert_nil block.api_content[:slides].first[:image_id]
+  end
 end



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


More information about the Noosfero-dev mailing list