Skip to content

Commit

Permalink
Merge pull request #45 from bonobos/rails-4-2-upgrade-master
Browse files Browse the repository at this point in the history
Upgrade to Rails 4.2
  • Loading branch information
jhawthorn committed Jun 2, 2015
2 parents a3763e7 + 31352da commit 6ab7422
Show file tree
Hide file tree
Showing 86 changed files with 263 additions and 247 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ before_install:
- cd $GEM; export BUNDLE_GEMFILE="`pwd`/Gemfile"
script:
- bundle exec rake test_app
- RSPEC_RETRY_COUNT=2 bundle exec rake spec
- bundle exec rake spec
rvm:
- 2.1.5
4 changes: 2 additions & 2 deletions api/app/controllers/spree/api/checkouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def next
authorize! :update, @order, order_token
@order.next!
respond_with(@order, default_template: 'spree/api/orders/show', status: 200)
rescue StateMachine::InvalidTransition
rescue StateMachines::InvalidTransition
respond_with(@order, default_template: 'spree/api/orders/could_not_transition', status: 422)
end

Expand All @@ -44,7 +44,7 @@ def complete
@order.complete!
respond_with(@order, default_template: 'spree/api/orders/show', status: 200)
end
rescue StateMachine::InvalidTransition
rescue StateMachines::InvalidTransition
respond_with(@order, default_template: 'spree/api/orders/could_not_transition', status: 422)
end

Expand Down
3 changes: 2 additions & 1 deletion api/app/controllers/spree/api/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class ProductsController < Spree::Api::BaseController

def index
if params[:ids]
@products = product_scope.where(:id => params[:ids].split(","))
ids = params[:ids].split(",").flatten
@products = product_scope.where(:id => ids)
else
@products = product_scope.ransack(params[:q]).result
end
Expand Down
3 changes: 2 additions & 1 deletion api/app/controllers/spree/api/properties_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def index
@properties = Spree::Property.accessible_by(current_ability, :read)

if params[:ids]
@properties = @properties.where(:id => params[:ids].split(","))
ids = params[:ids].split(",").flatten
@properties = @properties.where(:id => ids)
else
@properties = @properties.ransack(params[:q]).result
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def create
private

def stock_location
render 'spree/api/shared/stock_location_required', status: 422 and return unless params[:stock_location_id]
@stock_location ||= StockLocation.accessible_by(current_ability, :read).find(params[:stock_location_id])
end

Expand Down
8 changes: 6 additions & 2 deletions api/app/controllers/spree/api/zones_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ZonesController < Spree::Api::BaseController

def create
authorize! :create, Zone
@zone = Zone.new(map_nested_attributes_keys(Spree::Zone, params[:zone]))
@zone = Zone.new(map_nested_attributes_keys(Spree::Zone, zone_params))
if @zone.save
respond_with(@zone, :status => 201, :default_template => :show)
else
Expand All @@ -29,7 +29,7 @@ def show

def update
authorize! :update, zone
if zone.update_attributes(map_nested_attributes_keys(Spree::Zone, params[:zone]))
if zone.update_attributes(map_nested_attributes_keys(Spree::Zone, zone_params))
respond_with(zone, :status => 200, :default_template => :show)
else
invalid_resource!(zone)
Expand All @@ -38,6 +38,10 @@ def update

private

def zone_params
params.require(:zone).permit!
end

def zone
@zone ||= Spree::Zone.accessible_by(current_ability, :read).find(params[:id])
end
Expand Down
5 changes: 4 additions & 1 deletion api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@
end
end

resources :variants, only: [:index, :show] do
resources :variants do
resources :images
end

resources :option_types do
resources :option_values
end

resources :option_values, only: :index
get '/orders/mine', to: 'orders#mine', as: 'my_orders'
get "/orders/current", to: "orders#current", as: "current_order"

Expand Down Expand Up @@ -113,6 +114,8 @@
resources :stock_items
end

resources :stock_items, only: [:index, :update, :destroy]

resources :stores

resources :store_credit_events, only: [] do
Expand Down
6 changes: 3 additions & 3 deletions api/lib/spree/api/responders/rabl_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def to_format
super
end

rescue ActionView::MissingTemplate => e
api_behavior(e)
rescue ActionView::MissingTemplate
api_behavior
end

def template
request.headers['X-Spree-Template'] || controller.params[:template] || options[:default_template]
end

def api_behavior(error)
def api_behavior
if controller.params[:action] == "destroy"
# Render a blank template
super
Expand Down
4 changes: 2 additions & 2 deletions api/spec/controllers/spree/api/base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def index

context 'without an existing lock' do
it 'succeeds' do
api_get :index, order_token: order.token, order_id: order.number
api_get :index, order_token: order.guest_token, order_id: order.number
response.status.should == 200
end
end
Expand All @@ -146,7 +146,7 @@ def index
end

