From da790fcc28d69e6acd88aee6ea56d01719234cde Mon Sep 17 00:00:00 2001 From: David Carlos Date: Tue, 29 Jul 2014 17:33:42 -0300 Subject: [PATCH] Fixing invalid limit value in some blocks. --- app/models/block.rb | 4 ++++ app/models/profile_list_block.rb | 12 ++++++------ app/models/recent_documents_block.rb | 2 +- test/unit/profile_list_block_test.rb | 10 ++++++++++ test/unit/recent_documents_block_test.rb | 10 ++++++++++ test/unit/tags_block_test.rb | 10 ++++++++++ 6 files changed, 41 insertions(+), 7 deletions(-) diff --git a/app/models/block.rb b/app/models/block.rb index 1567781..36b173c 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -22,6 +22,10 @@ class Block < ActiveRecord::Base false end + def get_limit + [0,limit].max + end + def embed_code me = self proc do diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index 13e1afb..a7b4122 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -1,6 +1,6 @@ class ProfileListBlock < Block - attr_accessible :limit, :prioritize_profiles_with_image + attr_accessible :prioritize_profiles_with_image settings_items :limit, :type => :integer, :default => 6 settings_items :prioritize_profiles_with_image, :type => :boolean, :default => true @@ -18,13 +18,13 @@ class ProfileListBlock < Block result = nil visible_profiles = profiles.visible.includes([:image,:domains,:preferred_domain,:environment]) if !prioritize_profiles_with_image - result = visible_profiles.all(:limit => limit, :order => 'profiles.updated_at DESC').sort_by{ rand } - elsif profiles.visible.with_image.count >= limit - result = visible_profiles.with_image.all(:limit => limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand } + result = visible_profiles.all(:limit => get_limit, :order => 'profiles.updated_at DESC').sort_by{ rand } + elsif profiles.visible.with_image.count >= get_limit + result = visible_profiles.with_image.all(:limit => get_limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand } else - result = visible_profiles.with_image.sort_by{ rand } + visible_profiles.without_image.all(:limit => limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand } + 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 } end - result.slice(0..limit-1) + result.slice(0..get_limit-1) end def profile_count diff --git a/app/models/recent_documents_block.rb b/app/models/recent_documents_block.rb index 41c82f1..052f0f9 100644 --- a/app/models/recent_documents_block.rb +++ b/app/models/recent_documents_block.rb @@ -33,7 +33,7 @@ class RecentDocumentsBlock < Block end def docs - self.limit.nil? ? owner.recent_documents(nil, {}, false) : owner.recent_documents(self.limit, {}, false) + self.limit.nil? ? owner.recent_documents(nil, {}, false) : owner.recent_documents(self.get_limit, {}, false) end def self.expire_on diff --git a/test/unit/profile_list_block_test.rb b/test/unit/profile_list_block_test.rb index 1804b26..600ba61 100644 --- a/test/unit/profile_list_block_test.rb +++ b/test/unit/profile_list_block_test.rb @@ -216,4 +216,14 @@ class ProfileListBlockTest < ActiveSupport::TestCase assert ProfileListBlock.new.prioritize_profiles_with_image end + should 'return the max value in the range between zero and limit' do + block = ProfileListBlock.new + assert_equal 6, block.get_limit + end + + should 'return 0 if limit of the block is negative' do + block = ProfileListBlock.new + block.limit = -5 + assert_equal 0, block.get_limit + end end diff --git a/test/unit/recent_documents_block_test.rb b/test/unit/recent_documents_block_test.rb index f327ef0..78cc86b 100644 --- a/test/unit/recent_documents_block_test.rb +++ b/test/unit/recent_documents_block_test.rb @@ -89,4 +89,14 @@ class RecentDocumentsBlockTest < ActiveSupport::TestCase assert_equal 'always', @block.display end + should 'return the max value in the range between zero and limit' do + block = RecentDocumentsBlock.new + assert_equal 5, block.get_limit + end + + should 'return 0 if limit of the block is negative' do + block = RecentDocumentsBlock.new + block.limit = -5 + assert_equal 0, block.get_limit + end end diff --git a/test/unit/tags_block_test.rb b/test/unit/tags_block_test.rb index 595ebb9..eb28f7e 100644 --- a/test/unit/tags_block_test.rb +++ b/test/unit/tags_block_test.rb @@ -58,4 +58,14 @@ class TagsBlockTest < ActiveSupport::TestCase assert /\/first-tag".*\/second-tag".*\/third-tag"/m =~ block.content end + should 'return the max value in the range between zero and limit' do + block = TagsBlock.new + assert_equal 12, block.get_limit + end + + should '' do + block = TagsBlock.new + block.limit = -5 + assert_equal 0, block.get_limit + end end -- libgit2 0.21.2