Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix published scope #2606

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ protected function isTranslationModel(): bool
return Str::endsWith(get_class($this), 'Translation');
}

public function scopePublished($query): Builder
public function scopePublished(Builder $query): Builder
Copy link
Contributor Author

@Tofandel Tofandel Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the scopes have Builder $query in the twill interfaces, I'm actually really surprised php isn't complaining that the signature isn't compatible

interface TwillModelContract
{
public function scopePublished(Builder $query): Builder;
public function scopeAccessible(Builder $query): Builder;
public function scopeOnlyTrashed(Builder $query): Builder;
public function scopeDraft(Builder $query): Builder;
public function getTranslatedAttributes(): array;
}

{
return $query->where("{$this->getTable()}.published", true);
if ($this->isFillable('published')) {
return $query->where($query->qualifyColumn('published'), true);
}
return $query;
}

public function scopeAccessible($query): Builder
public function scopeAccessible(Builder $query): Builder
{
if (! TwillPermissions::enabled()) {
return $query;
Expand Down Expand Up @@ -75,11 +78,11 @@ public function scopeAccessible($query): Builder
return $query;
}

public function scopePublishedInListings($query): Builder
public function scopePublishedInListings(Builder $query): Builder
{
// @todo: Remove? Seems unused.
if ($this->isFillable('public')) {
$query->where("{$this->getTable()}.public", true);
$query->where($query->qualifyColumn('public'), true);
}

return $query->published()->visible();
Expand All @@ -88,21 +91,21 @@ public function scopePublishedInListings($query): Builder
/**
* @todo: Document
*/
public function scopeVisible($query): Builder
public function scopeVisible(Builder $query): Builder
{
if ($this->isFillable('publish_start_date')) {
$query->where(function ($query) {
$query->whereNull("{$this->getTable()}.publish_start_date")->orWhere(
"{$this->getTable()}.publish_start_date",
$query->whereNull($query->qualifyColumn('publish_start_date'))->orWhere(
$query->qualifyColumn('publish_start_date'),
'<=',
Carbon::now()
);
});

if ($this->isFillable('publish_end_date')) {
$query->where(function ($query) {
$query->whereNull("{$this->getTable()}.publish_end_date")->orWhere(
"{$this->getTable()}.publish_end_date",
$query->whereNull($query->qualifyColumn('publish_end_date'))->orWhere(
$query->qualifyColumn('publish_end_date'),
'>=',
Carbon::now()
);
Expand All @@ -118,12 +121,15 @@ public function setPublishStartDateAttribute($value): void
$this->attributes['publish_start_date'] = $value ?? Carbon::now();
}

public function scopeDraft($query): Builder
public function scopeDraft(Builder $query): Builder
{
return $query->where("{$this->getTable()}.published", false);
if ($this->isFillable('published')) {
return $query->where($query->qualifyColumn('published'), false);
}
return $query;
}

public function scopeOnlyTrashed($query): Builder
public function scopeOnlyTrashed(Builder $query): Builder
{
return $query->onlyTrashed();
}
Expand Down
Loading