Skip to content

Commit

Permalink
Refactored hydrateMeta function. Changed primary key of ArticleMeta f…
Browse files Browse the repository at this point in the history
…rom id to meta_id. Various touch ups to tests.
  • Loading branch information
Jeremy Sik committed Sep 16, 2015
1 parent a3b3b56 commit 8184e9c
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 12 deletions.
5 changes: 4 additions & 1 deletion api/app/Models/ArticleMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ class ArticleMeta extends BaseModel
*
* @var string
*/

public $table = 'article_metas';

protected $fillable = ['id', 'article_id', 'meta_name', 'meta_content'];
protected $primaryKey = 'meta_id';

protected $fillable = ['meta_id', 'article_id', 'meta_name', 'meta_content'];

protected $guarded = ['meta_name'];

Expand Down
1 change: 1 addition & 0 deletions api/database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@

$factory->define(App\Models\ArticleMeta::class, function (\Faker\Generator $faker) {
return [
'meta_id' => $faker->uuid,
'meta_name' => $faker->boolean(50) ? $faker->randomElement(['name','description','keyword','canonical']) : $faker->word,
'meta_content' => $faker->slug,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class CreateArticleMetaTable extends Migration
public function up()
{
Schema::create(ArticleMeta::getTableName(), function (Blueprint $table) {
$table->uuid('id');
$table->uuid('meta_id');
$table->uuid('article_id');
$table->string('meta_name', 255);
$table->string('meta_content', 255)->nullable();
$table->dateTime('created_at');
$table->dateTime('updated_at')->nullable();

$table->primary('id');
$table->primary('meta_id');

$table->unique(['article_id', 'meta_name']);

Expand Down
3 changes: 2 additions & 1 deletion api/tests/integration/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,10 @@ public function testShouldLogDeleteMeta()
$article->push();

$metaEntity = $article->articleMetas->first();
$this->deleteJson('/articles/'.$article->article_id.'/meta/'.$metaEntity->meta_name);
$this->deleteJson('/articles/'.$article->article_id.'/meta/'.$metaEntity->meta_id);

$article = Article::find($article->article_id);

$this->assertCount(1, $article->revisionHistory->toArray());

$this->cleanupDiscussions([$article]);
Expand Down
2 changes: 1 addition & 1 deletion app/src/app/admin/articles/article/article.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace app.admin.articles.article {
let seededChance = new Chance(1),
testMeta:common.models.ArticleMeta = new common.models.ArticleMeta({
articleId: undefined,
id: 'f6a31e1d-60e0-35b5-a878-085deabe57bd',
id: seededChance.guid(),
metaName: 'title',
metaContent: 'foobar'
}),
Expand Down
2 changes: 1 addition & 1 deletion app/src/app/admin/articles/article/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace app.admin.articles.article {
return articleService.getArticle($stateParams.permalink)
.then((article) => {

article._articleMetas = articleService.hydrateMetaFromTemplate(article, ArticleConfig.articleMetaTemplate);
article._articleMetas = articleService.hydrateMetaCollectionFromTemplate(article.articleId, article._articleMetas, ArticleConfig.articleMetaTemplate);

return article;
});
Expand Down
2 changes: 1 addition & 1 deletion app/src/common/services/article/articleService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@

let article = fixtures.getArticle();

let hydratedMetaTags = articleService.hydrateMetaFromTemplate(article, articleMetaTemplate);
let hydratedMetaTags = articleService.hydrateMetaCollectionFromTemplate(article.articleId, article._articleMetas, articleMetaTemplate);

expect(_.size(hydratedMetaTags)).to.equal(5);

Expand Down
11 changes: 6 additions & 5 deletions app/src/common/services/article/articleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,26 @@ namespace common.services.article {

/**
* Hydrates a meta template with meta which already exists
* @param article
* @param articleId
* @param articleMetas
* @param template
*/
public hydrateMetaFromTemplate(article:common.models.Article, template:string[]):common.models.ArticleMeta[] {
public hydrateMetaCollectionFromTemplate(articleId:string, articleMetas:common.models.ArticleMeta[], template:string[]):common.models.ArticleMeta[] {
return (<any>_).chain(template)
.map((metaTagName) => {
let existingTag = _.find(article._articleMetas, {metaName:metaTagName});
let existingTag = _.find(articleMetas, {metaName:metaTagName});
if(_.isEmpty(existingTag)) {
return new common.models.ArticleMeta({
metaName:metaTagName,
metaContent:'',
articleId:article.articleId,
articleId:articleId,
id:this.ngRestAdapter.uuid()
});
}
return existingTag;
})
.thru((templateMeta) => {
let leftovers = _.filter(article._articleMetas, (metaTag) => {
let leftovers = _.filter(articleMetas, (metaTag) => {
return !_.contains(templateMeta, metaTag);
});

Expand Down

0 comments on commit 8184e9c

Please sign in to comment.