Skip to content

Commit

Permalink
Refactor setLive Method by Extracting Draftable Relations Logic (#44)
Browse files Browse the repository at this point in the history
* Refactor: Extract relation replication logic into separate method

- Isolates the logic for replicating and associating draftable relations to a published model into a standalone method for improved readability and modularity
- Enhances flexibility for future adjustments and custom use-cases

* quick fix
  • Loading branch information
vildanbina authored Feb 10, 2024
1 parent 11047fa commit 3c61ace
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions src/Concerns/HasDrafts.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,40 +192,7 @@ public function setLive(): void
$published->setCurrent();
$published->saveQuietly();

collect($this->getDraftableRelations())->each(function (string $relationName) use ($published) {
$relation = $published->{$relationName}();
switch (true) {
case $relation instanceof HasOne:
if ($related = $this->{$relationName}) {
$replicated = $related->replicate();

$method = method_exists($replicated, 'getDraftableAttributes')
? 'getDraftableAttributes'
: 'getAttributes';

$published->{$relationName}()->create($replicated->$method());
}

break;
case $relation instanceof HasMany:
$this->{$relationName}()->get()->each(function ($model) use ($published, $relationName) {
$replicated = $model->replicate();

$method = method_exists($replicated, 'getDraftableAttributes')
? 'getDraftableAttributes'
: 'getAttributes';

$published->{$relationName}()->create($replicated->$method());
});

break;
case $relation instanceof MorphToMany:
case $relation instanceof BelongsToMany:
$published->{$relationName}()->sync($this->{$relationName}()->pluck('id'));

break;
}
});
; $this->replicateAndAssociateDraftableRelations($published);
});

$this->{$this->getIsPublishedColumn()} = false;
Expand All @@ -235,6 +202,44 @@ public function setLive(): void
$this->shouldCreateRevision = false;
}

public function replicateAndAssociateDraftableRelations(Model $published): void
{
collect($this->getDraftableRelations())->each(function (string $relationName) use ($published) {
$relation = $published->{$relationName}();
switch (true) {
case $relation instanceof HasOne:
if ($related = $this->{$relationName}) {
$replicated = $related->replicate();

$method = method_exists($replicated, 'getDraftableAttributes')
? 'getDraftableAttributes'
: 'getAttributes';

$published->{$relationName}()->create($replicated->$method());
}

break;
case $relation instanceof HasMany:
$this->{$relationName}()->get()->each(function ($model) use ($published, $relationName) {
$replicated = $model->replicate();

$method = method_exists($replicated, 'getDraftableAttributes')
? 'getDraftableAttributes'
: 'getAttributes';

$published->{$relationName}()->create($replicated->$method());
});

break;
case $relation instanceof MorphToMany:
case $relation instanceof BelongsToMany:
$published->{$relationName}()->sync($this->{$relationName}()->pluck('id'));

break;
}
});
}

public function getDraftableRelations(): array
{
return property_exists($this, 'draftableRelations') ? $this->draftableRelations : [];
Expand Down

0 comments on commit 3c61ace

Please sign in to comment.