Skip to content

Commit

Permalink
model events docs, add syncToCurrentVersion events
Browse files Browse the repository at this point in the history
  • Loading branch information
sheadawson committed Oct 11, 2024
1 parent 972b8f1 commit 5656aa7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 87 deletions.
9 changes: 8 additions & 1 deletion docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ export default defineConfig({
{text: "Preparing your Models", link: "/preparing-your-models"},
{text: "Managing Context", link: "/managing-context"},
{text: "Publishing", link: "/publishing"},
{text: "Versioning", link: "/versioning"}
{text: "Versioning", link: "/versioning"},
{text: "Model Events", link: "/model-events"}
],
},
{
text: "Integrations",
items: [
{text: "FilamentPHP", link: "/filament-php"},
],
},
],
Expand Down
1 change: 1 addition & 0 deletions docs/filament-php.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# FilamentPHP
34 changes: 34 additions & 0 deletions docs/model-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Model Events

Laravel Revisor provides several model events for you hook into key publishing and versioning events.

All events are fired on the Draft record, and pass the relevant published or versioned record as an argument to your
callback (if applicable).

Event listeners can be registered using static methods on the Model class that correspond to the event name.

```php
Page::published(function (Page $page, Page $publishedRecord) {
...
});
```

## Publishing Events

| Event | Description |
|--------------|-----------------------------------|
| publishing | Fired before publishing a Model |
| published | Fired after publishing a Model |
| unpublishing | Fired before unpublishing a Model |
| unpublished | Fired after unpublishing a Model |

## Versioning Events

| Event | Description |
|-------------------------|---------------------------------------------|
| savingNewVersion | Fired before saving a new Version |
| savedNewVersion | Fired after saving a new Version |
| syncingToCurrentVersion | Fired before syncing to the current Version |
| syncedToCurrentVersion | Fired after syncing to the current Version |
| revertingToVersion | Fired before reverting to a Version |
| revertedToVersion | Fired after reverting to a Version |
57 changes: 0 additions & 57 deletions docs/modes-and-dynamic-tables.md

This file was deleted.

28 changes: 0 additions & 28 deletions docs/page.md

This file was deleted.

21 changes: 20 additions & 1 deletion src/Concerns/HasVersioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function saveNewVersion(): HasRevisorContract|bool

$this->pruneVersions();

$this->fireModelEvent('savedNewVersion');
$this->fireModelEvent('savedNewVersion', $version);

return $this;
}
Expand Down Expand Up @@ -248,6 +248,7 @@ public function syncToCurrentVersionRecord(): HasRevisorContract|bool
if (! $this->currentVersionRecord) {
return $this->saveNewVersion();
}
$this->fireModelEvent('syncingToCurrentVersion', $this->currentVersionRecord);

$attributes = collect($this->attributes)
->except([$this->getKeyName(), 'version_number'])
Expand All @@ -263,6 +264,8 @@ public function syncToCurrentVersionRecord(): HasRevisorContract|bool
->update(['is_published' => 0]);
}

$this->fireModelEvent('syncedToCurrentVersion', $this->currentVersionRecord);

return $this;
}

Expand Down Expand Up @@ -354,6 +357,22 @@ public static function savedNewVersion(string|Closure $callback): void
static::registerModelEvent('savedNewVersion', $callback);
}

/**
* Register a "syncingToCurrentVersion" model event callback with the dispatcher.
*/
public static function syncingToCurrentVersion(string|Closure $callback): void
{
static::registerModelEvent('syncingToCurrentVersion', $callback);
}

/**
* Register a "syncedToCurrentVersion" model event callback with the dispatcher.
*/
public static function syncedToCurrentVersion(string|Closure $callback): void
{
static::registerModelEvent('syncedToCurrentVersion', $callback);
}

/**
* Register a "revertingToVersion" model event callback with the dispatcher.
*/
Expand Down

0 comments on commit 5656aa7

Please sign in to comment.