Skip to content

Commit 49bb5e1

Browse files
committed
Entities: Been through tests to get all passing in dev
Fixed issues and needed test changes along the way.
1 parent 2daaa70 commit 49bb5e1

51 files changed

Lines changed: 250 additions & 105 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/Entities/Models/Book.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Book extends Entity implements HasDescriptionInterface, HasCoverInterface,
3333

3434
public float $searchFactor = 1.2;
3535

36-
protected $hidden = ['pivot', 'deleted_at'];
36+
protected $hidden = ['pivot', 'deleted_at', 'entity_id', 'entity_type', 'chapter_id', 'book_id'];
3737
protected $fillable = ['name'];
3838

3939
/**
@@ -67,7 +67,8 @@ public function directPages(): HasMany
6767
*/
6868
public function chapters(): HasMany
6969
{
70-
return $this->hasMany(Chapter::class);
70+
return $this->hasMany(Chapter::class)
71+
->where('type', '=', 'chapter');
7172
}
7273

7374
/**

app/Entities/Models/BookChild.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function changeBook(int $newBookId): self
3030
{
3131
$oldUrl = $this->getUrl();
3232
$this->book_id = $newBookId;
33+
$this->unsetRelation('book');
3334
$this->refreshSlug();
3435
$this->save();
3536

app/Entities/Models/Bookshelf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Bookshelf extends Entity implements HasDescriptionInterface, HasCoverInter
1919

2020
public float $searchFactor = 1.2;
2121

22-
protected $hidden = ['image_id', 'deleted_at', 'description_html'];
22+
protected $hidden = ['image_id', 'deleted_at', 'description_html', 'entity_id', 'entity_type', 'chapter_id', 'book_id'];
2323
protected $fillable = ['name'];
2424

2525
/**

app/Entities/Models/Chapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Chapter extends BookChild implements HasDescriptionInterface, HasDefaultTe
1919
use ContainerTrait;
2020

2121
public float $searchFactor = 1.2;
22-
protected $hidden = ['pivot', 'deleted_at', 'description_html'];
22+
protected $hidden = ['pivot', 'deleted_at', 'description_html', 'entity_id', 'entity_type', 'chapter_id'];
2323
protected $fillable = ['name', 'priority'];
2424

2525
/**

app/Entities/Models/Entity.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ public function matchesOrContains(self $entity): bool
214214
*/
215215
public function activity(): MorphMany
216216
{
217+
// TODO - Ensure this is scoped to entity type properly.
218+
// Add test if not.
217219
return $this->morphMany(Activity::class, 'loggable')
218220
->orderBy('created_at', 'desc');
219221
}
@@ -460,9 +462,10 @@ abstract public function relatedData(): HasOne;
460462
protected function getContentsAttributes(): array
461463
{
462464
$contentFields = [];
465+
$contentModel = $this instanceof Page ? EntityPageData::class : EntityContainerData::class;
463466

464467
foreach ($this->attributes as $key => $value) {
465-
if (!in_array($key, static::$commonFields)) {
468+
if (in_array($key, $contentModel::$fields)) {
466469
$contentFields[$key] = $value;
467470
}
468471
}

app/Entities/Models/EntityContainerData.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class EntityContainerData extends Model
2020
protected $primaryKey = 'entity_id';
2121
public $incrementing = false;
2222

23+
public static array $fields = [
24+
'description',
25+
'description_html',
26+
'default_template_id',
27+
'image_id',
28+
'sort_rule_id',
29+
];
30+
2331
/**
2432
* Override the default set keys for save query method to make it work with composite keys.
2533
*/

app/Entities/Models/EntityPageData.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ class EntityPageData extends Model
1212
public $timestamps = false;
1313
protected $primaryKey = 'page_id';
1414
public $incrementing = false;
15+
16+
public static array $fields = [
17+
'draft',
18+
'template',
19+
'revision_count',
20+
'editor',
21+
'html',
22+
'text',
23+
'markdown',
24+
];
1525
}

app/Entities/Models/EntityQueryBuilder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,13 @@ public function withoutGlobalScope($scope): static
2626

2727
return parent::withoutGlobalScope($scope);
2828
}
29+
30+
/**
31+
* Override the default forceDelete method to add type filter onto the query
32+
* since it specifically ignores scopes by default.
33+
*/
34+
public function forceDelete()
35+
{
36+
return $this->query->where('type', '=', $this->model->getMorphClass())->delete();
37+
}
2938
}

app/Entities/Models/Page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Page extends BookChild
3434

3535
public string $textField = 'text';
3636
public string $htmlField = 'html';
37-
protected $hidden = ['html', 'markdown', 'text', 'pivot', 'deleted_at'];
37+
protected $hidden = ['html', 'markdown', 'text', 'pivot', 'deleted_at', 'entity_id', 'entity_type'];
3838
protected $fillable = ['name', 'priority'];
3939

4040
protected $casts = [

app/Entities/Queries/BookQueries.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public function visibleForList(): Builder
5555
->select(static::$listAttributes);
5656
}
5757

58+
public function visibleForContent(): Builder
59+
{
60+
return $this->start()->scopes('visible');
61+
}
62+
5863
public function visibleForListWithCover(): Builder
5964
{
6065
return $this->visibleForList()->with('cover');

0 commit comments

Comments
 (0)