diff --git a/app/models/order.rb b/app/models/order.rb index 146503a..25153d4 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -41,10 +41,12 @@ class Order < ActiveRecord::Base end def create_api_job + return if Rails.env.test? + priority = 0 run_at = Rails.application.config.call_api_after.from_now job = TabApiJob.new(id) - Delayed::Job.enqueue job, priority, run_at + Delayed::Job.enqueue job, priority: priority, run_at: run_at end end diff --git a/app/models/order_item.rb b/app/models/order_item.rb index 3a82226..4561fa7 100644 --- a/app/models/order_item.rb +++ b/app/models/order_item.rb @@ -14,7 +14,7 @@ class OrderItem < ActiveRecord::Base validates :product, presence: true validates :count, presence: true, numericality: { only_integer: true, - less_than_or_equal_to: ->(oi) { oi.product.stock }, + less_than_or_equal_to: ->(oi) { oi.product.try(:stock) || 100 }, greater_than_or_equal_to: 0 } before_destroy :put_back_in_stock! diff --git a/config/routes.rb b/config/routes.rb index d2f106d..d56ffdd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,7 +18,7 @@ Rails.application.routes.draw do end end - resources :users, only: [:show, :edit, :update, :destroy] do + resources :users, only: [:show, :edit, :update, :index, :destroy] do resources :orders, only: [:new, :create, :destroy] member do get 'quickpay' => 'users#quickpay' diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 2bf6a64..a54a545 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -30,8 +30,8 @@ require 'identicon' FactoryGirl.define do factory :user do - uid { Faker::Name.name } - avatar { Identicon.data_url_for uid } + name { Faker::Name.name } + avatar { Identicon.data_url_for name } factory :admin do admin true diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 16cf917..a42e40e 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -20,7 +20,9 @@ describe User do it{ should be_able_to(:read, Product.new) } it{ should_not be_able_to(:manage, Product.new) } - it{ should be_able_to(:manage, Order.new(user: user)) } + it{ should be_able_to(:create, Order.new(user: user)) } + it{ should be_able_to(:delete, Order.new(user: user, created_at: 2.minutes.ago)) } + it{ should_not be_able_to(:delete, Order.new(user: user, created_at: 10.minutes.ago)) } it{ should_not be_able_to(:manage, Order.new) } it{ should_not be_able_to(:manage, Stock.new) } diff --git a/spec/models/order_item_spec.rb b/spec/models/order_item_spec.rb index bdad4fe..33e0931 100644 --- a/spec/models/order_item_spec.rb +++ b/spec/models/order_item_spec.rb @@ -34,11 +34,6 @@ describe OrderItem do @order_item.count = -5 expect(@order_item).to_not be_valid end - - it 'should not be 0' do - @order_item.count = 0 - expect(@order_item).to_not be_valid - end end end @@ -50,12 +45,12 @@ describe OrderItem do end it 'should decrement on create' do - expect{@order_item.save}.to change{@product.stock}.by(-@count) + expect{ @order_item.save }.to change{ @product.stock }.by(-@count) end it 'should increment on cancel' do @order_item.save - expect{@order_item.cancel}.to change{@product.stock}.by(@count) + expect{ @order_item.destroy }.to change{ @product.stock }.by(@count) end end end diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 951b01b..380541f 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -20,32 +20,6 @@ describe Order do expect(@order).to be_valid end - describe 'cancelling' do - it 'should cancel the order' do - @order.cancel - expect(@order.cancelled).to be true - end - - it 'should not happen twice' do - @order.cancel - expect(@order.cancel).to be false - end - - it 'should not work on old orders' do - order = create :order, created_at: 3.days.ago - expect(order.cancel).to be false - end - - it 'should change the orders_count' do - expect{@order.cancel}.to change{@user.reload.orders_count}.by(-1) - end - - it 'should cancel the orderitems' do - expect(@order.order_items.first).to receive(:cancel) - @order.cancel - end - end - describe 'price' do it 'should be calculated from order_items' do @order = build :order, products_count: 0 @@ -54,6 +28,7 @@ describe Order do @order.order_items << oi end end.map{ |oi| oi.count * oi.product.price_cents }.sum + @order.valid? expect(@order.price_cents).to eq(sum) end end