Skip to content

Commit

Permalink
feat: allows publishing an unpublishing to not fire model events usin…
Browse files Browse the repository at this point in the history
…g new functions.
  • Loading branch information
jaymeh committed Sep 27, 2022
1 parent 6f57301 commit 4cd635c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ $post->publish();

// Unpublish the post
$post->unpublish();

// Publish the post without firing model events
$post->publishQuietly();

// Unpublish the post without firing model events
$post->unpublishQuietly();
```

A post is considered published when the `published_at` is not null and in the past.
Expand Down
26 changes: 25 additions & 1 deletion src/Publishable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Database\Eloquent\Builder;

trait Publishable
{
{
/**
* Scope a query to only include published models.
*
Expand Down Expand Up @@ -59,6 +59,18 @@ public function publish()
return $this->save();
}

/**
* Publishes the model without firing events.
*
* @return bool
*/
public function publishQuietly()
{
$this->published_at = Carbon::now();

return $this->saveQuietly();
}

/**
* @return bool
*/
Expand All @@ -69,6 +81,18 @@ public function unpublish()
return $this->save();
}

/**
* Unpublishes the model without firing events.
*
* @return bool
*/
public function unpublishQuietly()
{
$this->published_at = null;

return $this->saveQuietly();
}

/**
* @param mixed $value
* @return \Carbon\Carbon
Expand Down
41 changes: 41 additions & 0 deletions tests/PublishableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PawelMysior\Publishable\Tests;

use Carbon\Carbon;
use Illuminate\Support\Facades\Event;

class PublishableTest extends TestCase
{
Expand Down Expand Up @@ -132,4 +133,44 @@ public function test_published_at_field_is_not_fillable()
$post = new Post();
$this->assertNotContains('published_at', $post->getFillable());
}

/**
* @test
*/
public function test_publish_quietly_function_does_not_fire_model_events()
{
// Fake events so we can test on them.
Event::fake();

// Create a post.
$post = Post::create([
'published_at' => Carbon::yesterday(),
]);

// Run publish Quietly Function.
$post->publishQuietly();

// Assert it doesn't trigger the updated event.
Event::assertNotDispatched('eloquent.updated: PawelMysior\Publishable\Tests\Post');
}

/**
* @test
*/
public function test_unpublish_quietly_function_does_not_fire_model_events()
{
// Fake events so we can test on them.
Event::fake();

// Create a post.
$post = Post::create([
'published_at' => Carbon::yesterday(),
]);

// Run publish Quietly Function.
$post->unpublishQuietly();

// Assert it doesn't trigger the updated event.
Event::assertNotDispatched('eloquent.updated: PawelMysior\Publishable\Tests\Post');
}
}

0 comments on commit 4cd635c

Please sign in to comment.