From 5f2572bf82cd371b220987aa610bf30c65b4f8d6 Mon Sep 17 00:00:00 2001 From: "K. Adam White" Date: Thu, 28 Jan 2016 09:45:35 +0000 Subject: [PATCH] Alter .slug() collection filter method to use new ?slug= parameter V2 Beta 11 added support for a slug property (see discussion started in WP-API/WP-API#2065) -- we should use that instead of the `filter[name]` "shim" for WP_Query. --- lib/shared/collection-request.js | 22 ++++++++-------- tests/unit/lib/shared/collection-request.js | 28 +++++++++------------ 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/lib/shared/collection-request.js b/lib/shared/collection-request.js index bba6a068..9dc89e58 100644 --- a/lib/shared/collection-request.js +++ b/lib/shared/collection-request.js @@ -292,27 +292,29 @@ CollectionRequest.prototype.author = function( author ) { }; /** - * Query a collection of posts for a post with a specific slug. + * Query a collection for members with a specific slug. * - * @method name + * @method slug * @chainable - * @param {String} slug A post name (slug), e.g. "hello-world" + * @param {String} slug A post slug (slug), e.g. "hello-world" * @return {CollectionRequest} The CollectionRequest instance (for chaining) */ -CollectionRequest.prototype.name = function( slug ) { - return this.filter( 'name', slug ); +CollectionRequest.prototype.slug = function( slug ) { + return this.param( 'slug', slug ); }; /** - * Alias for `.name()`. + * Alias for .slug() * - * @method slug - * @alias name + * @method name + * @alias slug * @chainable - * @param {String} slug A post slug, e.g. "hello-world" + * @param {String} slug A post name (slug), e.g. "hello-world" * @return {CollectionRequest} The CollectionRequest instance (for chaining) */ -CollectionRequest.prototype.slug = CollectionRequest.prototype.name; +CollectionRequest.prototype.name = function( slug ) { + return this.slug( slug ); +}; /** * Query for posts published in a given year. diff --git a/tests/unit/lib/shared/collection-request.js b/tests/unit/lib/shared/collection-request.js index 39807854..0f4b6ff5 100644 --- a/tests/unit/lib/shared/collection-request.js +++ b/tests/unit/lib/shared/collection-request.js @@ -382,34 +382,30 @@ describe( 'CollectionRequest', function() { }); - describe( 'name()', function() { + describe( 'slug()', function() { - it( 'should set the "name" filter property on the request object', function() { - request.name( 'greatest-post-in-the-world' ); - expect( request._filters.name ).to.equal( 'greatest-post-in-the-world' ); + it( 'should set the "slug" parameter on the request', function() { + request.slug( 'greatest-post-in-the-world' ); + expect( request._renderURI() ).to.equal( '/?slug=greatest-post-in-the-world' ); }); it( 'should be chainable, and replace values', function() { - expect( request.name( 'post-slug-1' ).name( 'hello-world' ) ).to.equal( request ); - expect( request._filters.name ).to.equal( 'hello-world' ); + expect( request.slug( 'post-slug-1' ).slug( 'hello-world' ) ).to.equal( request ); + expect( request._renderURI() ).to.equal( '/?slug=hello-world' ); }); }); - describe( 'slug()', function() { - - it( 'should be an alias for name()', function() { - expect( request.slug ).to.equal( request.name ); - }); + describe( 'name()', function() { - it( 'should set the "name" filter property on the request object', function() { - request.slug( 'greatest-post-in-the-world' ); - expect( request._filters.name ).to.equal( 'greatest-post-in-the-world' ); + it( 'should alias through to set the "slug" parameter on the request', function() { + request.name( 'greatest-post-in-the-world' ); + expect( request._renderURI() ).to.equal( '/?slug=greatest-post-in-the-world' ); }); it( 'should be chainable, and replace values', function() { - expect( request.slug( 'post-slug-1' ).slug( 'hello-world' ) ).to.equal( request ); - expect( request._filters.name ).to.equal( 'hello-world' ); + expect( request.name( 'post-slug-1' ).name( 'hello-world' ) ).to.equal( request ); + expect( request._renderURI() ).to.equal( '/?slug=hello-world' ); }); });