Skip to content

Commit

Permalink
Fix related names for models (#62)
Browse files Browse the repository at this point in the history
* Fix related names for models

* allow related name from args

* Fix tests

* remove log

* ignore
  • Loading branch information
mmcardle authored Feb 18, 2019
1 parent 38ac240 commit cd855f7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ __py_cache__
.DS_Store
Chrome_*
FireFox_*
.DS_Store
4 changes: 2 additions & 2 deletions app/build/django_builder.min.js

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions app/js/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,13 +916,14 @@ function ModelServiceFactory() {
const relatedList = []
const relatedNames = {}
const relatedIndexs = {}
const modelName = this.name
this.relationships.forEach(function (relationship) {
const toClass = relationship.to_class()
relatedIndexs[toClass] = (relatedIndexs[toClass] || 0) + 1
if (relatedList.indexOf(toClass) === -1) {
relatedNames[relationship.name] = toClass.toLowerCase() + "s"
relatedNames[relationship.name] = modelName.toLowerCase() + "s"
} else {
relatedNames[relationship.name] = toClass.toLowerCase() + "s_" + relatedIndexs[toClass]
relatedNames[relationship.name] = modelName.toLowerCase() + "s_" + relatedIndexs[toClass]
}
relatedList.push(toClass)
})
Expand Down Expand Up @@ -957,10 +958,12 @@ function ModelServiceFactory() {
}

const related_name = relatedNames[relationship.name]

cls += 'related_name="' + related_name + '"';
if(relationship.opts){
cls += ', ' + relationship.opts;
const opts = relationship.opts || ''
if (opts.indexOf('related_name=') === -1) {
cls += 'related_name="' + related_name + '", ';
}
if (relationship.opts) {
cls += relationship.opts;
}
cls += renderer.new_lines(1) + renderer.spaces(4);
cls += ')';
Expand Down
46 changes: 42 additions & 4 deletions test/unit/controllersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,60 @@ describe('Testing ModelController', function () {
var field = $scope.field_factory.make_field(field_opts, $scope);

// Relationship
var rel_opts = {"name": 'Rel', "type": 'RelType', "to": 'RelTo'};
var rel_opts = {
"name": 'Rel',
"type": 'RelType',
"to": 'RelTo'
};
var rel = $scope.relationship_factory.make_relationship(rel_opts);

// Relationship with args
var rel_with_args_opts = {
"name": 'RelWithArgs',
"type": 'RelTypeWithArgs',
"to": 'RelToWithArgs',
"opts": 'xxx=1'
};
var rel_with_args = $scope.relationship_factory.make_relationship(
rel_with_args_opts
);

// Relationship with RelatedName
var rel_with_related_name_opts = {
"name": 'RelWithName',
"type": 'RelTypeWithName',
"to": 'RelToWithName',
"opts": 'related_name="RelToWithName"'
};
var rel_with_related_name = $scope.relationship_factory.make_relationship(
rel_with_related_name_opts
);

// Model
var model_opts = {"name": 'ModelName'};
var model = $scope.model_factory(model_opts, $scope);
model.fields.push(field);
model.relationships.push(rel);
model.relationships.push(rel_with_args);
model.relationships.push(rel_with_related_name);
$scope.models.push(model);

$scope.create_tar_ball_url(true, true).then(function(expected_tar_ball_url_app_channels){
var tar_content3 = atob(expected_tar_ball_url_app_channels.slice(28));
expect(tar_content3.length).toBe(30720);
expect(tar_content3.indexOf('ModelName')).toBeGreaterThan(0);
expect(tar_content3.indexOf('RelType')).toBeGreaterThan(0);
expect(tar_content3.indexOf('Field')).toBeGreaterThan(0);
expect(tar_content3).toContain('ModelName');
expect(tar_content3).toContain('Rel');
expect(tar_content3).toContain('RelType');
expect(tar_content3).toContain('related_name="modelnames"');
expect(tar_content3).toContain('related_name="modelnames"');
expect(tar_content3).toContain('RelWithArgs');
expect(tar_content3).toContain('RelTypeWithArgs');
expect(tar_content3).toContain('xxx=1');
expect(tar_content3).toContain('RelWithName');
expect(tar_content3).toContain('RelTypeWithName');
expect(tar_content3).toContain('related_name="RelToWithName"');
expect(tar_content3).toContain('Field');
expect(tar_content3).toContain('FieldType');
done()
}).catch(function(err){
fail(err)
Expand Down

0 comments on commit cd855f7

Please sign in to comment.