Skip to content

Commit

Permalink
cleaned up the Tag model and obsoleted/unused property
Browse files Browse the repository at this point in the history
  • Loading branch information
luceos committed Oct 7, 2019
1 parent 97bebce commit ba4c492
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/Api/Serializer/TagSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ protected function getDefaultAttributes($tag)
'backgroundUrl' => $tag->background_path,
'backgroundMode' => $tag->background_mode,
'icon' => $tag->icon,
'iconUrl' => $tag->icon_path,
'discussionCount' => (int) $tag->discussion_count,
'position' => $tag->position === null ? null : (int) $tag->position,
'defaultSort' => $tag->default_sort,
Expand Down
79 changes: 28 additions & 51 deletions src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,38 @@
use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;

/**
* @property int $id
* @property string $name
* @property string $slug
* @property string $description
* @property string $color
* @property string $background_path
* @property string $background_mode
* @property int $position
* @property int $parent_id
* @property string $default_sort
* @property bool $is_restricted
* @property bool $is_hidden
* @property int $discussion_count
* @property \Carbon\Carbon $last_posted_at
* @property int $last_posted_discussion_id
* @property int $last_posted_user_id
* @property string $icon
*/
class Tag extends AbstractModel
{
use ScopeVisibilityTrait;

/**
* {@inheritdoc}
*/
protected $table = 'tags';

/**
* {@inheritdoc}
*/
protected $dates = ['last_posted_at'];

/**
* {@inheritdoc}
*/
protected $casts = [
'is_hidden' => 'bool',
'is_restricted' => 'bool'
];

public static function boot()
{
parent::boot();
Expand All @@ -51,6 +66,7 @@ public static function boot()
* @param string $slug
* @param string $description
* @param string $color
* @param string $icon
* @param bool $isHidden
* @return static
*/
Expand All @@ -68,43 +84,26 @@ public static function build($name, $slug, $description, $color, $icon, $isHidde
return $tag;
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent()
{
return $this->belongsTo(self::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function lastPostedDiscussion()
{
return $this->belongsTo(Discussion::class, 'last_posted_discussion_id');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function lastPostedUser()
{
return $this->belongsTo(User::class, 'last_posted_user_id');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function discussions()
{
return $this->belongsToMany(Discussion::class);
}

/**
* Refresh a tag's last discussion details.
*
* @return $this
*/
public function refreshLastPostedDiscussion()
{
if ($lastPostedDiscussion = $this->discussions()->latest('last_posted_at')->first()) {
Expand All @@ -114,12 +113,6 @@ public function refreshLastPostedDiscussion()
return $this;
}

/**
* Set the tag's last discussion details.
*
* @param Discussion $discussion
* @return $this
*/
public function setLastPostedDiscussion(Discussion $discussion)
{
$this->last_posted_at = $discussion->last_posted_at;
Expand Down Expand Up @@ -173,13 +166,7 @@ public function scopeWithStateFor(Builder $query, User $user)
]);
}

/**
* @param User $user
* @param string $permission
* @param bool $condition
* @return array
*/
protected static function getIdsWherePermission(User $user, $permission, $condition = true)
protected static function getIdsWherePermission(User $user, string $permission, bool $condition = true): array
{
static $tags;

Expand Down Expand Up @@ -209,22 +196,12 @@ protected static function getIdsWherePermission(User $user, $permission, $condit
return $ids;
}

/**
* @param User $user
* @param string $permission
* @return array
*/
public static function getIdsWhereCan(User $user, $permission)
public static function getIdsWhereCan(User $user, string $permission): array
{
return static::getIdsWherePermission($user, $permission, true);
}

/**
* @param User $user
* @param string $permission
* @return array
*/
public static function getIdsWhereCannot(User $user, $permission)
public static function getIdsWhereCannot(User $user, string $permission): array
{
return static::getIdsWherePermission($user, $permission, false);
}
Expand Down

0 comments on commit ba4c492

Please sign in to comment.