Skip to content

Commit

Permalink
Merge pull request #3528 from nebulab/SamuelMartini/product_property_api
Browse files Browse the repository at this point in the history
[API] Add bang method when finding product property
  • Loading branch information
kennyadsl authored Mar 6, 2020
2 parents 5bd31ac + 4b66f49 commit c165e2d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
17 changes: 6 additions & 11 deletions api/app/controllers/spree/api/product_properties_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,18 @@ def create
end

def update
if @product_property
authorize! :update, @product_property
@product_property.update(product_property_params)
authorize! :update, @product_property
if @product_property.update(product_property_params)
respond_with(@product_property, status: 200, default_template: :show)
else
invalid_resource!(@product_property)
end
end

def destroy
if @product_property
authorize! :destroy, @product_property
@product_property.destroy
respond_with(@product_property, status: 204)
else
invalid_resource!(@product_property)
end
authorize! :destroy, @product_property
@product_property.destroy
respond_with(@product_property, status: 204)
end

private
Expand All @@ -65,7 +60,7 @@ def find_product
def product_property
if @product
@product_property ||= @product.product_properties.find_by(id: params[:id])
@product_property ||= @product.product_properties.includes(:property).where(spree_properties: { name: params[:id] }).first
@product_property ||= @product.product_properties.includes(:property).where(spree_properties: { name: params[:id] }).first!
authorize! :read, @product_property
end
end
Expand Down
12 changes: 12 additions & 0 deletions api/spec/requests/spree/api/product_properties_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ module Spree
expect(response.status).to eq(204)
expect { property_1.reload }.to raise_error(ActiveRecord::RecordNotFound)
end

context 'when product property does not exist' do
it 'cannot update because is not found' do
put spree.api_product_product_property_path(product, 'no property'), params: { product_property: { value: "my value 456" } }
expect(response.status).to eq(404)
end

it 'cannot delete because is not found' do
delete spree.api_product_product_property_path(product, 'no property')
expect(response.status).to eq(404)
end
end
end

context "with product identified by id" do
Expand Down

0 comments on commit c165e2d

Please sign in to comment.