Skip to content

Commit

Permalink
refactor: refactor Board, Changelog, Comment, Item, Project, Tag and …
Browse files Browse the repository at this point in the history
…User models.
  • Loading branch information
fabiorodriguesroque committed Nov 10, 2024
1 parent 48c4fff commit 1affb67
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 23 deletions.
29 changes: 25 additions & 4 deletions app/Models/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

use App\Traits\Sluggable;
use App\Traits\HasOgImage;
use Database\Factories\BoardFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Board extends Model
{
/** @use HasFactory<BoardFactory> */
use HasFactory, Sluggable, HasOgImage;

const SORT_ITEMS_BY_POPULAR = 'popular';
Expand All @@ -33,22 +38,38 @@ class Board extends Model
'block_votes' => 'boolean'
];

public function project()
/**
* Get the project that owns the board.
*
* @return BelongsTo<Project, $this>
*/
public function project(): BelongsTo
{
return $this->belongsTo(Project::class);
}

public function items()
/**
* Get the items for the board.
*
* @return HasMany<Item, $this>
*/
public function items(): hasMany
{
return $this->hasMany(Item::class);
}

public function scopeVisible($query)
/**
* Scope by visible boards.
*
* @param Builder<Board> $query
* @return Builder<Board>
*/
public function scopeVisible(Builder $query): Builder
{
return $query->where('visible', true);
}

public function canUsersCreateItem()
public function canUsersCreateItem(): bool
{
return $this->can_users_create;
}
Expand Down
18 changes: 18 additions & 0 deletions app/Models/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Traits\HasUpvote;
use App\Traits\Sluggable;
use App\Traits\HasOgImage;
use Database\Factories\ChangelogFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -13,6 +14,7 @@

class Changelog extends Model
{
/** @use HasFactory<ChangelogFactory> */
use HasFactory, Sluggable, HasOgImage, HasUpvote;

public $fillable = [
Expand All @@ -27,16 +29,32 @@ class Changelog extends Model
'published_at' => 'datetime',
];

/**
* Scope by published ones.
*
* @param Builder<Changelog> $query
* @return Builder<Changelog>
*/
public function scopePublished(Builder $query): Builder
{
return $query->where('published_at', '<=', now())->latest('published_at');
}

/**
* Get the user that owns the changelog.
*
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/**
* Get the item that owns the changelog.
*
* @return BelongsToMany<Item, $this>
*/
public function items(): BelongsToMany
{
return $this->belongsToMany(Item::class);
Expand Down
10 changes: 5 additions & 5 deletions app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace App\Models;

use App\Traits\HasUpvote;
use Database\Factories\CommentFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Spatie\Activitylog\LogOptions;
use Xetaio\Mentions\Models\Mention;
use Database\Factories\CommentFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Xetaio\Mentions\Models\Traits\HasMentionsTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Comment extends Model
Expand Down
78 changes: 73 additions & 5 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Str;
use App\Enums\InboxWorkflow;
use App\Settings\GeneralSettings;
use Database\Factories\ItemFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
Expand All @@ -18,10 +19,12 @@
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Spatie\Activitylog\Exceptions\InvalidConfiguration;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Item extends Model
{
/** @use HasFactory<ItemFactory> */
use HasFactory, Sluggable, HasOgImage, HasUpvote, HasTags;

public $fillable = [
Expand All @@ -48,6 +51,9 @@ public static function getTagClassName(): string
return Tag::class;
}

/**
* @return Attribute<string, string>
*/
protected function excerpt(): Attribute
{
return Attribute::make(
Expand All @@ -57,6 +63,9 @@ protected function excerpt(): Attribute
);
}

/**
* @return Attribute<string, string>
*/
protected function viewUrl(): Attribute
{
return Attribute::make(
Expand All @@ -70,64 +79,115 @@ protected function viewUrl(): Attribute
)->shouldCache();
}

/**
* Get the board that owns the item..
*
* @return BelongsTo<Board, $this>
*/
public function board(): BelongsTo
{
return $this->belongsTo(Board::class);
}

/**
* Get the user that owns the item.
*
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/**
* Get the project that owns the item.
*
* @return BelongsTo<Project, $this>
*/
public function project(): BelongsTo
{
return $this->belongsTo(Project::class);
}

/**
* @return MorphMany<Vote, $this>
*/
public function subscribedVotes(): MorphMany
{
return $this->votes()->where('subscribed', true);
}

/**
* Get the comments for the item.
*
* @return HasMany<Comment, $this>
*/
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}

/**
* Get the parent comments for the item.
*
* @return HasMany<Comment, $this>
*/
public function parentComments(): HasMany
{
return $this->hasMany(Comment::class)->whereNotNull('parent_id')->latest();
}

/**
* Get the assigned users that owns the item.
*
* @return BelongsToMany<User, $this>
*/
public function assignedUsers(): BelongsToMany
{
return $this->belongsToMany(User::class, 'item_user');
}

/**
* @return BelongsToMany<Changelog, $this>
*/
public function changelogs(): BelongsToMany
{
return $this->belongsToMany(Changelog::class);
}

/**
* @return MorphMany<Model, $this>
* @throws InvalidConfiguration
*/
public function activities(): MorphMany
{
return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'subject');
}

/**
* @return MorphToMany<Tag, $this>
*/
public function tags(): MorphToMany
{
return $this
->morphToMany(self::getTagClassName(), 'taggable', 'taggables', null, 'tag_id')
->morphToMany(Tag::class, 'taggable', 'taggables', null, 'tag_id')
->orderBy('order_column');
}

public function scopePopular($query)
/**
* @param Builder<Item> $query
* @return Builder<Item>
*/
public function scopePopular(Builder $query): Builder
{
return $query->orderBy('total_votes', 'desc');
}

public function scopeVisibleForCurrentUser(Builder $query)
/**
* @param Builder<Item> $query
* @return Builder<Item>
*/
public function scopeVisibleForCurrentUser(Builder $query): Builder
{
if (auth()->user()?->hasAdminAccess()) {
return $query;
Expand All @@ -138,7 +198,11 @@ public function scopeVisibleForCurrentUser(Builder $query)
});
}

