Skip to content

Commit

Permalink
rename "collection" to "schema"
Browse files Browse the repository at this point in the history
The idea here is that "schema" is easier to say, but also in the future
where we put "schema" can be either the _name_ of a schema or an actual
schema (useful for Form plugins and Controllers).

This commit leaves all the tests largely as they were, triggering all
the deprecation warnings as a test. The next commit will remove all the
deprecation triggers in the tests, as well as make the deprecation
warnings slightly less annoying...

Refs #25
  • Loading branch information
preaction committed May 16, 2019
1 parent b02bd5b commit 99df7fe
Show file tree
Hide file tree
Showing 35 changed files with 704 additions and 625 deletions.
256 changes: 131 additions & 125 deletions lib/Mojolicious/Plugin/Yancy.pm

Large diffs are not rendered by default.

113 changes: 55 additions & 58 deletions lib/Mojolicious/Plugin/Yancy/resources/public/yancy/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ var app = new Vue({
data: function () {
var current = this.parseHash();
return {
hasCollections: null,
currentCollection: current.collection || null,
collections: {},
hasSchema: null,
currentSchemaName: current.schema || null,
schema: {},
openedRow: null,
deleteIndex: null,
addingItem: false,
Expand Down Expand Up @@ -300,8 +300,8 @@ var app = new Vue({
this.fetchPage();
},

setCollection: function ( name ) {
this.currentCollection = name;
setSchema: function ( name ) {
this.currentSchemaName = name;
$( '#sidebar-collapse' ).collapse('hide');
},

Expand All @@ -316,9 +316,9 @@ var app = new Vue({
},

parseSpec: function ( spec ) {
var pathParts = [], collectionName, collection, pathObj, firstCollection;
this.collections = {};
this.hasCollections = false;
var pathParts = [], schemaName, schema, pathObj, firstSchema;
this.schema = {};
this.hasSchema = false;

// Preprocess definitions
for ( var defKey in spec.definitions ) {
Expand Down Expand Up @@ -372,74 +372,73 @@ var app = new Vue({
pathObj = spec.paths[ pathKey ];

pathParts = pathKey.split( '/' );
collectionName = pathParts[1];
schemaName = pathParts[1];

// Skip hidden collections
if ( spec.definitions[ collectionName ]['x-hidden'] ) {
// Skip hidden schemas
if ( spec.definitions[ schemaName ]['x-hidden'] ) {
continue;
}

collection = this.collections[ collectionName ];
if ( !collection ) {
collection = this.collections[ collectionName ] = {
schema = this.schema[ schemaName ];
if ( !schema ) {
schema = this.schema[ schemaName ] = {
operations: { }
};
}
if ( !firstCollection ) {
firstCollection = collectionName;
if ( !firstSchema ) {
firstSchema = schemaName;
}
this.hasCollections = true;
this.hasSchema = true;

// Array operations
if ( pathParts.length == 2 ) {
if ( pathObj.get ) {
collection.operations["list"] = {
schema.operations["list"] = {
url: [ spec.basePath, pathKey ].join(''),
schema: spec.definitions[ collectionName ]
schema: spec.definitions[ schemaName ]
};
}
if ( pathObj.post ) {
collection.operations["add"] = {
schema.operations["add"] = {
url: [ spec.basePath, pathKey ].join(''),
schema: spec.definitions[ collectionName ]
schema: spec.definitions[ schemaName ]
};
}
}
// Item operations
else {
if ( pathObj.get ) {
collection.operations["get"] = {
schema.operations["get"] = {
url: [ spec.basePath, pathKey ].join(''),
schema: spec.definitions[ collectionName ]
schema: spec.definitions[ schemaName ]
};
}
if ( pathObj.put ) {
collection.operations["set"] = {
schema.operations["set"] = {
url: [ spec.basePath, pathKey ].join(''),
schema: spec.definitions[ collectionName ]
schema: spec.definitions[ schemaName ]
};
}
if ( pathObj.delete ) {
collection.operations["delete"] = {
schema.operations["delete"] = {
url: [ spec.basePath, pathKey ].join(''),
schema: spec.definitions[ collectionName ]
schema: spec.definitions[ schemaName ]
};
}
}
}

if ( this.currentCollection && this.collections[ this.currentCollection ] ) {
if ( this.currentSchemaName && this.schema[ this.currentSchemaName ] ) {
this.fetchPage();
}
else {
this.currentCollection = firstCollection;
this.currentSchemaName = firstSchema;
}
},

fetchPage: function () {
if ( this.fetching ) return;
var coll = this.collections[ this.currentCollection ],
self = this,
var self = this,
query = {
$limit: this.perPage,
$offset: this.perPage * ( this.currentPage - 1 )
Expand All @@ -455,7 +454,7 @@ var app = new Vue({

this.fetching = true;
delete this.error.fetchPage;
$.get( coll.operations["list"].url, query ).done(
$.get( this.currentOperations["list"].url, query ).done(
function ( data, status, jqXHR ) {
if ( query.offset > data.total ) {
// We somehow got to a page that doesn't exist,
Expand All @@ -468,7 +467,7 @@ var app = new Vue({

self.items = data.items;
self.total = data.total;
self.columns = self.getListColumns( self.currentCollection ),
self.columns = self.getListColumns( self.currentSchemaName ),
self.fetching = false;
self.updateHash();
}
Expand All @@ -479,9 +478,8 @@ var app = new Vue({
);
},

getListColumns: function ( collName ) {
var coll = this.collections[ collName ],
schema = coll.operations["list"].schema,
getListColumns: function ( schemaName ) {
var schema = this.schema[ schemaName ].operations["list"].schema,
props = schema.properties,
columns = schema['x-list-columns'] || [];
return columns.map( function (c) {
Expand All @@ -494,23 +492,22 @@ var app = new Vue({

parseHash: function () {
var parts = location.hash.split( '/' ),
collection = parts[1],
schema = parts[1],
page = parts[2];
return {
collection: collection,
schema: schema,
page: page
};
},

updateHash: function () {
location.hash = "/" + this.currentCollection + "/" + this.currentPage;
location.hash = "/" + this.currentSchemaName + "/" + this.currentPage;
},

saveItem: function (i) {
var self = this,
coll = this.collections[ this.currentCollection ],
value = this.prepareSaveItem( this.items[i], coll.operations['set'].schema ),
url = this.fillUrl( coll.operations['set'].url, this.openedOldValue );
value = this.prepareSaveItem( this.items[i], this.currentOperations['set'].schema ),
url = this.fillUrl( this.currentOperations['set'].url, this.openedOldValue );
delete this.error.saveItem;
this.$set( this, 'formError', {} );
$.ajax(
Expand Down Expand Up @@ -541,9 +538,9 @@ var app = new Vue({

addItem: function () {
var self = this,
coll = this.collections[ this.currentCollection ],
value = this.prepareSaveItem( this.newItem, coll.operations['add'].schema ),
url = coll.operations['add'].url;
schema = this.currentSchema,
value = this.prepareSaveItem( this.newItem, schema ),
url = this.currentOperations['add'].url;
delete this.error.addItem;
this.$set( this, 'formError', {} );
$.ajax(
Expand All @@ -555,13 +552,13 @@ var app = new Vue({
}
).done(
function ( data, status, jqXHR ) {
var id = coll.operations['add'].schema['x-id-field'] || 'id';
var id = self.currentOperations['add'].schema['x-id-field'] || 'id';
var urlParams = {};
urlParams[ id ] = data;

$.ajax(
{
url: self.fillUrl( coll.operations['get'].url, urlParams ),
url: self.fillUrl( self.currentOperations['get'].url, urlParams ),
method: 'GET',
dataType: 'json'
}
Expand Down Expand Up @@ -654,7 +651,7 @@ var app = new Vue({
},

createBlankItem: function () {
var schema = this.collections[ this.currentCollection ].operations['add'].schema,
var schema = this.currentSchema,
item = {};
if ( schema.example ) {
return schema.example;
Expand All @@ -678,9 +675,9 @@ var app = new Vue({
deleteItem: function () {
var i = this.deleteIndex,
self = this,
coll = this.collections[ this.currentCollection ],
schema = this.currentSchema,
value = $( '#data-' + i ).val(),
url = this.fillUrl( coll.operations['delete'].url, this.items[i] );
url = this.fillUrl( this.currentOperations['delete'].url, this.items[i] );
$.ajax(
{
url: url,
Expand Down Expand Up @@ -712,8 +709,8 @@ var app = new Vue({
},

renderValue: function ( field, value ) {
var coll = this.schema;
var fieldType = coll.properties[ field ].type;
var schema = this.currentSchema;
var fieldType = schema.properties[ field ].type;
var type = Array.isArray( fieldType ) ? fieldType[0] : fieldType;
if ( type == 'boolean' ) {
return value ? 'Yes' : 'No';
Expand All @@ -722,7 +719,7 @@ var app = new Vue({
},

rowViewUrl: function ( data ) {
return this.fillUrl( this.schema[ 'x-view-item-url' ], data );
return this.fillUrl( this.currentSchema[ 'x-view-item-url' ], data );
},

addToast: function ( toast ) {
Expand Down Expand Up @@ -766,15 +763,15 @@ var app = new Vue({
return pages;
},

operations: function () {
return this.collections[ this.currentCollection ] ? this.collections[ this.currentCollection ].operations : {};
currentOperations: function () {
return this.schema[ this.currentSchemaName ] ? this.schema[ this.currentSchemaName ].operations : {};
},
schema: function () {
return this.operations.get ? this.operations.get.schema : {};
currentSchema: function () {
return this.currentOperations.get ? this.currentOperations.get.schema : {};
}
},
watch: {
currentCollection: function () {
currentSchemaName: function () {
this.data = [];
this.currentPage = 1;
this.openedRow = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</div>
% }
% else {
%= app->yancy->form->field_for( $plugin->collection, $field )
%= app->yancy->form->field_for( $plugin->schema, $field )
% }
% }
<button class="btn btn-primary">Register</button>
Expand Down
Loading

0 comments on commit 99df7fe

Please sign in to comment.