mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Add API call to soft-delete a variant
This commit is contained in:
13
app/controllers/spree/api/variants_controller_decorator.rb
Normal file
13
app/controllers/spree/api/variants_controller_decorator.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
Spree::Api::VariantsController.class_eval do
|
||||
def soft_delete
|
||||
@variant = scope.find(params[:id])
|
||||
authorize! :delete, @variant
|
||||
|
||||
@variant.deleted_at = Time.now()
|
||||
if @variant.save
|
||||
respond_with(@variant, :status => 204)
|
||||
else
|
||||
invalid_resource!(@variant)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -110,6 +110,10 @@ Spree::Core::Engine.routes.prepend do
|
||||
get :managed, on: :collection
|
||||
end
|
||||
|
||||
resources :variants do
|
||||
delete :soft_delete
|
||||
end
|
||||
|
||||
resources :orders do
|
||||
get :managed, on: :collection
|
||||
end
|
||||
|
||||
19
lib/spree/api/testing_support/setup.rb
Normal file
19
lib/spree/api/testing_support/setup.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
module Spree
|
||||
module Api
|
||||
module TestingSupport
|
||||
module Setup
|
||||
def sign_in_as_admin!
|
||||
let!(:current_api_user) do
|
||||
user = stub_model(Spree::LegacyUser)
|
||||
user.should_receive(:has_spree_role?).any_number_of_times.with("admin").and_return(true)
|
||||
|
||||
# Stub enterprises, needed for cancan ability checks
|
||||
user.stub(:enterprises) { [] }
|
||||
|
||||
user
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,11 +1,9 @@
|
||||
require 'spec_helper'
|
||||
require 'spree/api/testing_support/helpers'
|
||||
|
||||
module Spree
|
||||
describe Spree::Api::VariantsController do
|
||||
include Spree::Api::TestingSupport::Helpers
|
||||
render_views
|
||||
|
||||
|
||||
let!(:variant1) { FactoryGirl.create(:variant) }
|
||||
let!(:variant2) { FactoryGirl.create(:variant) }
|
||||
let!(:variant3) { FactoryGirl.create(:variant) }
|
||||
@@ -29,6 +27,7 @@ module Spree
|
||||
keys = json_response.keys.map{ |key| key.to_sym }
|
||||
unit_attributes.all?{ |attr| keys.include? attr }.should == true
|
||||
end
|
||||
|
||||
#it "sorts variants in ascending id order" do
|
||||
# spree_get :index, { :template => 'bulk_index', :format => :json }
|
||||
# ids = json_response.map{ |variant| variant['id'] }
|
||||
@@ -36,5 +35,19 @@ module Spree
|
||||
# ids[1].should < ids[2]
|
||||
#end
|
||||
end
|
||||
|
||||
context "as an administrator" do
|
||||
sign_in_as_admin!
|
||||
|
||||
it "soft deletes a variant" do
|
||||
product = create(:product)
|
||||
variant = product.master
|
||||
|
||||
spree_delete :soft_delete, {id: variant.to_param, product_id: product.to_param, format: :json}
|
||||
response.status.should == 204
|
||||
lambda { variant.reload }.should_not raise_error(ActiveRecord::RecordNotFound)
|
||||
variant.deleted_at.should_not be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,6 +23,8 @@ WebMock.disable_net_connect!(:allow_localhost => true)
|
||||
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
|
||||
require 'spree/core/testing_support/controller_requests'
|
||||
require 'spree/core/testing_support/capybara_ext'
|
||||
require 'spree/api/testing_support/setup'
|
||||
require 'spree/api/testing_support/helpers'
|
||||
|
||||
require 'active_record/fixtures'
|
||||
fixtures_dir = File.expand_path('../../db/default', __FILE__)
|
||||
@@ -92,6 +94,8 @@ RSpec.configure do |config|
|
||||
config.include Spree::CheckoutHelpers
|
||||
config.include Spree::Core::TestingSupport::ControllerRequests, :type => :controller
|
||||
config.include Devise::TestHelpers, :type => :controller
|
||||
config.extend Spree::Api::TestingSupport::Setup, :type => :controller
|
||||
config.include Spree::Api::TestingSupport::Helpers, :type => :controller
|
||||
config.include OpenFoodNetwork::FeatureToggleHelper
|
||||
config.include OpenFoodNetwork::EnterpriseGroupsHelper
|
||||
config.include OpenFoodNetwork::DistributionHelper
|
||||
|
||||
Reference in New Issue
Block a user