it 'returns a 409 conflict' do
api_get :index, order_token: order.token, order_id: order.number
api_get :index, order_token: order.guest_token, order_id: order.number
response.status.should == 409
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ module Spree

it "returns errors when source is required and missing" do
order.update_column(:state, "payment")
api_put :update, :id => order.to_param, :order_token => order.token,
api_put :update, :id => order.to_param, :order_token => order.guest_token,
:order => { :payments_attributes => [{ :payment_method_id => @payment_method.id }] }
response.status.should == 422
source_errors = json_response['errors']['payments.source']
Expand Down
14 changes: 4 additions & 10 deletions api/spec/controllers/spree/api/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
module Spree
describe Api::OrdersController, :type => :controller do
render_views

let!(:order) { create(:order) }
let(:variant) { create(:variant) }
let(:line_item) { create(:line_item) }
Expand Down Expand Up @@ -305,11 +304,6 @@ module Spree
assert_unauthorized!
end

it "cannot add address information to an order that doesn't belong to them" do
api_put :address, :id => order.to_param
assert_unauthorized!
end

it "can create an order" do
api_post :create, :order => { :line_items => { "0" => { :variant_id => variant.to_param, :quantity => 5 } } }
expect(response.status).to eq(201)
Expand Down Expand Up @@ -597,8 +591,8 @@ def clean_address(address)
expect(source[:name]).to eq payment.source.name
expect(source[:cc_type]).to eq payment.source.cc_type
expect(source[:last_digits]).to eq payment.source.last_digits
expect(source[:month].to_i).to eq payment.source.month
expect(source[:year].to_i).to eq payment.source.year
expect(source[:month]).to eq payment.source.month
expect(source[:year]).to eq payment.source.year
expect(source.has_key?(:gateway_customer_profile_id)).to be false
expect(source.has_key?(:gateway_payment_profile_id)).to be false
end
Expand Down Expand Up @@ -708,8 +702,8 @@ def clean_address(address)
expect(source[:name]).to eq payment.source.name
expect(source[:cc_type]).to eq payment.source.cc_type
expect(source[:last_digits]).to eq payment.source.last_digits
expect(source[:month].to_i).to eq payment.source.month
expect(source[:year].to_i).to eq payment.source.year
expect(source[:month]).to eq payment.source.month
expect(source[:year]).to eq payment.source.year
expect(source[:gateway_customer_profile_id]).to eq payment.source.gateway_customer_profile_id
expect(source[:gateway_payment_profile_id]).to eq payment.source.gateway_payment_profile_id
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module Spree
end

it "cannot delete a product property" do
api_delete :destroy, :property_name => property_1.property_name
api_delete :destroy, id: property_1.property_name
assert_unauthorized!
expect { property_1.reload }.not_to raise_error
end
Expand Down
10 changes: 10 additions & 0 deletions api/spec/controllers/spree/api/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,23 @@ def custom_show
end
end

def set_custom_route
@routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
r.draw { get 'custom_show' => 'spree/api/products#custom_show' }
end
end

it "uses the specified custom template through the request header" do
set_custom_route

request.headers['X-Spree-Template'] = 'show'
api_get :custom_show, :id => product.id
expect(response).to render_template('spree/api/products/show')
end

it "uses the specified custom template through the template URL parameter" do
set_custom_route

api_get :custom_show, :id => product.id, :template => 'show'
expect(response).to render_template('spree/api/products/show')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ module Spree
it_behaves_like "a return authorization creator"

it "cannot update a return authorization" do
api_put :update
api_put :update, id: 0
assert_not_found!
end

it "cannot delete a return authorization" do
api_delete :destroy
api_delete :destroy, id: 0
assert_not_found!
end
end
Expand Down
4 changes: 2 additions & 2 deletions api/spec/controllers/spree/api/stock_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ module Spree

describe "#update" do
it "cannot update a stock item" do
api_put :update, stock_location_id: stock_location.to_param, stock_item_id: stock_item.to_param
api_put :update, stock_location_id: stock_location.to_param, id: stock_item.to_param
response.status.should == 404
end
end

describe "#destroy" do
it "cannot destroy a stock item" do
api_delete :destroy, stock_location_id: stock_location.to_param, stock_item_id: stock_item.to_param
api_delete :destroy, stock_location_id: stock_location.to_param, id: stock_item.to_param
response.status.should == 404
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ module Spree
expect(json_response['stock_movements'].first['stock_item']['count_on_hand']).to eq 11
end

it 'requires a stock_location_id to be passed as a parameter' do
api_get :index
expect(json_response['error']).to match(/stock_location_id parameter must be provided/)
expect(response.status).to eq(422)
end

