├── app ├── .gitkeep └── initializers │ └── ember-data-slug.js ├── addon ├── .gitkeep ├── index.js ├── initializers │ └── ember-data-slug.js └── mixins │ ├── adapter-slug-support.js │ └── store-slug-support.js ├── vendor └── .gitkeep ├── tests ├── unit │ ├── .gitkeep │ └── mixins │ │ ├── adapter-slug-support-test.js │ │ └── store-slug-support-test.js ├── integration │ ├── .gitkeep │ └── slug-test.js ├── dummy │ ├── app │ │ ├── helpers │ │ │ └── .gitkeep │ │ ├── models │ │ │ ├── .gitkeep │ │ │ └── my-model.js │ │ ├── routes │ │ │ └── .gitkeep │ │ ├── styles │ │ │ └── app.css │ │ ├── components │ │ │ └── .gitkeep │ │ ├── controllers │ │ │ └── .gitkeep │ │ ├── templates │ │ │ └── components │ │ │ │ └── .gitkeep │ │ ├── resolver.js │ │ ├── adapters │ │ │ └── application.js │ │ ├── router.js │ │ ├── app.js │ │ └── index.html │ ├── public │ │ ├── robots.txt │ │ └── crossdomain.xml │ └── config │ │ └── environment.js ├── .eslintrc.js ├── test-helper.js ├── helpers │ ├── destroy-app.js │ ├── resolver.js │ ├── start-app.js │ └── module-for-acceptance.js ├── .jshintrc └── index.html ├── .watchmanconfig ├── .bowerrc ├── index.js ├── config ├── environment.js └── ember-try.js ├── .eslintrc.js ├── bower.json ├── .npmignore ├── testem.js ├── .ember-cli ├── .gitignore ├── .editorconfig ├── ember-cli-build.js ├── .travis.yml ├── LICENSE.md ├── README.md └── package.json /app/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /addon/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/unit/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/integration/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/helpers/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/models/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/routes/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/styles/app.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/components/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/controllers/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.watchmanconfig: -------------------------------------------------------------------------------- 1 | { 2 | "ignore_dirs": ["tmp", "dist"] 3 | } 4 | -------------------------------------------------------------------------------- /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "bower_components", 3 | "analytics": false 4 | } 5 | -------------------------------------------------------------------------------- /tests/dummy/public/robots.txt: -------------------------------------------------------------------------------- 1 | # http://www.robotstxt.org 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /tests/dummy/app/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from 'ember-resolver'; 2 | 3 | export default Resolver; 4 | -------------------------------------------------------------------------------- /app/initializers/ember-data-slug.js: -------------------------------------------------------------------------------- 1 | export { default, initialize } from 'ember-data-slug/initializers/ember-data-slug'; 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | 'use strict'; 3 | 4 | module.exports = { 5 | name: 'ember-data-slug' 6 | }; 7 | -------------------------------------------------------------------------------- /tests/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: '../node_modules/ember-cli-eslint/coding-standard/ember-testing.js' 3 | }; 4 | -------------------------------------------------------------------------------- /tests/dummy/app/models/my-model.js: -------------------------------------------------------------------------------- 1 | import DS from 'ember-data'; 2 | 3 | const { Model } = DS; 4 | 5 | export default Model.extend(); 6 | -------------------------------------------------------------------------------- /config/environment.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | 'use strict'; 3 | 4 | module.exports = function(/* environment, appConfig */) { 5 | return { }; 6 | }; 7 | -------------------------------------------------------------------------------- /tests/test-helper.js: -------------------------------------------------------------------------------- 1 | import resolver from './helpers/resolver'; 2 | import { 3 | setResolver 4 | } from 'ember-qunit'; 5 | 6 | setResolver(resolver); 7 | -------------------------------------------------------------------------------- /tests/dummy/app/adapters/application.js: -------------------------------------------------------------------------------- 1 | import DS from 'ember-data'; 2 | 3 | const { JSONAPIAdapter } = DS; 4 | 5 | export default JSONAPIAdapter.extend(); 6 | -------------------------------------------------------------------------------- /tests/helpers/destroy-app.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | 3 | export default function destroyApp(application) { 4 | Ember.run(application, 'destroy'); 5 | } 6 | -------------------------------------------------------------------------------- /addon/index.js: -------------------------------------------------------------------------------- 1 | export { default as StoreSlugSupport } from './mixins/store-slug-support'; 2 | export { default as AdapterSlugSupport } from './mixins/adapter-slug-support'; 3 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | 3 | module.exports = { 4 | extends: [ 5 | require.resolve('ember-cli-eslint/coding-standard/ember-application.js') 6 | ] 7 | }; 8 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-data-slug", 3 | "dependencies": { 4 | "ember": "~2.7.0", 5 | "ember-cli-shims": "0.1.1", 6 | "ember-qunit-notifications": "0.1.0", 7 | "pretender": "^0.12.0" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /bower_components 2 | /config/ember-try.js 3 | /dist 4 | /tests 5 | /tmp 6 | **/.gitkeep 7 | .bowerrc 8 | .editorconfig 9 | .ember-cli 10 | .gitignore 11 | .jshintrc 12 | .watchmanconfig 13 | .travis.yml 14 | bower.json 15 | ember-cli-build.js 16 | testem.js 17 | -------------------------------------------------------------------------------- /testem.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | module.exports = { 3 | "framework": "qunit", 4 | "test_page": "tests/index.html?hidepassed", 5 | "disable_watching": true, 6 | "launch_in_ci": [ 7 | "PhantomJS" 8 | ], 9 | "launch_in_dev": [ 10 | "PhantomJS", 11 | "Chrome" 12 | ] 13 | }; 14 | -------------------------------------------------------------------------------- /tests/dummy/app/router.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import config from './config/environment'; 3 | 4 | const Router = Ember.Router.extend({ 5 | location: config.locationType, 6 | rootURL: config.rootURL 7 | }); 8 | 9 | Router.map(function() { 10 | }); 11 | 12 | export default Router; 13 | -------------------------------------------------------------------------------- /.ember-cli: -------------------------------------------------------------------------------- 1 | { 2 | /** 3 | Ember CLI sends analytics information by default. The data is completely 4 | anonymous, but there are times when you might want to disable this behavior. 5 | 6 | Setting `disableAnalytics` to true will prevent any data from being sent. 7 | */ 8 | "disableAnalytics": false 9 | } 10 | -------------------------------------------------------------------------------- /tests/helpers/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from '../../resolver'; 2 | import config from '../../config/environment'; 3 | 4 | const resolver = Resolver.create(); 5 | 6 | resolver.namespace = { 7 | modulePrefix: config.modulePrefix, 8 | podModulePrefix: config.podModulePrefix 9 | }; 10 | 11 | export default resolver; 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | 7 | # dependencies 8 | /node_modules 9 | /bower_components 10 | 11 | # misc 12 | /.sass-cache 13 | /connect.lock 14 | /coverage/* 15 | /libpeerconnection.log 16 | npm-debug.log 17 | testem.log 18 | -------------------------------------------------------------------------------- /addon/initializers/ember-data-slug.js: -------------------------------------------------------------------------------- 1 | import DS from 'ember-data'; 2 | import { AdapterSlugSupport, StoreSlugSupport } from 'ember-data-slug'; 3 | 4 | const { Store, RESTAdapter } = DS; 5 | 6 | export function initialize(/* application */) { 7 | Store.reopen(StoreSlugSupport); 8 | RESTAdapter.reopen(AdapterSlugSupport); 9 | } 10 | 11 | export default { 12 | name: 'ember-data-slug', 13 | initialize 14 | }; 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | indent_style = space 14 | indent_size = 2 15 | 16 | [*.hbs] 17 | insert_final_newline = false 18 | 19 | [*.{diff,md}] 20 | trim_trailing_whitespace = false 21 | -------------------------------------------------------------------------------- /tests/dummy/app/app.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import Resolver from './resolver'; 3 | import loadInitializers from 'ember-load-initializers'; 4 | import config from './config/environment'; 5 | 6 | let App; 7 | 8 | Ember.MODEL_FACTORY_INJECTIONS = true; 9 | 10 | App = Ember.Application.extend({ 11 | modulePrefix: config.modulePrefix, 12 | podModulePrefix: config.podModulePrefix, 13 | Resolver 14 | }); 15 | 16 | loadInitializers(App, config.modulePrefix); 17 | 18 | export default App; 19 | -------------------------------------------------------------------------------- /tests/helpers/start-app.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import Application from '../../app'; 3 | import config from '../../config/environment'; 4 | 5 | export default function startApp(attrs) { 6 | let application; 7 | 8 | let attributes = Ember.merge({}, config.APP); 9 | attributes = Ember.merge(attributes, attrs); // use defaults, but you can override; 10 | 11 | Ember.run(() => { 12 | application = Application.create(attributes); 13 | application.setupForTesting(); 14 | application.injectTestHelpers(); 15 | }); 16 | 17 | return application; 18 | } 19 | -------------------------------------------------------------------------------- /ember-cli-build.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | /* global require, module */ 3 | var EmberAddon = require('ember-cli/lib/broccoli/ember-addon'); 4 | 5 | module.exports = function(defaults) { 6 | var app = new EmberAddon(defaults, { 7 | // Add options here 8 | babel: { 9 | includePolyfill: true 10 | } 11 | }); 12 | 13 | /* 14 | This build file specifies the options for the dummy test app of this 15 | addon, located in `/tests/dummy` 16 | This build file does *not* influence how the addon or the app using it 17 | behave. You most likely want to be modifying `./index.js` or app's build file 18 | */ 19 | 20 | return app.toTree(); 21 | }; 22 | -------------------------------------------------------------------------------- /tests/dummy/public/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 15 | 16 | -------------------------------------------------------------------------------- /tests/helpers/module-for-acceptance.js: -------------------------------------------------------------------------------- 1 | import { module } from 'qunit'; 2 | import Ember from 'ember'; 3 | import startApp from '../helpers/start-app'; 4 | import destroyApp from '../helpers/destroy-app'; 5 | 6 | const { RSVP: { Promise } } = Ember; 7 | 8 | export default function(name, options = {}) { 9 | module(name, { 10 | beforeEach() { 11 | this.application = startApp(); 12 | 13 | if (options.beforeEach) { 14 | return options.beforeEach.apply(this, arguments); 15 | } 16 | }, 17 | 18 | afterEach() { 19 | let afterEach = options.afterEach && options.afterEach.apply(this, arguments); 20 | return Promise.resolve(afterEach).then(() => destroyApp(this.application)); 21 | } 22 | }); 23 | } 24 | -------------------------------------------------------------------------------- /tests/dummy/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | 12 | 13 | 14 | 15 | {{content-for "head-footer"}} 16 | 17 | 18 | {{content-for "body"}} 19 | 20 | 21 | 22 | 23 | {{content-for "body-footer"}} 24 | 25 | 26 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: node_js 3 | node_js: 4 | - "4" 5 | 6 | sudo: false 7 | 8 | cache: 9 | directories: 10 | - node_modules 11 | 12 | env: 13 | - EMBER_TRY_SCENARIO=default 14 | - EMBER_TRY_SCENARIO=ember-data-2.x 15 | - EMBER_TRY_SCENARIO=ember-data-1.13 16 | 17 | matrix: 18 | fast_finish: true 19 | 20 | before_install: 21 | - npm config set spin false 22 | - npm install -g bower 23 | - bower --version 24 | - npm install phantomjs-prebuilt 25 | - phantomjs --version 26 | 27 | install: 28 | - npm install 29 | - bower install 30 | 31 | script: 32 | # Usually, it's ok to finish the test scenario without reverting 33 | # to the addon's original dependency state, skipping "cleanup". 34 | - ember try:one $EMBER_TRY_SCENARIO test --skip-cleanup 35 | -------------------------------------------------------------------------------- /config/ember-try.js: -------------------------------------------------------------------------------- 1 | /*jshint node:true*/ 2 | module.exports = { 3 | scenarios: [ 4 | { 5 | name: 'default', 6 | bower: { 7 | dependencies: { } 8 | } 9 | }, 10 | { 11 | name: 'ember-data-2.x', 12 | npm: { 13 | devDpendencies: { 14 | 'ember-data': '~2.0' 15 | } 16 | } 17 | }, 18 | { 19 | name: 'ember-data-1.13', 20 | bower: { 21 | dependencies: { 22 | "ember": "~1.13", 23 | "ember-data": "~1.13", 24 | "ember-cli-shims": "0.0.6" 25 | }, 26 | resolutions: { 27 | 'ember': '~1.13', 28 | 'ember-data': '~1.13' 29 | } 30 | }, 31 | npm: { 32 | devDpendencies: { 33 | 'ember-data': '~1.13' 34 | } 35 | } 36 | } 37 | ] 38 | }; 39 | -------------------------------------------------------------------------------- /addon/mixins/adapter-slug-support.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | 3 | const { Mixin } = Ember; 4 | 5 | export default Mixin.create({ 6 | 7 | urlForQueryRecord(query, modelName) { 8 | let { __ember_data_slug } = query; 9 | 10 | if (__ember_data_slug) { 11 | return this.urlForFindRecord(__ember_data_slug, modelName); 12 | } 13 | 14 | return this._super(...arguments); 15 | }, 16 | 17 | // sortQueryParams needs to be used, as there is no other way to modify query 18 | // params. There will be once `ds-improved-ajax` is enabled, wich will allow 19 | // us to overwrite dataForRequest instead 20 | sortQueryParams(queryParams) { 21 | let { __ember_data_slug } = queryParams; 22 | 23 | if (__ember_data_slug) { 24 | delete queryParams.__ember_data_slug; 25 | } 26 | 27 | return this._super(...arguments); 28 | } 29 | 30 | }); 31 | -------------------------------------------------------------------------------- /tests/.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "predef": [ 3 | "document", 4 | "window", 5 | "location", 6 | "setTimeout", 7 | "$", 8 | "-Promise", 9 | "define", 10 | "console", 11 | "visit", 12 | "exists", 13 | "fillIn", 14 | "click", 15 | "keyEvent", 16 | "triggerEvent", 17 | "find", 18 | "findWithAssert", 19 | "wait", 20 | "DS", 21 | "andThen", 22 | "currentURL", 23 | "currentPath", 24 | "currentRouteName" 25 | ], 26 | "node": false, 27 | "browser": false, 28 | "boss": true, 29 | "curly": true, 30 | "debug": false, 31 | "devel": false, 32 | "eqeqeq": true, 33 | "evil": true, 34 | "forin": false, 35 | "immed": false, 36 | "laxbreak": false, 37 | "newcap": true, 38 | "noarg": true, 39 | "noempty": false, 40 | "nonew": false, 41 | "nomen": false, 42 | "onevar": false, 43 | "plusplus": false, 44 | "regexp": false, 45 | "undef": true, 46 | "sub": true, 47 | "strict": false, 48 | "white": false, 49 | "eqnull": true, 50 | "esversion": 6, 51 | "unused": true 52 | } 53 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ember-data-slug Tests 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | {{content-for "test-head"}} 12 | 13 | 14 | 15 | 16 | 17 | {{content-for "head-footer"}} 18 | {{content-for "test-head-footer"}} 19 | 20 | 21 | {{content-for "body"}} 22 | {{content-for "test-body"}} 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | {{content-for "body-footer"}} 31 | {{content-for "test-body-footer"}} 32 | 33 | 34 | -------------------------------------------------------------------------------- /tests/dummy/config/environment.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | 3 | module.exports = function(environment) { 4 | var ENV = { 5 | modulePrefix: 'dummy', 6 | environment: environment, 7 | rootURL: '/', 8 | locationType: 'auto', 9 | EmberENV: { 10 | FEATURES: { 11 | // Here you can enable experimental features on an ember canary build 12 | // e.g. 'with-controller': true 13 | } 14 | }, 15 | 16 | APP: { 17 | // Here you can pass flags/options to your application instance 18 | // when it is created 19 | } 20 | }; 21 | 22 | if (environment === 'development') { 23 | // ENV.APP.LOG_RESOLVER = true; 24 | // ENV.APP.LOG_ACTIVE_GENERATION = true; 25 | // ENV.APP.LOG_TRANSITIONS = true; 26 | // ENV.APP.LOG_TRANSITIONS_INTERNAL = true; 27 | // ENV.APP.LOG_VIEW_LOOKUPS = true; 28 | } 29 | 30 | if (environment === 'test') { 31 | // Testem prefers this... 32 | ENV.locationType = 'none'; 33 | 34 | // keep test console output quieter 35 | ENV.APP.LOG_ACTIVE_GENERATION = false; 36 | ENV.APP.LOG_VIEW_LOOKUPS = false; 37 | 38 | ENV.APP.rootElement = '#ember-testing'; 39 | } 40 | 41 | if (environment === 'production') { 42 | 43 | } 44 | 45 | return ENV; 46 | }; 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ember-data-slug [![Build Status](https://travis-ci.org/pangratz/ember-data-slug.svg?branch=master)](https://travis-ci.org/pangratz/ember-data-slug) [![Ember Observer Score](https://emberobserver.com/badges/ember-data-slug.svg)](https://emberobserver.com/addons/ember-data-slug) 2 | 3 | Slug support for `store.findRecord`. 4 | 5 | If your API supports to reference a model via slug additionally to the id, then 6 | this addon allows you to use `findRecord` with both the slug or id: 7 | 8 | ```js 9 | // GET /videos/best-dance-ever 10 | let findViaSlug = store.findRecord("video", "best-dance-ever"); 11 | 12 | // GET /videos/1 13 | let findViaId = store.findRecord("video", "1"); 14 | 15 | // both requests return the same response 16 | // { 17 | // id: "1", 18 | // slug: "best-dancing-ever", 19 | // url: "https://www.youtube.com/watch?v=1TphEh0Qgv0" 20 | // } 21 | ``` 22 | 23 | This addon ensures that the same records are referenced within Ember Data: 24 | 25 | ```js 26 | Ember.RSVP.all([ findViaSlug, findViaId ]).then(function([ foundViaSlug, foundViaId ]) { 27 | // true 28 | foundViaSlug === foundViaId; 29 | }); 30 | ``` 31 | 32 | Install via: 33 | 34 | ``` 35 | ember install ember-data-slug 36 | ``` 37 | 38 | # Development 39 | 40 | ## Installation 41 | 42 | * `git clone` this repository 43 | * `npm install` 44 | * `bower install` 45 | 46 | ## Running 47 | 48 | * `ember serve` 49 | * Visit your app at http://localhost:4200. 50 | 51 | ## Running Tests 52 | 53 | * `npm test` (Runs `ember try:testall` to test your addon against multiple Ember versions) 54 | * `ember test` 55 | * `ember test --server` 56 | 57 | ## Building 58 | 59 | * `ember build` 60 | 61 | For more information on using ember-cli, visit [http://ember-cli.com/](http://ember-cli.com/). 62 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-data-slug", 3 | "version": "0.0.2", 4 | "description": "slug support for store.findRecord", 5 | "directories": { 6 | "doc": "doc", 7 | "test": "tests" 8 | }, 9 | "scripts": { 10 | "build": "ember build", 11 | "start": "ember server", 12 | "test": "ember try:each" 13 | }, 14 | "repository": "https://github.com/pangratz/ember-data-slug", 15 | "engines": { 16 | "node": ">= 0.10.0" 17 | }, 18 | "author": "Clemens Müller ", 19 | "license": "MIT", 20 | "devDependencies": { 21 | "broccoli-asset-rev": "^2.4.2", 22 | "ember-ajax": "^2.0.1", 23 | "ember-cli": "2.7.0", 24 | "ember-cli-app-version": "^1.0.0", 25 | "ember-cli-dependency-checker": "^1.2.0", 26 | "ember-cli-eslint": "1.7.0", 27 | "ember-cli-htmlbars": "^1.0.3", 28 | "ember-cli-htmlbars-inline-precompile": "^0.3.1", 29 | "ember-cli-inject-live-reload": "^1.4.0", 30 | "ember-cli-pretender": "0.6.0", 31 | "ember-cli-qunit": "^2.0.0", 32 | "ember-cli-release": "1.0.0-beta.2", 33 | "ember-cli-sri": "^2.1.0", 34 | "ember-cli-test-loader": "^1.1.0", 35 | "ember-cli-uglify": "^1.2.0", 36 | "ember-data": "^2.7.0", 37 | "ember-disable-prototype-extensions": "^1.1.0", 38 | "ember-export-application-global": "^1.0.5", 39 | "ember-load-initializers": "^0.5.1", 40 | "ember-resolver": "^2.0.3", 41 | "ember-welcome-page": "^1.0.1", 42 | "loader.js": "^4.0.1" 43 | }, 44 | "keywords": [ 45 | "ember-addon" 46 | ], 47 | "dependencies": { 48 | "ember-cli-babel": "^5.1.6" 49 | }, 50 | "ember-addon": { 51 | "after": [ 52 | "ember-data" 53 | ], 54 | "configPath": "tests/dummy/config", 55 | "versionCompatibility": { 56 | "ember-data": ">=1.13.0" 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /addon/mixins/store-slug-support.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | 3 | const { Map, MapWithDefault, Mixin, get } = Ember; 4 | 5 | export default Mixin.create({ 6 | 7 | init() { 8 | this._super(...arguments); 9 | 10 | this._typeSlugCache = MapWithDefault.create({ 11 | defaultValue: function() { 12 | return Map.create(); 13 | } 14 | }); 15 | }, 16 | 17 | willDestroy() { 18 | this._super(...arguments); 19 | 20 | this._typeSlugCache.clear(); 21 | delete this._typeSlugCache; 22 | }, 23 | 24 | findRecord(modelName, idOrSlug, options) { 25 | const slugCache = this._typeSlugCache.get(modelName); 26 | 27 | let coercedIdOrSlug = `${idOrSlug}`; 28 | 29 | let id = slugCache.get(coercedIdOrSlug); 30 | if (id) { 31 | return this._super(modelName, id, options); 32 | } 33 | 34 | // now we know we're dealing with a slug 35 | let slug = coercedIdOrSlug; 36 | 37 | // get the passed query without reload and backgroundReload, since those 38 | // are not supported by queryRecord and would end up as query params 39 | let { reload, backgroundReload, ...query } = options || {}; // eslint-disable-line no-unused-vars 40 | 41 | // add the slug to the query, so the adapter can use it to build the URL 42 | // for queryRecord 43 | query.__ember_data_slug = slug; 44 | 45 | return this.queryRecord(modelName, query).then((foundRecord) => { 46 | id = get(foundRecord, 'id'); 47 | 48 | // inform cache about slug -> id mapping 49 | slugCache.set(slug, id); 50 | 51 | // setup id -> id mapping, so the original findRecord behaviour is 52 | // maintained: once a record found via the slug is in the store and the 53 | // record is requested again via store.findRecord("modelName", id) if we 54 | // don't setup the id -> id mapping here, then the above `if (id)` path 55 | // is never taken and queryRecord is always used 56 | slugCache.set(id, id); 57 | 58 | return foundRecord; 59 | }); 60 | } 61 | 62 | }); 63 | -------------------------------------------------------------------------------- /tests/unit/mixins/adapter-slug-support-test.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import AdapterSlugSupportMixin from 'ember-data-slug/mixins/adapter-slug-support'; 3 | import { module, test } from 'qunit'; 4 | 5 | module('Unit | Mixin | adapter slug support'); 6 | 7 | test('urlForQueryRecord calls urlForFindRecord', function(assert) { 8 | let AdapterSlugSupportObject = Ember.Object.extend(AdapterSlugSupportMixin); 9 | let subject = AdapterSlugSupportObject.create({ 10 | urlForFindRecord(id, modelName) { 11 | assert.equal(id, 'the-slug'); 12 | assert.equal(modelName, 'my-model'); 13 | } 14 | }); 15 | 16 | let query = { 17 | __ember_data_slug: 'the-slug' 18 | }; 19 | 20 | subject.urlForQueryRecord(query, 'my-model'); 21 | }); 22 | 23 | test('urlForQueryRecord calls _super when not invoked for ember-data-slug', function(assert) { 24 | let AdapterSlugSupportObject = Ember.Object.extend({ 25 | urlForQueryRecord(query, modelName) { 26 | assert.deepEqual(query, { 27 | slug: 'the-slug' 28 | }); 29 | assert.equal(modelName, 'my-model'); 30 | } 31 | }).extend(AdapterSlugSupportMixin); 32 | 33 | let subject = AdapterSlugSupportObject.create(); 34 | 35 | let query = { 36 | slug: 'the-slug' 37 | }; 38 | 39 | subject.urlForQueryRecord(query, 'my-model'); 40 | }); 41 | 42 | test('sortQueryParams removes properties when invoked for ember-data-slug', function(assert) { 43 | let AdapterSlugSupportObject = Ember.Object.extend(AdapterSlugSupportMixin); 44 | let subject = AdapterSlugSupportObject.create(); 45 | 46 | let queryParams = { 47 | __ember_data_slug: '123', 48 | other: true 49 | }; 50 | 51 | subject.sortQueryParams(queryParams); 52 | 53 | assert.deepEqual(queryParams, { other: true }); 54 | }); 55 | 56 | test('sortQueryParams ignores properties if not invoked for ember-data-slug', function(assert) { 57 | let AdapterSlugSupportObject = Ember.Object.extend(AdapterSlugSupportMixin); 58 | let subject = AdapterSlugSupportObject.create(); 59 | 60 | let queryParams = { 61 | slug: 'the-slug' 62 | }; 63 | 64 | subject.sortQueryParams(queryParams); 65 | 66 | assert.deepEqual(queryParams, { slug: 'the-slug' }); 67 | }); 68 | -------------------------------------------------------------------------------- /tests/integration/slug-test.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import DS from 'ember-data'; 3 | import Pretender from 'pretender'; 4 | import { moduleFor, test } from 'ember-qunit'; 5 | import startApp from '../helpers/start-app'; 6 | import destroyApp from '../helpers/destroy-app'; 7 | 8 | const { run } = Ember; 9 | const { AdapterError } = DS; 10 | 11 | function json(data) { 12 | return [200, {}, JSON.stringify(data)]; 13 | } 14 | 15 | moduleFor('service:store', 'ember-data slug suport for findRecord', { 16 | integration: true, 17 | 18 | beforeEach() { 19 | this.application = startApp(); 20 | 21 | this.server = new Pretender(); 22 | 23 | this.store = this.subject(); 24 | 25 | this.findRecord = (...args) => { 26 | return run(this.store, 'findRecord', ...args); 27 | } 28 | }, 29 | 30 | afterEach() { 31 | this.server.shutdown(); 32 | 33 | destroyApp(this.application); 34 | } 35 | }); 36 | 37 | test('record can be requested via slug', async function(assert) { 38 | this.server.get('/my-models/the-slug', function() { 39 | return json({ 40 | data: { 41 | type: 'my-model', 42 | id: 'the-id' 43 | } 44 | }); 45 | }); 46 | 47 | let foundViaSlug = await this.findRecord('my-model', 'the-slug'); 48 | assert.equal(foundViaSlug.get('id'), 'the-id'); 49 | 50 | this.server.get('/my-models/the-id', function() { 51 | return json({ 52 | data: { 53 | type: 'my-model', 54 | id: 'the-id' 55 | } 56 | }); 57 | }); 58 | 59 | let foundViaId = await this.findRecord('my-model', 'the-id', { reload: true }); 60 | assert.equal(foundViaId.get('id'), 'the-id'); 61 | 62 | assert.deepEqual(foundViaSlug, foundViaId); 63 | }); 64 | 65 | test('no record for the slug model is created', async function(assert) { 66 | this.server.get('/my-models/the-slug', function() { 67 | return json({ 68 | data: { 69 | type: 'my-model', 70 | id: 'the-id' 71 | } 72 | }); 73 | }); 74 | 75 | await this.findRecord('my-model', 'the-slug'); 76 | 77 | let slugRecord = this.store.peekRecord('my-model', 'the-slug'); 78 | assert.notOk(slugRecord); 79 | }); 80 | 81 | test('not found slug throws AdapterError', async function(assert) { 82 | this.server.get('/my-models/the-slug', function() { 83 | return [404, {}, null]; 84 | }); 85 | 86 | try { 87 | await this.findRecord('my-model', 'the-slug'); 88 | } catch (error) { 89 | assert.ok(error instanceof AdapterError); 90 | } 91 | }); 92 | 93 | test('not found slug rejects promise', async function(assert) { 94 | this.server.get('/my-models/the-slug', function() { 95 | return [404, {}, null]; 96 | }); 97 | 98 | await this.findRecord('my-model', 'the-slug').then(function() { 99 | throw "error"; 100 | }, function(error) { 101 | assert.ok(error instanceof AdapterError); 102 | }); 103 | }); 104 | 105 | test('store.findRecord(modelName, slug) calls store.queryRecord', async function(assert) { 106 | this.server.get('/my-models/the-slug', function() { 107 | return json({ 108 | data: { 109 | type: 'my-model', 110 | id: 'the-id' 111 | } 112 | }); 113 | }); 114 | 115 | this.store.reopen({ 116 | queryRecord(modelName, query) { 117 | assert.equal(modelName, 'my-model'); 118 | assert.deepEqual(query, { 119 | __ember_data_slug: 'the-slug' 120 | }); 121 | 122 | return this._super(...arguments); 123 | } 124 | }); 125 | 126 | let foundViaSlug = await this.findRecord('my-model', 'the-slug'); 127 | assert.ok(foundViaSlug); 128 | }); 129 | -------------------------------------------------------------------------------- /tests/unit/mixins/store-slug-support-test.js: -------------------------------------------------------------------------------- 1 | import Ember from 'ember'; 2 | import StoreSlugSupportMixin from 'ember-data-slug/mixins/store-slug-support'; 3 | import { module, test } from 'qunit'; 4 | 5 | const { RSVP } = Ember; 6 | 7 | let Store; 8 | let queryRecordCalled, queryRecordModelName, queryRecordQuery; 9 | let findRecordCalled, findRecordModelName, findRecordId, findRecordOptions; 10 | 11 | module('Unit | Mixin | store slug support', { 12 | beforeEach() { 13 | queryRecordCalled = 0; 14 | findRecordCalled = 0; 15 | 16 | Store = Ember.Object.extend({ 17 | findRecord(modelName, id, options) { 18 | findRecordCalled++; 19 | findRecordModelName = modelName; 20 | findRecordId = id; 21 | findRecordOptions = options; 22 | 23 | return RSVP.resolve({ 24 | id: '123' 25 | }); 26 | }, 27 | 28 | queryRecord(modelName, query) { 29 | queryRecordCalled++; 30 | queryRecordModelName = modelName; 31 | queryRecordQuery = query; 32 | 33 | return RSVP.resolve({ 34 | id: '123' 35 | }); 36 | } 37 | }).extend(StoreSlugSupportMixin); 38 | }, 39 | 40 | afterEach() { 41 | Store = null; 42 | } 43 | }); 44 | 45 | test('makes first request via queryRecord, subsequent via findRecord', async function(assert) { 46 | let store = Store.create(); 47 | 48 | await store.findRecord('my-model', 'the-slug'); 49 | 50 | assert.equal(findRecordCalled, 0); 51 | assert.equal(queryRecordCalled, 1); 52 | assert.equal(queryRecordModelName, 'my-model'); 53 | assert.deepEqual(queryRecordQuery, { __ember_data_slug: 'the-slug' }); 54 | 55 | await store.findRecord('my-model', '123'); 56 | 57 | assert.equal(queryRecordCalled, 1); 58 | assert.equal(findRecordCalled, 1); 59 | assert.equal(findRecordModelName, 'my-model'); 60 | assert.equal(findRecordId, '123'); 61 | assert.equal(findRecordOptions, undefined); 62 | 63 | await store.findRecord('my-model', '123'); 64 | 65 | assert.equal(queryRecordCalled, 1); 66 | assert.equal(findRecordCalled, 2); 67 | assert.equal(findRecordModelName, 'my-model'); 68 | assert.equal(findRecordId, '123'); 69 | assert.equal(findRecordOptions, undefined); 70 | }); 71 | 72 | test('coerces passed slug/id into string', async function(assert) { 73 | let store = Store.create(); 74 | 75 | await store.findRecord('my-model', 999); 76 | 77 | assert.equal(queryRecordCalled, 1); 78 | assert.equal(findRecordCalled, 0); 79 | assert.equal(queryRecordModelName, 'my-model'); 80 | 81 | await store.findRecord('my-model', 123); 82 | 83 | assert.equal(queryRecordCalled, 1); 84 | assert.equal(findRecordCalled, 1); 85 | assert.equal(findRecordId, '123'); 86 | 87 | await store.findRecord('my-model', '123'); 88 | 89 | assert.equal(queryRecordCalled, 1); 90 | assert.equal(findRecordCalled, 2); 91 | assert.equal(findRecordId, '123'); 92 | }); 93 | 94 | test('passes through the options to _super#findRecord', async function(assert) { 95 | let store = Store.create(); 96 | 97 | // simulate findRecord already being invoked 98 | await store.findRecord('my-model', '123'); 99 | 100 | await store.findRecord('my-model', '123', { 101 | myCustomOptions: true 102 | }); 103 | 104 | assert.deepEqual(findRecordOptions, { 105 | myCustomOptions: true 106 | }); 107 | }); 108 | 109 | test('passes correct query to queryRecord', async function(assert) { 110 | let store = Store.create(); 111 | 112 | await store.findRecord('my-model', 999, { 113 | include: 'other-model', 114 | otherParam: true, 115 | adapterOptions: { 116 | hello: 'world' 117 | } 118 | }); 119 | 120 | assert.deepEqual(queryRecordQuery, { 121 | __ember_data_slug: '999', 122 | adapterOptions: { 123 | hello: 'world' 124 | }, 125 | include: 'other-model', 126 | otherParam: true 127 | }); 128 | }); 129 | 130 | test('works with different models', async function(assert) { 131 | let store = Store.create(); 132 | 133 | await store.findRecord('my-first-model', 1); 134 | 135 | assert.equal(queryRecordCalled, 1); 136 | assert.equal(findRecordCalled, 0); 137 | assert.equal(queryRecordModelName, 'my-first-model'); 138 | 139 | await store.findRecord('my-second-model', 1); 140 | 141 | assert.equal(queryRecordCalled, 2); 142 | assert.equal(findRecordCalled, 0); 143 | assert.equal(queryRecordModelName, 'my-second-model'); 144 | 145 | await store.findRecord('my-first-model', 1); 146 | 147 | assert.equal(queryRecordCalled, 2); 148 | assert.equal(findRecordCalled, 1); 149 | assert.equal(findRecordModelName, 'my-first-model'); 150 | 151 | await store.findRecord('my-second-model', 1); 152 | 153 | assert.equal(queryRecordCalled, 2); 154 | assert.equal(findRecordCalled, 2); 155 | assert.equal(findRecordModelName, 'my-second-model'); 156 | }); 157 | --------------------------------------------------------------------------------