Permalink
Browse files

Product#images are sorted by their position [Fixes #1446]

  • Loading branch information...
joahking authored and radar committed Apr 26, 2012
1 parent f2e9cc3 commit 511537771272713db5b3b7acaa48ae69379e96cc
Showing with 15 additions and 2 deletions.
  1. +2 −2 core/app/models/spree/product.rb
  2. +13 −0 core/spec/models/product_spec.rb
@@ -59,7 +59,7 @@ class Product < ActiveRecord::Base
accepts_nested_attributes_for :variants, :allow_destroy => true
def variant_images
Image.find_by_sql("SELECT #{Asset.quoted_table_name}.* FROM #{Asset.quoted_table_name} LEFT JOIN #{Variant.quoted_table_name} ON (#{Variant.quoted_table_name}.id = #{Asset.quoted_table_name}.viewable_id) WHERE (#{Variant.quoted_table_name}.product_id = #{self.id})")
Image.find_by_sql("SELECT #{Asset.quoted_table_name}.* FROM #{Asset.quoted_table_name} LEFT JOIN #{Variant.quoted_table_name} ON (#{Variant.quoted_table_name}.id = #{Asset.quoted_table_name}.viewable_id) WHERE (#{Variant.quoted_table_name}.product_id = #{self.id}) ORDER BY #{Asset.quoted_table_name}.position")
end
alias_method :images, :variant_images
@@ -159,7 +159,7 @@ def duplicate
# don't dup the actual variants, just the characterising types
p.option_types = option_types if has_variants?
# allow site to do some customization
p.send(:duplicate_extra, self) if p.respond_to?(:duplicate_extra)
p.save!
@@ -282,4 +282,17 @@
end
context "#images" do
let(:product) { Factory(:product) }
before do
Factory(:image, :viewable => product, :alt => "position 2", :position => 2)
Factory(:image, :viewable => product, :alt => "position 1", :position => 1)
end
it "should be sorted by position" do
product.images.map(&:alt).should eq(["position 1", "position 2"])
end
end
end

0 comments on commit 5115377

Please sign in to comment.