it 'can control the page size through a parameter' do
create(:stock_movement, stock_item: stock_item)
api_get :index, stock_location_id: stock_location.to_param, per_page: 1
Expand Down
4 changes: 4 additions & 0 deletions api/spec/controllers/spree/api/zones_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def custom_show
end

it "uses the specified template" do
@routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
r.draw { get 'custom_show' => 'spree/api/zones#custom_show' }
end

request.headers['X-Spree-Template'] = 'show'
api_get :custom_show, :id => @zone.id
expect(response).to render_template('spree/api/zones/show')
Expand Down
16 changes: 8 additions & 8 deletions api/spec/features/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def parsed

def login
expect {
post 'api/users', user: { email: "[email protected]", password: "featurecheckoutuser" }
post '/api/users', user: { email: "[email protected]", password: "featurecheckoutuser" }
}.to change { Spree.user_class.count }.by 1
expect(response).to have_http_status(:created)
@user = Spree.user_class.find(parsed['id'])
Expand All @@ -33,27 +33,27 @@ def login
end

def create_order(order_params: {})
expect { post 'api/orders', order_params }.to change { Order.count }.by 1
expect { post '/api/orders', order_params }.to change { Order.count }.by 1
expect(response).to have_http_status(:created)
@order = Order.find(parsed['id'])
expect(@order.email).to eq "[email protected]"
end

def update_order(order_params: {})
put "api/orders/#{@order.number}", order_params
put "/api/orders/#{@order.number}", order_params
expect(response).to have_http_status(:ok)
end

def create_line_item(variant, quantity = 1)
expect {
post "api/orders/#{@order.number}/line_items", line_item: { variant_id: variant.id, quantity: quantity }
post "/api/orders/#{@order.number}/line_items", line_item: { variant_id: variant.id, quantity: quantity }
}.to change { @order.line_items.count }.by 1
expect(response).to have_http_status(:created)
end

def add_promotion(promotion)
expect {
put "api/orders/#{@order.number}/apply_coupon_code", coupon_code: promotion_code.value
put "/api/orders/#{@order.number}/apply_coupon_code", coupon_code: promotion_code.value
}.to change { @order.promotions.count }.by 1
expect(response).to have_http_status(:ok)
end
Expand All @@ -69,19 +69,19 @@ def add_address(address, billing: true)

def add_payment
expect {
post "api/orders/#{@order.number}/payments", payment: { payment_method_id: payment_method.id }
post "/api/orders/#{@order.number}/payments", payment: { payment_method_id: payment_method.id }
}.to change { @order.reload.payments.count }.by 1
expect(response).to have_http_status(:created)
expect(@order.payments.last.payment_method).to eq payment_method
end

def advance
put "api/checkouts/#{@order.number}/advance"
put "/api/checkouts/#{@order.number}/advance"
expect(response).to have_http_status(:ok)
end

def complete
put "api/checkouts/#{@order.number}/complete"
put "/api/checkouts/#{@order.number}/complete"
expect(response).to have_http_status(:ok)
end

Expand Down
1 change: 1 addition & 0 deletions api/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}

require 'spree/testing_support/factories'
require 'spree/testing_support/rspec-activemodel-mocks_patch'
require 'spree/testing_support/preferences'

require 'spree/api/testing_support/caching'
Expand Down
9 changes: 8 additions & 1 deletion api/spec/support/controller_hacks.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
require 'active_support/all'

module ControllerHacks
extend ActiveSupport::Concern

included do
routes { Spree::Core::Engine.routes }
end

def api_get(action, params={}, session=nil, flash=nil)
api_process(action, params, session, flash, "GET")
end
Expand All @@ -22,7 +29,7 @@ def api_delete(action, params={}, session=nil, flash=nil)

def api_process(action, params={}, session=nil, flash=nil, method="get")
scoping = respond_to?(:resource_scoping) ? resource_scoping : {}
process(action, method, params.merge(scoping).reverse_merge!(:use_route => :spree, :format => :json), session, flash)
process(action, method, params.merge(scoping).reverse_merge!(format: :json), session, flash)
end
end

Expand Down
4 changes: 2 additions & 2 deletions backend/app/controllers/spree/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def complete
@order.complete!
flash[:success] = Spree.t(:order_completed)
redirect_to edit_admin_order_url(@order)
rescue StateMachine::InvalidTransition => e
rescue StateMachines::InvalidTransition => e
flash[:error] = e.message
redirect_to confirm_admin_order_url(@order)
end
Expand All @@ -140,7 +140,7 @@ def approve
end

def resend
OrderMailer.confirm_email(@order.id, true).deliver
OrderMailer.confirm_email(@order.id, true).deliver_now
flash[:success] = Spree.t(:order_email_resent)

redirect_to :back
Expand Down
Loading

0 comments on commit 6ab7422

Please sign in to comment.