Change set_variant_attribute to set_variant_attributes, implement it on Order

This commit is contained in:
Rohan Mitchell
2012-08-02 14:45:27 +10:00
parent ea05f05576
commit 2db2fbcade
5 changed files with 24 additions and 4 deletions

View File

@@ -18,9 +18,7 @@ Spree::OrdersController.class_eval do
def populate_variant_attributes
if params.key? :variant_attributes
params[:variant_attributes].each do |variant_id, attributes|
attributes.each do |k, v|
@order.set_variant_attribute(Spree::Variant.find(variant_id), k, v)
end
@order.set_variant_attributes(Spree::Variant.find(variant_id), attributes)
end
end
end

View File

@@ -1,4 +1,6 @@
Spree::LineItem.class_eval do
attr_accessible :max_quantity
def shipping_method
self.product.shipping_method_for_distributor(self.order.distributor)
end

View File

@@ -15,6 +15,13 @@ Spree::Order.class_eval do
can_change_distributor? || product.distributors.include?(distributor)
end
def set_variant_attributes(variant, attributes)
line_item = contains?(variant)
line_item.assign_attributes(attributes)
line_item.save!
end
before_validation :shipping_address_from_distributor

View File

@@ -116,7 +116,7 @@ describe Spree::OrdersController do
p = create(:product, :distributors => [distributor_product], :group_buy => true)
order = current_order(true)
order.should_receive(:set_variant_attribute).with(p.master, 'max_quantity', '3')
order.should_receive(:set_variant_attributes).with(p.master, {'max_quantity' => '3'})
controller.stub(:current_order).and_return(order)
expect do

View File

@@ -46,4 +46,17 @@ describe Spree::Order do
# And cannot be added if it does not match
subject.can_add_product_to_cart?(p_subsequent_other_dist).should be_false
end
it "sets attributes on line items for variants" do
subject.save!
d = create(:distributor)
p = create(:product, :distributors => [d])
subject.distributor = d
subject.add_variant(p.master, 1)
subject.set_variant_attributes(p.master, {'max_quantity' => '3'})
li = Spree::LineItem.last
li.max_quantity.should == 3
end
end