forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #366 from bonobos/address_book
new AddressBookController
- Loading branch information
Showing
9 changed files
with
340 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Spree | ||
module Api | ||
class AddressBooksController < Spree::Api::BaseController | ||
# Note: the AddressBook is the resource to think about here, not individual addresses | ||
|
||
def show | ||
render_address_book | ||
end | ||
|
||
def update | ||
address_params = address_book_params | ||
default_flag = address_params.delete(:default) | ||
address = current_api_user.save_in_address_book(address_params, default_flag) | ||
if address.valid? | ||
render_address_book | ||
else | ||
invalid_resource!(address) | ||
end | ||
end | ||
|
||
def destroy | ||
current_api_user.remove_from_address_book(params[:address_id]) | ||
render_address_book | ||
end | ||
|
||
private | ||
|
||
def render_address_book | ||
@user_addresses = current_api_user.user_addresses | ||
render :show, status: :ok | ||
end | ||
|
||
def address_book_params | ||
params.require(:address_book).permit(permitted_address_book_attributes) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
collection @user_addresses | ||
node do |user_address| | ||
partial("spree/api/addresses/show", object: user_address.address).merge(default: user_address.default) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
api/spec/controllers/spree/api/address_books_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'spec_helper' | ||
|
||
module Spree | ||
describe Api::AddressBooksController, :type => :controller do | ||
render_views | ||
|
||
context "unauthorized user" do | ||
it "get 401 on /show" do | ||
api_get :show | ||
expect(response.status).to eq 401 | ||
end | ||
|
||
it "get 401 on /update" do | ||
api_put :update | ||
expect(response.status).to eq 401 | ||
end | ||
|
||
it "get 401 on /destroy" do | ||
api_delete :destroy, address_id: 1 | ||
expect(response.status).to eq 401 | ||
end | ||
end | ||
|
||
context "authorized user with addresses" do | ||
let(:address1) { create(:address) } | ||
let(:address2) { create(:address, firstname: "Different") } | ||
|
||
before do | ||
stub_authentication! | ||
current_api_user.save_in_address_book(address1.attributes, true) | ||
current_api_user.save_in_address_book(address2.attributes, false) | ||
end | ||
|
||
it "gets their address book" do | ||
api_get :show | ||
expect(json_response.length).to eq 2 | ||
end | ||
|
||
it "the first one is default" do | ||
api_get :show | ||
first, second = *json_response | ||
expect(first["default"]).to be true | ||
expect(second["default"]).to be false | ||
end | ||
|
||
it "can remove an address" do | ||
api_delete :destroy, address_id: address1.id | ||
expect(json_response.length).to eq 1 | ||
end | ||
|
||
it "can update an address" do | ||
updated_params = address2.attributes | ||
updated_params[:firstname] = "Johnny" | ||
updated_params[:default] = true | ||
api_put :update, address_book: updated_params | ||
expect(json_response.first["firstname"]).to eq "Johnny" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.