Skip to content

Commit

Permalink
Refotarado TagsUtils para esquema de fabrica
Browse files Browse the repository at this point in the history
  • Loading branch information
RatoX committed Dec 26, 2014
1 parent 645e4bb commit a2af537
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
25 changes: 3 additions & 22 deletions app/scripts/controllers/post/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ angular.module('cmsApp')
.controller('PostCreateCtrl', function ($rootScope, $scope, $location, $routeParams, $q, PostUtil, Repository, YoutubeLinkUtil, VimeoLinkUtil, TagsUtil, _) {
var getReleatedPosts = function(tags){
tags = _.map(tags, function(e){ return getSlug(e.tag);} );
var tagsFile = $rootScope.user.postsGroupByTag;
var releatedPosts = TagsUtil.getPostsByTags(tags, tagsFile);

return releatedPosts;
return $scope.tags.getPostsByTags(tags);
};

$scope.state = 'default';
Expand All @@ -26,21 +23,7 @@ angular.module('cmsApp')
$scope.cover = '';
$scope.fields = $rootScope.user.skelleton || [];
$scope.files = [];
$scope.autocomplete = {
tags: function(query){
var deferred = $q.defer();
var keys = $rootScope.user.tags;
var tags = _.map(keys, function(e){
if(e.match(query)){
return {tag: e};
}
return false;
});
deferred.resolve(_.compact(tags));

return deferred.promise;
}
};
$scope.tags = { };

$scope.fields.forEach(function(element){
if( element.type.view === 'cover'){
Expand Down Expand Up @@ -90,9 +73,7 @@ angular.module('cmsApp')

var loadTagsFile = function(){
Repository.tagsFile.get($scope.user).then(function(result){
var tagsFile = angular.fromJson(result);
$rootScope.user.tags = _.keys(tagsFile);
$rootScope.user.postsGroupByTag = tagsFile;
$scope.tags = new TagsUtil(angular.fromJson(result));
});
};

Expand Down
32 changes: 29 additions & 3 deletions app/scripts/services/util/tags-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Service in the cmsApp.
*/
angular.module('cmsApp')
.service('TagsUtil', function TagsUtil(_) {
.factory('TagsUtil', function($q, _) {
var intersection = function(array){
if (!array) {
return [];
Expand Down Expand Up @@ -36,11 +36,37 @@ angular.module('cmsApp')

return _.countBy(agroups);
};
var tagsFile = {};
var allTags = {};

this.getPostsByTags = function(selectedTags, allTagsWithPosts){
var allPosts = _.values(_.pick(allTagsWithPosts, selectedTags));
var search = function(query){
var deferred = $q.defer();
var tags = _.map(allTags, function(e){
if(e.match(query)){
return {tag: e};
}
return false;
});
deferred.resolve(_.compact(tags));

return deferred.promise;
};

var getPostsByTags = function(selectedTags){
var postsByTag = _.pick(tagsFile, selectedTags);
var allPosts = _.values(postsByTag);
var result = intersection.apply(this, allPosts);

return result;
};

var factory = function(file){
tagsFile = file;
allTags = _.keys(tagsFile);
return {
search: search,
getPostsByTags: getPostsByTags
};
};
return factory;
});
2 changes: 1 addition & 1 deletion app/views/post/include/dynamic-fields.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
class='tag-input'
show-errors
placeholder="{{ field.placeholder }}">
<auto-complete source="autocomplete.tags($query)"></auto-complete>
<auto-complete source="tags.search($query)"></auto-complete>
</tags-input>
</div>
</div>
14 changes: 10 additions & 4 deletions test/spec/services/util/tags-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,34 @@ describe('Service: TagsUtil', function () {
});

describe('give a tag-posts file and a set of tags', function (){
var tagsWithPosts;
var tagsWithPosts, factory;

beforeEach(function () {
tagsWithPosts = {'Banana':['dois','tres', 'sete'],
'Cianureto':['tres','sete'],
'Manga':['dois','quatro'],
'Maca':['quatro', 'seis', 'sete']};
factory = new TagsUtil(tagsWithPosts);
});

it('should get the posts with more relevance to a set of tags', function(){
var tags = ['Manga','Banana','Cianureto'];
expect(TagsUtil.getPostsByTags(tags, tagsWithPosts)).toEqual({'sete':1, 'dois':1, 'tres': 1});
expect(factory.getPostsByTags(tags, tagsWithPosts)).toEqual({'sete':1, 'dois':1, 'tres': 1});
});

it('should get the posts with more relevance to a set of tags', function(){
var tags = ['Banana','Maca'];
expect(TagsUtil.getPostsByTags(tags, tagsWithPosts)).toEqual({'sete':1});
expect(factory.getPostsByTags(tags, tagsWithPosts)).toEqual({'sete':1});
});

it('should get at least 3 result', function(){
var tags = ['Banana','Maca'];
expect(factory.getPostsByTags(tags, tagsWithPosts)).toEqual({'dois':1, 'tres': 0, 'quatro': 0});
});

it('should get the posts with more relevance to a set of tags', function(){
var tags = ['Banana','Maca','Cianureto'];
expect(TagsUtil.getPostsByTags(tags, tagsWithPosts)).toEqual({'sete':3,'tres':1});
expect(factory.getPostsByTags(tags, tagsWithPosts)).toEqual({'sete':3,'tres':1});
});
});
});

0 comments on commit a2af537

Please sign in to comment.