Skip to content

Commit

Permalink
Merge pull request #152 from tightenco/allow-custom-ziggy-in-url-builder
Browse files Browse the repository at this point in the history
Update the route function to allow a third param for custom object.
  • Loading branch information
DanielCoulbourne authored Apr 21, 2018
2 parents 1da970b + 35bdd67 commit d2c8fe9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
18 changes: 9 additions & 9 deletions dist/js/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ var Router = function (_String) {
_inherits(Router, _String);

function Router(name, params, absolute) {
var customZiggy = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;

_classCallCheck(this, Router);

var _this = _possibleConstructorReturn(this, (Router.__proto__ || Object.getPrototypeOf(Router)).call(this));

_this.name = name;
_this.absolute = absolute;
_this.template = _this.name ? new __WEBPACK_IMPORTED_MODULE_0__UrlBuilder__["a" /* default */](name, absolute).construct() : '', _this.urlParams = _this.normalizeParams(params);
_this.ziggy = customZiggy ? customZiggy : Ziggy;
_this.template = _this.name ? new __WEBPACK_IMPORTED_MODULE_0__UrlBuilder__["a" /* default */](name, absolute, _this.ziggy).construct() : '', _this.urlParams = _this.normalizeParams(params);
_this.queryParams = _this.normalizeParams(params);
return _this;
}
Expand Down Expand Up @@ -250,8 +253,8 @@ var Router = function (_String) {
return Router;
}(String);

function route(name, params, absolute) {
return new Router(name, params, absolute);
function route(name, params, absolute, customZiggy) {
return new Router(name, params, absolute, customZiggy);
};

/***/ }),
Expand All @@ -264,15 +267,12 @@ var _createClass = function () { function defineProperties(target, props) { for
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var UrlBuilder = function () {
function UrlBuilder(name, absolute) {
var route = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var customZiggy = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;

function UrlBuilder(name, absolute, ziggyObject) {
_classCallCheck(this, UrlBuilder);

this.name = name;
this.ziggy = customZiggy ? customZiggy : Ziggy;
this.route = route ? route : this.ziggy.namedRoutes[this.name];
this.ziggy = ziggyObject;
this.route = this.ziggy.namedRoutes[this.name];

if (typeof this.name === 'undefined') {
throw new Error('Ziggy Error: You must provide a route name');
Expand Down
2 changes: 1 addition & 1 deletion dist/js/route.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/js/UrlBuilder.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class UrlBuilder {
constructor(name, absolute, route = null, customZiggy = null) {
constructor(name, absolute, ziggyObject) {

this.name = name;
this.ziggy = customZiggy ? customZiggy : Ziggy;
this.route = route ? route : this.ziggy.namedRoutes[this.name];
this.ziggy = ziggyObject;
this.route = this.ziggy.namedRoutes[this.name];

if (typeof this.name === 'undefined') {
throw new Error('Ziggy Error: You must provide a route name');
Expand Down
9 changes: 5 additions & 4 deletions src/js/route.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import UrlBuilder from './UrlBuilder';

class Router extends String {
constructor(name, params, absolute) {
constructor(name, params, absolute, customZiggy=null) {
super();

this.name = name;
this.absolute = absolute;
this.template = this.name ? new UrlBuilder(name, absolute).construct() : '',
this.ziggy = customZiggy ? customZiggy : Ziggy;
this.template = this.name ? new UrlBuilder(name, absolute, this.ziggy).construct() : '',
this.urlParams = this.normalizeParams(params);
this.queryParams = this.normalizeParams(params);
}
Expand Down Expand Up @@ -137,6 +138,6 @@ class Router extends String {
}
}

export default function route(name, params, absolute) {
return new Router(name, params, absolute);
export default function route(name, params, absolute, customZiggy) {
return new Router(name, params, absolute, customZiggy);
};
24 changes: 24 additions & 0 deletions tests/js/test.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,4 +617,28 @@ describe('route()', function() {
global.Ziggy.baseDomain = orgBaseDomain;
global.Ziggy.basePort = orgBasePort;
});

it('Should still work if I pass in a custom Ziggy object', function() {
const customZiggy = {
namedRoutes: {
"tightenDev.packages.index": {
"uri": "tightenDev/{dev}/packages",
"methods": ["GET", "HEAD"],
"domain": null
},
},
baseUrl: 'http://notYourAverage.dev/',
baseProtocol: 'http',
baseDomain: 'notYourAverage.dev',
basePort: false,
defaultParameters: {
locale: "en"
}
};

assert.equal(
'http://notYourAverage.dev/tightenDev/1/packages',
route('tightenDev.packages.index', { dev: 1 }, true, customZiggy).url()
)
});
});

0 comments on commit d2c8fe9

Please sign in to comment.