Commit b9adeec95a75ecae91356b3ed3d81ee6cd62ba74

Authored by AntonioTerceiro
1 parent 3a721d63

ActionItem490: enhancing products block

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2151 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/products_block.rb
@@ -8,8 +8,27 @@ class ProductsBlock < Block @@ -8,8 +8,27 @@ class ProductsBlock < Block
8 block_title(_('Products')) + 8 block_title(_('Products')) +
9 content_tag( 9 content_tag(
10 'ul', 10 'ul',
11 - 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' ) )} 11 + 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' )}
12 ) 12 )
13 end 13 end
14 14
  15 + def footer
  16 + link_to(_('View all'), owner.generate_url(:controller => 'catalog', :action => 'index'))
  17 + end
  18 +
  19 + settings_items :product_ids, Array
  20 +
  21 + def products(reload = false)
  22 + if product_ids.blank?
  23 + products_list = owner.products(reload)
  24 + result = []
  25 + [4, products_list.size].min.times do
  26 + result << products_list.rand
  27 + end
  28 + result
  29 + else
  30 + product_ids.map {|item| owner.products.find(item) }
  31 + end
  32 + end
  33 +
15 end 34 end
test/unit/products_block_test.rb
@@ -29,4 +29,69 @@ class ProductsBlockTest &lt; ActiveSupport::TestCase @@ -29,4 +29,69 @@ class ProductsBlockTest &lt; ActiveSupport::TestCase
29 29
30 end 30 end
31 31
  32 + should 'point to all products in footer' do
  33 + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise')
  34 + enterprise.products.create!(:name => 'product one')
  35 + enterprise.products.create!(:name => 'product two')
  36 +
  37 + block.stubs(:owner).returns(enterprise)
  38 +
  39 + footer = block.footer
  40 +
  41 + assert_tag_in_string footer, :tag => 'a', :attributes => { :href => /\/catalog\/testenterprise$/ }, :content => 'View all'
  42 + end
  43 +
  44 + should 'list 4 random products by default' do
  45 + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise')
  46 + enterprise.products.create!(:name => 'product one')
  47 + enterprise.products.create!(:name => 'product two')
  48 + enterprise.products.create!(:name => 'product three')
  49 + enterprise.products.create!(:name => 'product four')
  50 + enterprise.products.create!(:name => 'product five')
  51 +
  52 + block.stubs(:owner).returns(enterprise)
  53 +
  54 + assert_equal 4, block.products.size
  55 + end
  56 +
  57 + should 'list all products if less than 4 by default' do
  58 + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise')
  59 + enterprise.products.create!(:name => 'product one')
  60 + enterprise.products.create!(:name => 'product two')
  61 + enterprise.products.create!(:name => 'product three')
  62 +
  63 + block.stubs(:owner).returns(enterprise)
  64 +
  65 + assert_equal 3, block.products.size
  66 + end
  67 +
  68 +
  69 + should 'be able to set product_ids and have them listed' do
  70 + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise')
  71 +
  72 + p1 = enterprise.products.create!(:name => 'product one')
  73 + p2 = enterprise.products.create!(:name => 'product two')
  74 + p3 = enterprise.products.create!(:name => 'product three')
  75 + p4 = enterprise.products.create!(:name => 'product four')
  76 + p5 = enterprise.products.create!(:name => 'product five')
  77 +
  78 + block.stubs(:owner).returns(enterprise)
  79 +
  80 + block.product_ids = [p1, p3, p5].map(&:id)
  81 + assert_equal [p1, p3, p5], block.products
  82 + end
  83 +
  84 + should 'save product_ids' do
  85 + enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise')
  86 + p1 = enterprise.products.create!(:name => 'product one')
  87 + p2 = enterprise.products.create!(:name => 'product two')
  88 +
  89 + block = ProductsBlock.new
  90 + enterprise.boxes.first.blocks << block
  91 + block.product_ids = [p1.id, p2.id]
  92 + block.save!
  93 +
  94 + assert_equal [p1.id, p2.id], ProductsBlock.find(block.id).product_ids
  95 + end
  96 +
32 end 97 end