diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index a1d7a54ea5..79c680b67a 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -18,6 +18,10 @@ Spree::Order.class_eval do def set_variant_attributes(variant, attributes) line_item = contains?(variant) + if attributes.key?(:max_quantity) && attributes[:max_quantity].to_i < line_item.quantity + attributes[:max_quantity] = line_item.quantity + end + line_item.assign_attributes(attributes) line_item.save! end diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index fa2dd4275f..1610859e54 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -136,5 +136,23 @@ feature %q{ page.should_not have_selector "#variant_attributes_#{p.master.id}_max_quantity" end + scenario "adding a product with a max quantity less than quantity results in max_quantity==quantity" do + # Given a group buy product and a distributor + d = create(:distributor) + p = create(:product, :distributors => [d], :group_buy => true) + + # When I add the item to my cart + visit spree.product_path p + fill_in "variants_#{p.master.id}", :with => 2 + fill_in "variant_attributes_#{p.master.id}_max_quantity", :with => 1 + click_button 'Add To Cart' + + # Then the item should be in my cart with correct quantities + order = Spree::Order.last + li = order.line_items.first + li.product.should == p + li.quantity.should == 2 + li.max_quantity.should == 2 + end end