Skip to content

Commit

Permalink
Refactor and docblock
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack97 committed Sep 3, 2018
1 parent 00c6798 commit 1902c66
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
58 changes: 53 additions & 5 deletions src/Draftable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

trait Draftable
{
/**
* Register a global scope to only retrieve
* published models in query results.
*/
public static function bootDraftable()
{
static::addGlobalScope('published', function (Builder $query) {
Expand All @@ -15,41 +19,85 @@ public static function bootDraftable()
});
}

/**
* Scope to include draft models in query results.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*/
public function scopeWithDrafts(Builder $query)
{
$query->withoutGlobalScope('published');
}

/**
* Scope to only retrieve draft models in query results.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*/
public function scopeOnlyDrafts(Builder $query)
{
$query->withDrafts()
->where('published_at', '>', Carbon::now())
->orWhereNull('published_at');
}

public function isPublished()
{
return ! is_null($this->published_at) && $this->published_at <= now();
}

/**
* Publish the model.
*/
public function publish()
{
$this->schedule(Carbon::now());
}

/**
* Publish the model on a given date.
*
* @param \Carbon\Carbon $time
*/
public function schedule(Carbon $time)
{
$this->setPublishedAt($time);
}

/**
* Draft the model.
*/
public function draft()
{
$this->setPublishedAt(null);
}

/**
* Update the "published_at" column value.
*
* @param mixed $publishedAt
*/
protected function setPublishedAt($publishedAt)
{
$this->published_at = $publishedAt;
$this->save();
}

/**
* Determine if the model is published.
*
* @return bool
*/
public function isPublished()
{
return (
! is_null($this->published_at)
&& $this->published_at <= Carbon::now()
);
}

/**
* Determine if the model is draft.
*
* @return bool
*/
public function isDraft()
{
return ! $this->isPublished();
}
}
6 changes: 3 additions & 3 deletions tests/DraftableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function it_will_draft_a_model_on_save()
{
$model = TestModel::create(['title' => 'Test']);

$this->assertFalse($model->isPublished());
$this->assertTrue($model->isDraft());
}

/** @test */
Expand All @@ -35,7 +35,7 @@ public function it_can_publish_a_model_on_a_scheduled_date()
$model = TestModel::create(['title' => 'Scheduled']);
$model->schedule($date = Carbon::now()->addWeek());

$this->assertFalse($model->isPublished());
$this->assertTrue($model->isDraft());

Carbon::setTestNow($date);

Expand All @@ -52,7 +52,7 @@ public function it_can_draft_a_published_model()

$publishedModel->draft();

$this->assertFalse($publishedModel->isPublished());
$this->assertTrue($publishedModel->isDraft());
}

/** @test */
Expand Down
21 changes: 13 additions & 8 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ public function setUp()
{
parent::setUp();

$this->app['db']->connection()
->getSchemaBuilder()
->create('test_models', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamp('published_at')->nullable();
$table->timestamps();
});
$this->setUpDatabase($this->app);
}

protected function getEnvironmentSetUp($app)
Expand All @@ -30,4 +23,16 @@ protected function getEnvironmentSetUp($app)
'prefix' => ''
]);
}

protected function setUpDatabase($app)
{
$app['db']->connection()
->getSchemaBuilder()
->create('test_models', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamp('published_at')->nullable();
$table->timestamps();
});
}
}

0 comments on commit 1902c66

Please sign in to comment.