public function scopeForInbox($query)
/**
* @param Builder<Item> $query
* @return Builder<Item>|null
*/
public function scopeForInbox(Builder $query): Builder|null
{
return match (app(GeneralSettings::class)->getInboxWorkflow()) {
InboxWorkflow::WithoutBoardAndProject => $query->whereNull('project_id')->whereNull('board_id'),
Expand All @@ -148,7 +212,11 @@ public function scopeForInbox($query)
};
}

public function scopeNoChangelogTag($query)
/**
* @param Builder<Item> $query
* @return Builder<Item>
*/
public function scopeNoChangelogTag(Builder $query): Builder
{
return $query
->whereDoesntHave('tags', function (Builder $query) {
Expand Down
24 changes: 21 additions & 3 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Traits\Sluggable;
use App\Traits\HasOgImage;
use Database\Factories\ProjectFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand All @@ -12,6 +13,7 @@

class Project extends Model
{
/** @use HasFactory<ProjectFactory> */
use HasFactory, Sluggable, HasOgImage;

public $fillable = [
Expand All @@ -32,25 +34,41 @@ class Project extends Model

/**
* Get the boards for the project.
*
*
* @return HasMany<Board, $this>
*/
public function boards()
public function boards(): HasMany
{
return $this->hasMany(Board::class)->orderBy('sort_order');
}

/**
* Get the project members.
*
* @return BelongsToMany<User, $this>
*/
public function members(): BelongsToMany
{
return $this->belongsToMany(User::class, 'project_member')->using(ProjectMember::class);
}

/**
* Get the project items.
*
* @return HasMany<Item, $this>
*/
public function items(): HasMany
{
return $this->hasMany(Item::class);
}

public function scopeVisibleForCurrentUser($query)
/**
* Scope by visible projects to current user.
*
* @param Builder<Project> $query
* @return Builder<Project>
*/
public function scopeVisibleForCurrentUser(Builder $query): Builder
{
if (auth()->user()?->hasAdminAccess()) {
return $query;
Expand Down
Loading

0 comments on commit 1affb67

Please sign in to comment.