Skip to content

Commit

Permalink
Product#images are sorted by their position [Fixes #1446]
Browse files Browse the repository at this point in the history
  • Loading branch information
joahking authored and radar committed Apr 26, 2012
1 parent f2e9cc3 commit 5115377
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand Down
13 changes: 13 additions & 0 deletions core/spec/models/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.