Skip to content

Instantly share code, notes, and snippets.

@JuliaLovel
Created November 6, 2012 03:22
Show Gist options
  • Save JuliaLovel/4022361 to your computer and use it in GitHub Desktop.
Save JuliaLovel/4022361 to your computer and use it in GitHub Desktop.
Test a loop in view file
#Assign @similar_products the same way as view file
@similar_taxon = @product.taxons.first
@similar_products = (@product.similar_products.length > 0) ? @product.similar_products.first(3) : Spree::Product.in_taxon(@similar_taxon).limit(3)
if @similar_products.length >= 3
#Display a similar products container div, only if there are 3 similar products
page.should have_selector(".title-bar h4", :content => "Similar Products")
#Anything else that doesn't need to be looped goes here...
#In a loop, check for the divs corresponding to each similar product
@similar_products.each do |sp|
it "should have an entry for each similar product" do
page.should have_content(link_to(image_tag(sp.images[0].attachment.url(:mini)), product_path(sp))
#Test other view stuff
end
end
end
<% @similar_taxon = @product.taxons.first %>
<% @similar_products = (@product.similar_products.length > 0) ? @product.similar_products.first(3) : Spree::Product.in_taxon(@similar_taxon).limit(3) %>
<% if @similar_products.length >= 3 %>
<!-- start similar products -->
<div class="title-bar">
<h4>Similar Products</h4>
</div>
<div class="module-cnt border">
<% @similar_products.each_with_index do |sp, i| %>
<div class="module-row <%= (i==2) ? "" : "border-bottom" %> clearfix">
<%= (sp.images[0]) ? link_to(image_tag(sp.images[0].attachment.url(:mini), :width => "48", :class => "pull-left"), product_path(sp)) : link_to(image_tag("img/global/placeholder-48x48.gif", :width => "48", :class => "pull-left"), product_path(sp)) %>
<p class="pull-left">
<%= link_to sp.name.html_safe, product_path(sp) %><span class="stars stars-<%= sp.avg_rating %>"></span>
<span><%= number_to_currency(sp.price) %></span>
</p>
</div>
<% end %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment