Skip to content

Commit fcdf29e

Browse files
committed
wip: add post factories
1 parent b18dbea commit fcdf29e

File tree

3 files changed

+15
-64
lines changed

3 files changed

+15
-64
lines changed

database/factories/PostTypeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Database\Factories;
3+
namespace Javaabu\Cms\Database\Factories;
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
66
use Illuminate\Support\Carbon;

src/Models/Post.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Javaabu\Cms\Models;
44

55
use Carbon\Carbon;
6+
use Database\Factories\PostFactory;
67
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Factories\HasFactory;
79
use Illuminate\Database\Eloquent\Model;
810
use Illuminate\Database\Eloquent\Relations\BelongsTo;
911
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -30,6 +32,7 @@ class Post extends Model implements
3032
use Publishable;
3133
use HasSlug;
3234
use IsJsonTranslatable;
35+
use HasFactory;
3336

3437
protected static $status_class = PublishStatuses::class;
3538

@@ -433,4 +436,9 @@ public function getContentBlocksAttribute(): array
433436
];
434437
}
435438
}
439+
440+
protected static function newFactory(): PostFactory
441+
{
442+
return PostFactory::new();
443+
}
436444
}

src/Models/PostType.php

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Javaabu\Cms\Models;
44

5-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Relations\HasMany;
7-
use Illuminate\Support\Str;
8-
use Javaabu\Cms\Enums\PostTypeFeatures;
7+
use Javaabu\Cms\Database\Factories\PostTypeFactory;
98
use Javaabu\Helpers\AdminModel\AdminModel;
109
use Javaabu\Helpers\AdminModel\IsAdminModel;
1110
use Illuminate\Database\Eloquent\Model;
@@ -14,7 +13,7 @@
1413

1514
class PostType extends Model implements AdminModel, Translatable
1615
{
17-
// use HasFactory;
16+
use HasFactory;
1817
use IsAdminModel;
1918
use IsJsonTranslatable;
2019
/**
@@ -47,20 +46,10 @@ public function getAdminUrlAttribute(): string
4746
return 'post_type';
4847
}
4948

50-
/**
51-
* Get the permission slug
52-
*
53-
* @return string
54-
*/
55-
public function getPermissionSlugAttribute()
56-
{
57-
return Str::slug($this->slug, '_');
58-
}
59-
6049
/**
6150
* Get the route key name
6251
*/
63-
public function getRouteKeyName()
52+
public function getRouteKeyName(): string
6453
{
6554
return 'slug';
6655
}
@@ -78,44 +67,11 @@ public function getTranslatables(): array
7867
];
7968
}
8069

81-
public function categoryType(): BelongsTo
82-
{
83-
return $this->belongsTo(CategoryType::class);
84-
}
85-
86-
public function getFeatureName($feature): ?string
87-
{
88-
if (! $this->hasFeature($feature)) {
89-
return null;
90-
}
91-
92-
$feature_title = $this->features[$feature];
93-
94-
if (gettype($feature_title) == 'boolean') {
95-
return PostTypeFeatures::getLabel($feature);
96-
}
97-
98-
if (gettype($feature_title) == 'string') {
99-
return Str::title($feature_title);
100-
}
101-
102-
return null;
103-
}
104-
10570
public function hasFeature($feature): bool
10671
{
10772
return array_key_exists($feature, $this->features);
10873
}
10974

110-
public function getFeatureCollectionName($feature): ?string
111-
{
112-
if (! $this->hasFeature($feature)) {
113-
return null;
114-
}
115-
116-
return PostTypeFeatures::getCollectionName($feature);
117-
}
118-
11975
/**
12076
* Gets the views to use for the post type
12177
*
@@ -138,14 +94,6 @@ public function getPaginatorCount(): int
13894
return get_setting($this->slug . '_per_page') ?? get_setting('per_page');
13995
}
14096

141-
/**
142-
* A post type has many posts
143-
*/
144-
public function userVisiblePosts()
145-
{
146-
return $this->posts()->userVisibleForPostType($this);
147-
}
148-
14997
/**
15098
* A post type has many posts
15199
*
@@ -156,14 +104,9 @@ public function posts(): HasMany
156104
return $this->hasMany(Post::class, 'type', 'slug');
157105
}
158106

159-
/**
160-
* Slugify the value
161-
*
162-
* @param $value
163-
*/
164-
public function setSlugAttribute($value)
107+
protected static function newFactory(): PostTypeFactory
165108
{
166-
$this->attributes['slug'] = Str::slug($value);
109+
return PostTypeFactory::new();
167110
}
168111
}
169112

0 commit comments

Comments
 (0)