Skip to content

Commit

Permalink
Merge pull request #41 from zyoung51/feature/template-scope
Browse files Browse the repository at this point in the history
GRIP-17: Add scoping to templates
  • Loading branch information
benbp committed Dec 8, 2015
2 parents dd0bdf1 + 6d2f92f commit cce3e65
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
30 changes: 23 additions & 7 deletions lib/common/template.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ function templateServiceFactory(FileLoader, Constants, Promise, _, waterline, Lo
});
};

TemplateService.prototype.put = function (file, contents) {
TemplateService.prototype.put = function (file, contents, scope) {
scope = scope || 'global';
return Promise.resolve(
waterline.templates.findOne({ name: file })
waterline.templates.findOne({ name: file, scope: scope })
.then(function(doc) {
if (!_.isEmpty(doc)) {
return waterline.templates.update(
Expand All @@ -70,24 +71,39 @@ function templateServiceFactory(FileLoader, Constants, Promise, _, waterline, Lo
return waterline.templates.create(
{
name: file,
contents: contents
contents: contents,
scope: scope
}
);
}
})
);
};

TemplateService.prototype.get = function (name) {
return waterline.templates.findOne({ name: name });
TemplateService.prototype.get = function (name, scope) {
scope = scope || ['global'];

// The position of the tag defines the scope priority
var scopeWeight = {}, i = 1;
_.forEach(scope, function(item) {
scopeWeight[item] = i;
i += 1;
});

return waterline.templates.find({name: name, scope: scope}).then(function(templates) {
templates.sort(function(a,b) {
return scopeWeight[a.scope] - scopeWeight[b.scope];
});
return templates[0];
});
};

TemplateService.prototype.getAll = function () {
return waterline.templates.find();
};

TemplateService.prototype.render = function render (name, options) {
return this.get(name).then(function (template) {
TemplateService.prototype.render = function render (name, options, scope) {
return this.get(name, scope).then(function (template) {
return ejs.render(template.contents, options);
});
};
Expand Down
4 changes: 4 additions & 0 deletions lib/models/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function TemplateModelFactory (Model) {
contents: {
type: 'string',
required: true
},
scope: {
type: 'string',
defaultsTo: 'global'
}
}
});
Expand Down
67 changes: 62 additions & 5 deletions spec/lib/common/template-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,80 @@ describe('Templates', function () {
});

it('should get a template', function() {
waterline.templates.findOne.resolves('test contents');
waterline.templates.find.resolves(['test contents']);
return this.subject.get('test template')
.then(function(out) {
expect(out).to.equal('test contents');
expect(waterline.templates.findOne)
.to.have.been.calledWith({ name: 'test template' });
expect(waterline.templates.find)
.to.have.been.calledWith({ name: 'test template', scope: ['global'] });
});
});

it('should create a new template', function() {
it('should get the template in the right scope', function() {
var templates = [
{ name: 'template', contents: 'global scope', scope: 'global'},
{ name: 'template', contents: 'a scope', scope: 'a'},
{ name: 'template', contents: 'b scope', scope: 'b'}
];
var scope = ['b', 'a', 'global'];
waterline.templates.find.resolves(templates);
return this.subject.get('template', scope)
.then(function(out) {
expect(out.contents).to.equal('b scope');
expect(waterline.templates.find)
.to.have.been.calledWith({ name: 'template', scope: scope });
});
});

it('should be empty when the scope does not exist', function() {
var templates = [];
var scope = ['b'];
waterline.templates.find.resolves(templates);
return this.subject.get('template', scope)
.then(function(out) {
expect(out).to.equal(undefined);
expect(waterline.templates.find)
.to.have.been.calledWith({ name: 'template', scope: scope });
});
});

it('should get the next template when the scope does not exist', function() {
var templates = [
{ name: 'template', contents: 'global scope', scope: 'global'},
{ name: 'template', contents: 'a scope', scope: 'a'}
];
var scope = ['b', 'a', 'global'];
waterline.templates.find.resolves(templates);
return this.subject.get('template', scope)
.then(function(out) {
expect(out.contents).to.equal('a scope');
expect(waterline.templates.find)
.to.have.been.calledWith({ name: 'template', scope: scope });
});
});

it('should create a new template in global scope', function() {
var self = this;
waterline.templates.findOne.resolves(null);
return self.subject.put('test template', 'test contents')
.then(function() {
expect(waterline.templates.create).to.have.been.calledWith({
name: 'test template',
contents: 'test contents'
contents: 'test contents',
scope: 'global'
});
});
});

it('should create a new template in the specified scope', function() {
var self = this;
waterline.templates.findOne.resolves(null);
return self.subject.put('test template', 'test contents', 'sku')
.then(function() {
expect(waterline.templates.create).to.have.been.calledWith({
name: 'test template',
contents: 'test contents',
scope: 'sku'
});
});
});
Expand Down

0 comments on commit cce3e65

Please sign in to comment.