From b9adeec95a75ecae91356b3ed3d81ee6cd62ba74 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 4 Jul 2008 17:54:04 +0000 Subject: [PATCH] ActionItem490: enhancing products block --- app/models/products_block.rb | 21 ++++++++++++++++++++- test/unit/products_block_test.rb | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/app/models/products_block.rb b/app/models/products_block.rb index e498e08..11f1aa6 100644 --- a/app/models/products_block.rb +++ b/app/models/products_block.rb @@ -8,8 +8,27 @@ class ProductsBlock < Block block_title(_('Products')) + content_tag( 'ul', - owner.products.map {|product| content_tag('li', link_to(product.name, product.url), :class => 'product', :style => 'background-image:url(%s)' % ( product.image ? product.image.public_filename(:minor) : '/images/icons-app/product-default-pic-minor.png' ) )} + owner.products.map {|product| content_tag('li', link_to(product.name, product.url, :style => 'background-image:url(%s)' % ( product.image ? product.image.public_filename(:minor) : '/images/icons-app/product-default-pic-minor.png' )), :class => 'product' )} ) end + def footer + link_to(_('View all'), owner.generate_url(:controller => 'catalog', :action => 'index')) + end + + settings_items :product_ids, Array + + def products(reload = false) + if product_ids.blank? + products_list = owner.products(reload) + result = [] + [4, products_list.size].min.times do + result << products_list.rand + end + result + else + product_ids.map {|item| owner.products.find(item) } + end + end + end diff --git a/test/unit/products_block_test.rb b/test/unit/products_block_test.rb index ac56bce..36927d3 100644 --- a/test/unit/products_block_test.rb +++ b/test/unit/products_block_test.rb @@ -29,4 +29,69 @@ class ProductsBlockTest < ActiveSupport::TestCase end + should 'point to all products in footer' do + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise') + enterprise.products.create!(:name => 'product one') + enterprise.products.create!(:name => 'product two') + + block.stubs(:owner).returns(enterprise) + + footer = block.footer + + assert_tag_in_string footer, :tag => 'a', :attributes => { :href => /\/catalog\/testenterprise$/ }, :content => 'View all' + end + + should 'list 4 random products by default' do + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise') + enterprise.products.create!(:name => 'product one') + enterprise.products.create!(:name => 'product two') + enterprise.products.create!(:name => 'product three') + enterprise.products.create!(:name => 'product four') + enterprise.products.create!(:name => 'product five') + + block.stubs(:owner).returns(enterprise) + + assert_equal 4, block.products.size + end + + should 'list all products if less than 4 by default' do + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise') + enterprise.products.create!(:name => 'product one') + enterprise.products.create!(:name => 'product two') + enterprise.products.create!(:name => 'product three') + + block.stubs(:owner).returns(enterprise) + + assert_equal 3, block.products.size + end + + + should 'be able to set product_ids and have them listed' do + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise') + + p1 = enterprise.products.create!(:name => 'product one') + p2 = enterprise.products.create!(:name => 'product two') + p3 = enterprise.products.create!(:name => 'product three') + p4 = enterprise.products.create!(:name => 'product four') + p5 = enterprise.products.create!(:name => 'product five') + + block.stubs(:owner).returns(enterprise) + + block.product_ids = [p1, p3, p5].map(&:id) + assert_equal [p1, p3, p5], block.products + end + + should 'save product_ids' do + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise') + p1 = enterprise.products.create!(:name => 'product one') + p2 = enterprise.products.create!(:name => 'product two') + + block = ProductsBlock.new + enterprise.boxes.first.blocks << block + block.product_ids = [p1.id, p2.id] + block.save! + + assert_equal [p1.id, p2.id], ProductsBlock.find(block.id).product_ids + end + end -- libgit2 0.21.2