Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
fix(className): Add class name to model, change props names for detec…
Browse files Browse the repository at this point in the history
…t pk key in model
  • Loading branch information
EndyKaufman committed May 10, 2017
1 parent 0a6c10e commit e28266d
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/shared/models/content-type.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class ContentType extends ResourceModel {
};
static fields: any = ['id', 'name', 'title'];

public className = 'ContentType';

id: number;
name: string;
title: string;
Expand Down
5 changes: 3 additions & 2 deletions src/shared/models/fontawesome.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export class Fontawesome extends ResourceModel {
};
static fields: any = ['code', 'class'];

public _pkFieldName = 'class';
public _pkIsNumber = false;
public className = 'Fontawesome';
public pkFieldName = 'class';
public pkIsNumber = false;

code: string;
class: string;
Expand Down
2 changes: 2 additions & 0 deletions src/shared/models/group-permission.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class GroupPermission extends ResourceModel {
};
static fields: any = ['id', 'group', 'permission'];

public className = 'GroupPermission';

id: number;
group: Group;
permission: Permission;
Expand Down
2 changes: 2 additions & 0 deletions src/shared/models/group.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class Group extends ResourceModel {
};
static fields: any = ['id', 'name', 'title', 'permissions'];

public className = 'Group';

id: number;
name: string;
title: string;
Expand Down
2 changes: 2 additions & 0 deletions src/shared/models/meta.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export class MetaModel extends ResourceModel {
};
static fields: any = ['totalResults', 'curPage', 'totalPages', 'perPage'];

public className = 'MetaModel';

totalResults: number;
curPage: number;
totalPages: number;
Expand Down
2 changes: 2 additions & 0 deletions src/shared/models/permission.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class Permission extends ResourceModel {
};
static fields: any = ['id', 'contentType', 'title', 'name'];

public className = 'Permission';

id: number;
contentType: ContentType;
title: string;
Expand Down
24 changes: 11 additions & 13 deletions src/shared/models/resource.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,33 @@ import * as _ from 'lodash';
import { translate } from '../utils';

export class ResourceModel {
public _pkFieldName: string;
public _pkIsNumber: boolean;
public className = 'ResourceModel';
public pkFieldName: string;
public pkIsNumber: boolean;
[key: string]: any;

static meta(): any {
const meta: any = ResourceModel;
return meta;
}
constructor(obj?: any) {
if (this._pkFieldName === undefined) {
this._pkFieldName = 'id';
if (this.pkFieldName === undefined) {
this.pkFieldName = 'id';
}
if (this._pkIsNumber === undefined) {
this._pkIsNumber = true;
if (this.pkIsNumber === undefined) {
this.pkIsNumber = true;
}
if (obj && (_.isNumber(obj) || _.isString(obj))) {
const newObj: any = {};
newObj[this._pkFieldName] = obj;
newObj[this.pkFieldName] = obj;
obj = newObj;
}
this.parse(obj ? obj : {});
}
get pk(): string | number {
const key = this._pkFieldName;
const key = this.pkFieldName;
return this[key];
}
get pkFieldName() {
return this._pkFieldName;
}
parse(obj: any) {

}
Expand Down Expand Up @@ -66,8 +64,8 @@ export class ResourceModel {
}
}
}
if (this._pkIsNumber) {
this[this._pkFieldName] = +obj[this._pkFieldName];
if (this.pkIsNumber) {
this[this.pkFieldName] = +obj[this.pkFieldName];
}
}
format() {
Expand Down
5 changes: 3 additions & 2 deletions src/shared/models/theme.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export class Theme extends ResourceModel {

static fields: any = ['url', 'name'];

public _pkFieldName = 'url';
public _pkIsNumber = false;
public className = 'Theme';
public pkFieldName = 'url';
public pkIsNumber = false;

public url: string;
public name: string;
Expand Down
3 changes: 3 additions & 0 deletions src/shared/models/user-group.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export class UserGroup extends ResourceModel {
group: translate('Group')
};
static fields: any = ['id', 'user', 'group'];

public className = 'UserGroup';

id: number;
user: User;
group: Group;
Expand Down
5 changes: 2 additions & 3 deletions src/shared/models/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class User extends ResourceModel {
'isStaff', 'isActive', 'firstName', 'lastName', 'email',
'lastLogin', 'dateJoined', 'groups'];

public className = 'User';

id: number;
username: string;
password: string;
Expand Down Expand Up @@ -117,9 +119,6 @@ export class User extends ResourceModel {
}
}
checkPermissions(permissionNames: string[]) {
if (permissionNames.indexOf('admin') === -1) {
permissionNames.push('admin');
}
if (this.checkRoles(permissionNames)) {
return true;
} else {
Expand Down

0 comments on commit e28266d

Please # to comment.