Skip to content
This repository has been archived by the owner on Jun 27, 2018. It is now read-only.

custom field types based on reference field #74

Open
wants to merge 2 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
7 changes: 5 additions & 2 deletions src/Utils/ReferenceExtractor.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import ReferencedListField from '../Field/ReferencedListField';
import ReferenceField from '../Field/ReferenceField';

export default {

getReferencedLists(fields) {
return this.indexByName(fields.filter(f => f.type() === 'referenced_list'));
return this.indexByName(fields.filter(f => f instanceof ReferencedListField));
},
getReferences(fields, withRemoteComplete, optimized = null) {
let references = fields.filter(f => f.type() === 'reference' || f.type() === 'reference_many');
let references = fields.filter( f => (f instanceof ReferenceField) && !(f instanceof ReferencedListField));
if (withRemoteComplete === true) {
references = references.filter(r => r.remoteComplete());
} else if (withRemoteComplete === false) {
Expand Down
71 changes: 56 additions & 15 deletions tests/lib/Utils/ReferenceExtractorTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@ var assert = require('chai').assert;

import ReferenceExtractor from "../../../lib/Utils/ReferenceExtractor";

import ReferenceField from "../../../lib/Field/ReferenceField";
import ReferenceManyField from "../../../lib/Field/ReferenceManyField";
import ReferencedListField from "../../../lib/Field/ReferencedListField";
import Field from "../../../lib/Field/Field";

class CustomReferenceField extends ReferenceField {
constructor(name) {
super(name);
this._type = 'custom_reference';
}
}

class CustomReferenceManyField extends ReferenceManyField {
constructor(name) {
super(name);
this._type = 'custom_reference_many';
}
}

class CustomReferencedListField extends ReferencedListField {
constructor(name) {
super(name);
this._type = 'custom_referenced_list';
}
}



describe('ReferenceExtractor', () => {

describe('getReferences', () => {
Expand All @@ -11,8 +39,8 @@ describe('ReferenceExtractor', () => {

it('should index by reference name', () => {
const fields = [
{ type() { return 'reference' }, name() { return 'foo' } },
{ type() { return 'reference' }, name() { return 'bar' } }
new ReferenceField('foo'),
new ReferenceField('bar')
];
assert.deepEqual({
foo: fields[0],
Expand All @@ -22,14 +50,19 @@ describe('ReferenceExtractor', () => {

it('should filter out non-reference fields', () => {
const fields = [
{ type() { return 'reference' }, name() { return 'foo' } },
{ type() { return 'reference_many' }, name() { return 'bar' } },
{ type() { return 'referenced_list' }, name() { return 'baz' } },
{ type() { return 'string' }, name() { return 'boo' } }
new ReferenceField('foo'),
new ReferenceManyField('bar'),
new ReferencedListField('baz'),
new Field('boo'),
new CustomReferenceField('cfoo'),
new CustomReferenceManyField('cbar'),
new CustomReferencedListField('cbaz'),
];
assert.deepEqual({
foo: fields[0],
bar: fields[1]
bar: fields[1],
cfoo: fields[4],
cbar: fields[5]
}, ReferenceExtractor.getReferences(fields));
})
})
Expand All @@ -41,24 +74,32 @@ describe('ReferenceExtractor', () => {

it('should index by reference name', () => {
const fields = [
{ type() { return 'referenced_list' }, name() { return 'foo' } },
{ type() { return 'referenced_list' }, name() { return 'bar' } }
new ReferencedListField('foo'),
new ReferencedListField('bar'),
new CustomReferencedListField('baz'),
new CustomReferencedListField('boo')
];
assert.deepEqual({
foo: fields[0],
bar: fields[1]
bar: fields[1],
baz: fields[2],
boo: fields[3],
}, ReferenceExtractor.getReferencedLists(fields));
})

it('should filter out non-referenced_list fields', () => {
const fields = [
{ type() { return 'reference' }, name() { return 'foo' } },
{ type() { return 'reference_many' }, name() { return 'bar' } },
{ type() { return 'referenced_list' }, name() { return 'baz' } },
{ type() { return 'string' }, name() { return 'boo' } }
new ReferenceField('foo'),
new ReferenceManyField('bar'),
new ReferencedListField('baz'),
new Field('boo'),
new CustomReferenceField('cfoo'),
new CustomReferenceManyField('cbar'),
new CustomReferencedListField('cbaz'),
];
assert.deepEqual({
baz: fields[2]
baz: fields[2],
cbaz: fields[6]
}, ReferenceExtractor.getReferencedLists(fields));
})
})
Expand Down