Skip to content

Commit

Permalink
move draftRecord HasOne to HasRevisor Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
sheadawson committed Oct 23, 2024
1 parent 38d7dd2 commit e168efa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
19 changes: 0 additions & 19 deletions src/Concerns/HasPublishing.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,6 @@ public function publishedRecord(): HasOne
);
}

/**
* Get the draft record for this model
*
* @throws Exception
*/
public function draftRecord(): HasOne
{
if ($this->isDraftTableRecord()) {
throw new Exception('The draft record HasOne relationship is only available to Published and Version records');
}

$instance = (new static)->withDraftContext();
$localKey = $this->isVersionTableRecord() ? 'record_id' : $this->getKeyName();

return $this->newHasOne(
$instance, $this, $instance->getModel()->getTable().'.'.$this->getKeyName(), $localKey
);
}

/**
* Get the publisher relationship for this model
*/
Expand Down
21 changes: 21 additions & 0 deletions src/Concerns/HasRevisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Indra\Revisor\Concerns;

use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Indra\Revisor\Contracts\HasRevisor as HasRevisorContract;
Expand Down Expand Up @@ -92,6 +94,25 @@ public function getDraftTable(): string
return Revisor::getDraftTableFor($this->getBaseTable());
}

/**
* Get the draft record for this model
*
* @throws Exception
*/
public function draftRecord(): HasOne
{
if ($this->isDraftTableRecord()) {
throw new Exception('The draft record HasOne relationship is only available to Published and Version records');
}

$instance = (new static)->withDraftContext();
$localKey = $this->isVersionTableRecord() ? 'record_id' : $this->getKeyName();

return $this->newHasOne(
$instance, $this, $instance->getModel()->getTable().'.'.$this->getKeyName(), $localKey
);
}

/**
* Get a Builder instance for the Draft table
*/
Expand Down

0 comments on commit e168efa

Please sign in to comment.