Skip to content

Commit

Permalink
Replace Spree.routes with Spree.pathFor
Browse files Browse the repository at this point in the history
This PR replaces all instances of Spree.routes with Spree.pathFor. Spree.routes still
exists, we're just preferring to use Spree.pathFor per #3405.

It uses Proxy to issue a deprecation warning, however if Proxy isn't defined (in older
browsers like IE) will just return the routes without deprecation. Hopefully the developer
of the app will have something newer than IE, if they don't I imagine they have
bigger problems than JS route deprecations 馃槵

Also adds Proxy to ESlint so it doesn't trigger the no-undef rule.
  • Loading branch information
seand7565 committed May 16, 2020
1 parent afd7f5b commit 63258f3
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 60 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -93,6 +93,7 @@
"Sortable": "readonly",
"Spree": "readonly",
"Turbolinks": "readonly",
"update_state": "writable"
"update_state": "writable",
"Proxy": "readonly"
}
}
Expand Up @@ -6,7 +6,7 @@ Spree.ready(function() {

Spree.ajax({
type: 'POST',
url: Spree.routes.apply_coupon_code(window.order_number),
url: Spree.pathFor('api/orders/' + window.order_number + '/coupon_codes'),
data: {
coupon_code: $("#coupon_code").val(),
token: Spree.api_key
Expand Down
2 changes: 1 addition & 1 deletion backend/app/assets/javascripts/spree/backend/admin.js
Expand Up @@ -131,7 +131,7 @@ Spree.ready(function(){
data: {
token: Spree.api_key
},
url: Spree.routes.checkouts_api + "/" + window.order_number + "/advance"
url: Spree.pathFor('api/checkouts/' + window.order_number + '/advance')
}).done(function() {
window.location.reload();
});
Expand Down
Expand Up @@ -4,7 +4,7 @@ Spree.Collections.States = Backbone.Collection.extend({
},

url: function () {
return Spree.routes.states_search + "?country_id=" + this.country_id
return Spree.pathFor('api/states?country_id=' + this.country_id)
},

parse: function(resp, options) {
Expand Down
6 changes: 3 additions & 3 deletions backend/app/assets/javascripts/spree/backend/models/order.js
Expand Up @@ -4,7 +4,7 @@
//= require spree/backend/models/address

Spree.Models.Order = Backbone.Model.extend({
urlRoot: Spree.routes.orders_api,
urlRoot: Spree.pathFor('api/orders'),
idAttribute: "number",

relations: {
Expand All @@ -16,7 +16,7 @@ Spree.Models.Order = Backbone.Model.extend({

advance: function(opts) {
var options = {
url: Spree.routes.checkouts_api + "/" + this.id + "/advance",
url: Spree.pathFor('api/checkouts/' + this.id + '/advance'),
type: 'PUT',
};
_.extend(options, opts);
Expand All @@ -25,7 +25,7 @@ Spree.Models.Order = Backbone.Model.extend({

empty: function (opts) {
var options = {
url: Spree.routes.orders_api + "/" + this.id + "/empty",
url: Spree.pathFor('api/orders/' + this.id + '/empty'),
type: 'PUT',
};
_.extend(options, opts);
Expand Down
@@ -1,5 +1,5 @@
Spree.Models.Payment = Backbone.Model.extend({
urlRoot: function() {
return Spree.routes.payments_api(this.get('order_id'));
return Spree.pathFor('api/orders/' + this.get('order_id') + '/payments')
}
});
@@ -1,7 +1,7 @@
Spree.Models.Shipment = Backbone.Model.extend({
idAttribute: "number",
paramRoot: "shipment",
urlRoot: Spree.routes.shipments_api,
urlRoot: Spree.pathFor('api/shipments'),

relations: {
"selected_shipping_rate": Backbone.Model,
Expand Down
@@ -1,6 +1,6 @@
Spree.Models.StockItem = Backbone.Model.extend({
paramRoot: 'stock_item',
urlRoot: function() {
return Spree.routes.stock_items_api(this.get('stock_location_id'));
return Spree.pathFor('api/stock_locations/' + this.get('stock_location_id') + '/stock_items');
}
});
Expand Up @@ -11,7 +11,7 @@ Spree.ready(function () {
multiple: true,
initSelection: function (element, callback) {
return Spree.ajax({
url: Spree.routes.option_type_search,
url: Spree.pathFor('api/option_types'),
data: { ids: element.val() },
type: 'get',
success: function(data) {
Expand All @@ -20,7 +20,7 @@ Spree.ready(function () {
});
},
ajax: {
url: Spree.routes.option_type_search,
url: Spree.pathFor('api/option_types'),
quietMillis: 200,
datatype: 'json',
params: { "headers": { 'Authorization': 'Bearer ' + Spree.api_key } },
Expand Down
Expand Up @@ -14,15 +14,15 @@ $.fn.optionValueAutocomplete = function (options) {
minimumInputLength: 3,
multiple: multiple,
initSelection: function (element, callback) {
$.get(Spree.routes.option_value_search, {
$.get(Spree.pathFor('api/option_values'), {
ids: element.val().split(','),
token: Spree.api_key
}, function (data) {
callback(multiple ? data : data[0]);
});
},
ajax: {
url: Spree.routes.option_value_search,
url: Spree.pathFor('api/option_values'),
datatype: 'json',
data: function (term, page) {
// Note: This doesn't work. variants_product_id isn't an allowed filter
Expand Down
Expand Up @@ -13,15 +13,15 @@ $.fn.productAutocomplete = function (options) {
minimumInputLength: 3,
multiple: multiple,
initSelection: function (element, callback) {
$.get(Spree.routes.admin_product_search, {
$.get(Spree.pathFor('admin/search/products'), {
ids: element.val().split(','),
token: Spree.api_key
}, function (data) {
callback(multiple ? data.products : data.products[0]);
});
},
ajax: {
url: Spree.routes.admin_product_search,
url: Spree.pathFor('admin/search/products'),
datatype: 'json',
params: { "headers": { 'Authorization': 'Bearer ' + Spree.api_key } },
data: function (term, page) {
Expand Down
85 changes: 62 additions & 23 deletions backend/app/assets/javascripts/spree/backend/routes.js
@@ -1,27 +1,66 @@
Spree.routes.checkouts_api = Spree.pathFor('api/checkouts')
Spree.routes.classifications_api = Spree.pathFor('api/classifications')
Spree.routes.option_value_search = Spree.pathFor('api/option_values')
Spree.routes.option_type_search = Spree.pathFor('api/option_types')
Spree.routes.orders_api = Spree.pathFor('api/orders')
Spree.routes.product_search = Spree.pathFor('api/products')
Spree.routes.admin_product_search = Spree.pathFor('admin/search/products')
Spree.routes.shipments_api = Spree.pathFor('api/shipments')
Spree.routes.checkouts_api = Spree.pathFor('api/checkouts')
Spree.routes.stock_locations_api = Spree.pathFor('api/stock_locations')
Spree.routes.taxon_products_api = Spree.pathFor('api/taxons/products')
Spree.routes.taxons_search = Spree.pathFor('api/taxons')
Spree.routes.user_search = Spree.pathFor('admin/search/users')
Spree.routes.variants_api = Spree.pathFor('api/variants')
Spree.routes.users_api = Spree.pathFor('api/users')
/**
* @deprecated Spree.routes will be removed in a future release. Please use Spree.pathFor instead.
* See: https://github.com/solidusio/solidus/issues/3405
*/
if(typeof Proxy == "function") {
Spree.routes = new Proxy({
states_search: Spree.pathFor('api/states'),
apply_coupon_code: function(order_id) {
return Spree.pathFor("api/orders/" + order_id + "/coupon_codes");
},
classifications_api: Spree.pathFor('api/classifications'),
option_value_search: Spree.pathFor('api/option_values'),
option_type_search: Spree.pathFor('api/option_types'),
orders_api: Spree.pathFor('api/orders'),
product_search: Spree.pathFor('api/products'),
admin_product_search: Spree.pathFor('admin/search/products'),
shipments_api: Spree.pathFor('api/shipments'),
checkouts_api: Spree.pathFor('api/checkouts'),
stock_locations_api: Spree.pathFor('api/stock_locations'),
taxon_products_api: Spree.pathFor('api/taxons/products'),
taxons_search: Spree.pathFor('api/taxons'),
user_search: Spree.pathFor('admin/search/users'),
variants_api: Spree.pathFor('api/variants'),
users_api: Spree.pathFor('api/users'),

Spree.routes.line_items_api = function(order_id) {
return Spree.pathFor('api/orders/' + order_id + '/line_items')
}
line_items_api: function(order_id) {
return Spree.pathFor('api/orders/' + order_id + '/line_items')
},

Spree.routes.payments_api = function(order_id) {
return Spree.pathFor('api/orders/' + order_id + '/payments')
}
payments_api: function(order_id) {
return Spree.pathFor('api/orders/' + order_id + '/payments')
},

stock_items_api: function(stock_location_id) {
return Spree.pathFor('api/stock_locations/' + stock_location_id + '/stock_items')
}
}, Spree.routesDeprecationProxy);
} else {
Spree.routes.checkouts_api = Spree.pathFor('api/checkouts')
Spree.routes.classifications_api = Spree.pathFor('api/classifications')
Spree.routes.option_value_search = Spree.pathFor('api/option_values')
Spree.routes.option_type_search = Spree.pathFor('api/option_types')
Spree.routes.orders_api = Spree.pathFor('api/orders')
Spree.routes.product_search = Spree.pathFor('api/products')
Spree.routes.admin_product_search = Spree.pathFor('admin/search/products')
Spree.routes.shipments_api = Spree.pathFor('api/shipments')
Spree.routes.checkouts_api = Spree.pathFor('api/checkouts')
Spree.routes.stock_locations_api = Spree.pathFor('api/stock_locations')
Spree.routes.taxon_products_api = Spree.pathFor('api/taxons/products')
Spree.routes.taxons_search = Spree.pathFor('api/taxons')
Spree.routes.user_search = Spree.pathFor('admin/search/users')
Spree.routes.variants_api = Spree.pathFor('api/variants')
Spree.routes.users_api = Spree.pathFor('api/users')

Spree.routes.line_items_api = function(order_id) {
return Spree.pathFor('api/orders/' + order_id + '/line_items')
}

Spree.routes.payments_api = function(order_id) {
return Spree.pathFor('api/orders/' + order_id + '/payments')
}

Spree.routes.stock_items_api = function(stock_location_id) {
return Spree.pathFor('api/stock_locations/' + stock_location_id + '/stock_items')
Spree.routes.stock_items_api = function(stock_location_id) {
return Spree.pathFor('api/stock_locations/' + stock_location_id + '/stock_items')
}
}
10 changes: 5 additions & 5 deletions backend/app/assets/javascripts/spree/backend/shipments.js
Expand Up @@ -11,7 +11,7 @@ var ShipShipmentView = Backbone.View.extend({
onSubmit: function(e){
Spree.ajax({
type: "PUT",
url: Spree.routes.shipments_api + "/" + this.shipment_number + "/ship",
url: Spree.pathFor('api/shipments/' + this.shipment_number + '/ship'),
data: {
send_mailer: this.$("[name='send_mailer']").is(":checked")
},
Expand All @@ -27,7 +27,7 @@ adjustShipmentItems = function(shipment_number, variant_id, quantity){
var shipment = _.findWhere(shipments, {number: shipment_number});
var inventory_units = _.where(shipment.inventory_units, {variant_id: variant_id});

var url = Spree.routes.shipments_api + "/" + shipment_number;
var url = Spree.pathFor('api/shipments/' + shipment_number);

var new_quantity = 0;
if(inventory_units.length<quantity){
Expand Down Expand Up @@ -106,15 +106,15 @@ var ShipmentSplitItemView = Backbone.View.extend({
split_attr.stock_location_id = target_id;
jqXHR = Spree.ajax({
type: "POST",
url: Spree.routes.shipments_api + "/transfer_to_location",
url: Spree.pathFor('api/shipments/transfer_to_location'),
data: split_attr
});
} else if (target_type == 'shipment') {
// transfer to an existing shipment
split_attr.target_shipment_number = target_id;
jqXHR = Spree.ajax({
type: "POST",
url: Spree.routes.shipments_api + "/transfer_to_shipment",
url: Spree.pathFor('api/shipments/transfer_to_shipment'),
data: split_attr
});
} else {
Expand Down Expand Up @@ -179,7 +179,7 @@ var ShipmentItemView = Backbone.View.extend({
var _this = this;
Spree.ajax({
type: "GET",
url: Spree.routes.variants_api + "/" + this.variant_id,
url: Spree.pathFor('api/variants/' + this.variant_id),
}).success(function(variant){
var split = new ShipmentSplitItemView({
shipmentItemView: _this,
Expand Down
Expand Up @@ -10,7 +10,7 @@ $.fn.taxonAutocomplete = function () {

Spree.ajax({
type: "GET",
url: Spree.routes.taxons_search,
url: Spree.pathFor('api/taxons'),
data: {
ids: ids,
per_page: count,
Expand All @@ -22,7 +22,7 @@ $.fn.taxonAutocomplete = function () {
});
},
ajax: {
url: Spree.routes.taxons_search,
url: Spree.pathFor('api/taxons'),
datatype: 'json',
data: function (term, page) {
return {
Expand Down
6 changes: 3 additions & 3 deletions backend/app/assets/javascripts/spree/backend/taxons.js
Expand Up @@ -9,7 +9,7 @@ Spree.ready(function() {
var saveSort = function(e) {
var item = e.item;
Spree.ajax({
url: Spree.routes.classifications_api,
url: Spree.pathFor('api/classifications'),
method: 'PUT',
data: {
product_id: item.getAttribute('data-product-id'),
Expand All @@ -27,7 +27,7 @@ Spree.ready(function() {
dropdownCssClass: "taxon_select_box",
placeholder: Spree.translations.find_a_taxon,
ajax: {
url: Spree.routes.taxons_search,
url: Spree.pathFor('api/taxons'),
params: {
"headers": {
'Authorization': 'Bearer ' + Spree.api_key
Expand Down Expand Up @@ -55,7 +55,7 @@ Spree.ready(function() {

$('#taxon_id').on("change", function(e) {
Spree.ajax({
url: Spree.routes.taxon_products_api,
url: Spree.pathFor('api/taxons/products'),
data: {
id: e.val,
simple: 1
Expand Down
4 changes: 2 additions & 2 deletions backend/app/assets/javascripts/spree/backend/user_picker.js
Expand Up @@ -10,7 +10,7 @@ $.fn.userAutocomplete = function () {
multiple: true,
initSelection: function (element, callback) {
Spree.ajax({
url: Spree.routes.users_api,
url: Spree.pathFor('api/users'),
data: {
ids: element.val()
},
Expand All @@ -20,7 +20,7 @@ $.fn.userAutocomplete = function () {
});
},
ajax: {
url: Spree.routes.users_api,
url: Spree.pathFor('api/users'),
datatype: 'json',
params: { "headers": { 'Authorization': 'Bearer ' + Spree.api_key } },
data: function (term) {
Expand Down
Expand Up @@ -16,12 +16,12 @@
minimumInputLength: 3,
initSelection: function(element, callback) {
Spree.ajax({
url: Spree.routes.variants_api + "/" + element.val(),
url: Spree.pathFor('api/variants/' + element.val()),
success: callback
});
},
ajax: {
url: Spree.routes.variants_api,
url: Spree.pathFor('api/variants'),
datatype: "json",
quietMillis: 500,
params: {
Expand Down
Expand Up @@ -26,7 +26,7 @@ Spree.Views.Order.CustomerSelect = Backbone.View.extend({
this.$el.select2({
placeholder: Spree.translations.choose_a_customer,
ajax: {
url: Spree.routes.users_api,
url: Spree.pathFor('api/users'),
params: { "headers": { 'Authorization': 'Bearer ' + Spree.api_key } },
datatype: 'json',
data: function(term, page) {
Expand Down

0 comments on commit 63258f3

Please sign in to comment.