Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Add bang method when finding product property #3528

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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