Commit da790fcc28d69e6acd88aee6ea56d01719234cde

Authored by David Silva
1 parent 22243571

Fixing invalid limit value in some blocks.

Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com>
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
app/models/block.rb
... ... @@ -22,6 +22,10 @@ class Block &lt; ActiveRecord::Base
22 22 false
23 23 end
24 24  
  25 + def get_limit
  26 + [0,limit].max
  27 + end
  28 +
25 29 def embed_code
26 30 me = self
27 31 proc do
... ...
app/models/profile_list_block.rb
1 1 class ProfileListBlock < Block
2 2  
3   - attr_accessible :limit, :prioritize_profiles_with_image
  3 + attr_accessible :prioritize_profiles_with_image
4 4  
5 5 settings_items :limit, :type => :integer, :default => 6
6 6 settings_items :prioritize_profiles_with_image, :type => :boolean, :default => true
... ... @@ -18,13 +18,13 @@ class ProfileListBlock &lt; Block
18 18 result = nil
19 19 visible_profiles = profiles.visible.includes([:image,:domains,:preferred_domain,:environment])
20 20 if !prioritize_profiles_with_image
21   - result = visible_profiles.all(:limit => limit, :order => 'profiles.updated_at DESC').sort_by{ rand }
22   - elsif profiles.visible.with_image.count >= limit
23   - result = visible_profiles.with_image.all(:limit => limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand }
  21 + result = visible_profiles.all(:limit => get_limit, :order => 'profiles.updated_at DESC').sort_by{ rand }
  22 + elsif profiles.visible.with_image.count >= get_limit
  23 + result = visible_profiles.with_image.all(:limit => get_limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand }
24 24 else
25   - result = visible_profiles.with_image.sort_by{ rand } + visible_profiles.without_image.all(:limit => limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand }
  25 + result = visible_profiles.with_image.sort_by{ rand } + visible_profiles.without_image.all(:limit => get_limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand }
26 26 end
27   - result.slice(0..limit-1)
  27 + result.slice(0..get_limit-1)
28 28 end
29 29  
30 30 def profile_count
... ...
app/models/recent_documents_block.rb
... ... @@ -33,7 +33,7 @@ class RecentDocumentsBlock &lt; Block
33 33 end
34 34  
35 35 def docs
36   - self.limit.nil? ? owner.recent_documents(nil, {}, false) : owner.recent_documents(self.limit, {}, false)
  36 + self.limit.nil? ? owner.recent_documents(nil, {}, false) : owner.recent_documents(self.get_limit, {}, false)
37 37 end
38 38  
39 39 def self.expire_on
... ...
test/unit/profile_list_block_test.rb
... ... @@ -216,4 +216,14 @@ class ProfileListBlockTest &lt; ActiveSupport::TestCase
216 216 assert ProfileListBlock.new.prioritize_profiles_with_image
217 217 end
218 218  
  219 + should 'return the max value in the range between zero and limit' do
  220 + block = ProfileListBlock.new
  221 + assert_equal 6, block.get_limit
  222 + end
  223 +
  224 + should 'return 0 if limit of the block is negative' do
  225 + block = ProfileListBlock.new
  226 + block.limit = -5
  227 + assert_equal 0, block.get_limit
  228 + end
219 229 end
... ...
test/unit/recent_documents_block_test.rb
... ... @@ -89,4 +89,14 @@ class RecentDocumentsBlockTest &lt; ActiveSupport::TestCase
89 89 assert_equal 'always', @block.display
90 90 end
91 91  
  92 + should 'return the max value in the range between zero and limit' do
  93 + block = RecentDocumentsBlock.new
  94 + assert_equal 5, block.get_limit
  95 + end
  96 +
  97 + should 'return 0 if limit of the block is negative' do
  98 + block = RecentDocumentsBlock.new
  99 + block.limit = -5
  100 + assert_equal 0, block.get_limit
  101 + end
92 102 end
... ...
test/unit/tags_block_test.rb
... ... @@ -58,4 +58,14 @@ class TagsBlockTest &lt; ActiveSupport::TestCase
58 58 assert /\/first-tag".*\/second-tag".*\/third-tag"/m =~ block.content
59 59 end
60 60  
  61 + should 'return the max value in the range between zero and limit' do
  62 + block = TagsBlock.new
  63 + assert_equal 12, block.get_limit
  64 + end
  65 +
  66 + should '' do
  67 + block = TagsBlock.new
  68 + block.limit = -5
  69 + assert_equal 0, block.get_limit
  70 + end
61 71 end
... ...