Skip to content

Commit

Permalink
skipping validation for delivery date on admin section
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmt committed Mar 31, 2014
1 parent a795fce commit 0a71e60
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def edit

def update
@order = Order.find_by(number: params[:order_id])
if @order.update_attributes(delivery_options_params) && @order.next
if update_delivery_options(@order) && @order.next
flash[:success] = Spree.t('delivery_options_updated')
end

Expand All @@ -18,6 +18,15 @@ def update

private

def update_delivery_options(order)
order_params = delivery_options_params
if order_params[:delivery_date]
@order.delivery_date = order_params.delete(:delivery_date)
@order.save(validate: false)
end
@order.update_attributes(order_params)
end

def delivery_options_params
params.require(:order).permit(:delivery_date, :delivery_time, :delivery_instructions)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def delivery_date_present?
end

def valid_delivery_date?
if self.delivery_date
if self.delivery_date && self.delivery_date_changed?
self.errors[:delivery_date] << 'cannot be today or in the past' if self.delivery_date <= Date.current

options = delivery_time_options(self.delivery_date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
before :each do
Spree::Order.stub(:find_by).and_return(order)
order.stub(:update_attributes).and_return(true)
order.stub(:save)
end

it 'should update order' do
order.should_receive(:update_attributes).with({"delivery_date" => tomorrow})

it 'should update delivery date and skip validation' do
order.should_receive(:delivery_date=).with(tomorrow)
order.should_receive(:save).with(validate: false)
spree_post :update, order: {delivery_date: tomorrow}
end

it 'should call next if update is successful' do
order.should_receive(:update_attributes).with({"delivery_date" => tomorrow}).and_return(true)
it 'should update other attributes and call next if update is successful' do
order.should_receive(:update_attributes).with({"delivery_time" => '7:30am to 9am'}).and_return(true)
order.should_receive(:next)
spree_post :update, order: {delivery_date: tomorrow}
spree_post :update, order: {delivery_date: tomorrow, delivery_time: '7:30am to 9am'}
end

it 'should render edit when successful' do
Expand All @@ -46,14 +47,14 @@
end

it 'should render edit when unsuccessful' do
order.should_receive(:update_attributes).with({"delivery_date" => tomorrow}).and_return false
spree_post :update, order: {delivery_date: tomorrow}
order.should_receive(:update_attributes).with({"delivery_time" => '7:30am to 9am'}).and_return false
spree_post :update, order: {delivery_date: tomorrow, delivery_time: '7:30am to 9am'}
response.should render_template(:edit)
end

it 'should not allow to update invalid attributes' do
order.should_receive(:update_attributes).with({"delivery_date" => tomorrow})
spree_post :update, order: {delivery_date: tomorrow, crazy: 'blah'}
order.should_receive(:update_attributes).with({"delivery_time" => '7:30am to 9am'})
spree_post :update, order: {delivery_date: tomorrow, delivery_time: '7:30am to 9am', crazy: 'blah'}
end

end
Expand Down
1 change: 0 additions & 1 deletion spec/models/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
SpreeDeliveryOptions::Config.delivery_time_options = {monday: ['Between 6-7am']}.to_json
end


it 'should not be valid if delivery date is in the past' do
order.delivery_date = Date.yesterday
order.valid_delivery_date?.should == false
Expand Down

0 comments on commit 0a71e60

Please sign in to comment.