Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path to 1.0 #11

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ indent_style = space
indent_size = 2

[*.hbs]
insert_final_newline = false
indent_style = space
indent_size = 2

Expand Down
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bower_components/
tests/
tmp/
dist/

.bowerrc
.editorconfig
Expand All @@ -9,5 +10,6 @@ tmp/
.npmignore
**/.gitkeep
bower.json
ember-cli-build.js
Brocfile.js
testem.json
21 changes: 20 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
---
language: node_js
node_js:
- "0.12"

sudo: false

cache:
directories:
- node_modules

env:
- EMBER_TRY_SCENARIO=minimum
- EMBER_TRY_SCENARIO=2.0.0
- EMBER_TRY_SCENARIO=2.1.0
- EMBER_TRY_SCENARIO=ember-release
- EMBER_TRY_SCENARIO=ember-beta
- EMBER_TRY_SCENARIO=ember-canary

matrix:
fast_finish: true
allow_failures:
- env: EMBER_TRY_SCENARIO=ember-canary

before_install:
- mkdir travis-phantomjs
- wget https://s3.amazonaws.com/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2 -O $PWD/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2
- tar -xvf $PWD/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2 -C $PWD/travis-phantomjs
- export PATH=$PWD/travis-phantomjs:$PATH
- "npm config set spin false"
- "npm install -g npm@^2"

Expand All @@ -17,4 +36,4 @@ install:
- bower install

script:
- npm test
- ember try $EMBER_TRY_SCENARIO test
3 changes: 3 additions & 0 deletions .watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignore_dirs": ["tmp"]
}
21 changes: 0 additions & 21 deletions Brocfile.js

This file was deleted.

22 changes: 0 additions & 22 deletions LICENSE

This file was deleted.

190 changes: 67 additions & 123 deletions addon/mixins/copyable.js
Original file line number Diff line number Diff line change
@@ -1,135 +1,79 @@
import Ember from 'ember';
import DS from 'ember-data';
const { PromiseObject } = DS;
const { String: { camelize }, get, set, Mixin, typeOf, RSVP } = Ember;

function getCustom(key, custom) {
let value = this.get(key);

return new RSVP.Promise(resolve => {
if (typeOf(custom) === 'function') {
const customValue = custom.bind(this)(value, this);
if (customValue && customValue.then) {
return customValue.then(resolve);
} else {
return resolve(customValue);
}
} else {
return resolve(custom);
}
});
}

function getCopy(key, kind, option) {
let value = this.get(key);

if (!value || !value.then) {
value = RSVP.resolve(value);
}

export default Ember.Mixin.create({
copyable: true,
copy: function(options) {
options = options || {};

var _this = this;
return new Ember.RSVP.Promise(function(resolve) {

var model = _this.constructor;
var copy = _this.get('store').createRecord(model.modelName || model.typeKey);
var queue = [];

model.eachAttribute(function(attr) {
switch(Ember.typeOf(options[attr])) {
case 'undefined':
copy.set(attr, _this.get(attr));
break;
case 'null':
copy.set(attr, null);
break;
default:
copy.set(attr, options[attr]);
}
});

model.eachRelationship(function(relName, meta) {
var rel = _this.get(relName);
if (!rel) { return; }

var overwrite;
var passedOptions = {};
switch(Ember.typeOf(options[relName])) {
case 'null':
return;
case 'instance':
overwrite = options[relName];
break;
case 'object':
passedOptions = options[relName];
break;
case 'array':
overwrite = options[relName];
break;
default:
}

if (rel.constructor === DS.PromiseObject) {

queue.push(rel.then(function(obj) {

if (obj && obj.get('copyable')) {
return obj.copy(passedOptions).then(function(objCopy) {
copy.set(relName, overwrite || objCopy);
});

} else {
copy.set(relName, overwrite || obj);
}

}));


} else if (rel.constructor === DS.PromiseManyArray) {

if (overwrite) {
copy.get(relName).pushObjects(overwrite);
} else {
queue.push(rel.then(function(array) {
var resolvedCopies =
array.map(function(obj) {
if (obj.get('copyable')) {
return obj.copy(passedOptions);
} else {
return obj;
}
});
return Ember.RSVP.all(resolvedCopies).then(function(copies){
copy.get(relName).pushObjects(copies);
});
}));
}
} else {
if (meta.kind === 'belongsTo') {
var obj = rel;

if (obj && obj.get('copyable')) {
queue.push( obj.copy(passedOptions).then(function(objCopy) {
copy.set(relName, overwrite || objCopy);
}));

} else {
copy.set(relName, overwrite || obj);
}

} else {
var objs = rel;

if (objs.get('content')) {
objs = objs.get('content').compact();
}

if (objs.get('firstObject.copyable')) {

var copies = objs.map(function(obj) {
return obj.copy(passedOptions);
});
return new RSVP.Promise(resolve => {
value.then(value => {
if (!value) {
return resolve(value);
}

if (kind === 'hasMany'){
return RSVP.all(value.map(item => item ? copyValue(item, option) : item)).then(resolve);
} else {
return resolve(copyValue(value, option));
}
});
});
}

function copyValue(value, option) {
if (typeOf(value.copy) === 'function') {
return value.copy(option);
} else {
return get(value, 'copy') || value;
}
}

if (overwrite) {
copy.get(relName).pushObjects(overwrite);
} else {
queue.push( Ember.RSVP.all(copies).then( function(resolvedCopies) {
copy.get(relName).pushObjects(resolvedCopies);
}));
}
export default Mixin.create({
copy(options = {}) {

const properties = {};
const addProperties = (_, { name, key = key || name, kind, options: { copy: custom } }) => { // jshint ignore:line
const option = options[key] || custom;

} else {
copy.get(relName).pushObjects(overwrite || objs);
}
}
if (option !== undefined && (kind ? typeOf(option) !== 'object' : true)) {
set(properties, key, getCustom.bind(this)(key, option));
} else {
set(properties, key, getCopy.bind(this)(key, kind, option));
}
};

}
});
this.eachRelationship(addProperties);
this.eachAttribute(addProperties);

const store = this.get('store');
const classKey = camelize(this.get('constructor.modelName'));

Ember.RSVP.all(queue).then(function() {
resolve(copy);
});
return PromiseObject.create({
promise: RSVP.hash(properties).then(properties => {
return store.createRecord(classKey, properties);
})
});
}
});

23 changes: 13 additions & 10 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "ember-cli-copyable",
"name": "ember-copyable",
"dependencies": {
"jquery": "^1.11.1",
"ember": "1.10.0",
"ember-data": "1.0.0-beta.15",
"ember-resolver": "~0.1.12",
"loader.js": "ember-cli/loader.js#3.2.0",
"ember": "2.1.0",
"ember-cli-shims": "ember-cli/ember-cli-shims#0.0.3",
"ember-cli-test-loader": "ember-cli-test-loader#0.1.3",
"ember-load-initializers": "ember-cli/ember-load-initializers#0.0.2",
"ember-qunit": "0.2.8",
"ember-data": "2.1.0",
"ember-load-initializers": "ember-cli/ember-load-initializers#0.1.5",
"ember-qunit": "0.4.9",
"ember-qunit-notifications": "0.0.7",
"qunit": "~1.17.1"
"ember-resolver": "~0.1.18",
"jquery": "^1.11.3",
"loader.js": "ember-cli/loader.js#3.2.1",
"qunit": "~1.18.0"
},
"resolutions": {
"ember": "2.1.0"
}
}
}
Loading