Skip to content

Commit 089f8be

Browse files
author
Pe Ell
authored
Published flag scope configurable auto apply (#26)
* Published flag scope configurable auto apply * Fix migrate command to 5.4 support. Drop 5.4 support till trait be fixed.
1 parent 215a3e5 commit 089f8be

9 files changed

+109
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to `laravel-eloquent-flag` will be documented in this file.
44

5+
## [3.8.0] - 2017-01-29
6+
7+
### Added
8+
9+
- `shouldApplyPublishedAtScope` & `shouldApplyPublishedFlagScope` methods to skip Published flags global scope auto apply.
10+
511
## [3.7.0] - 2017-01-14
612

713
### Added
@@ -121,6 +127,7 @@ All notable changes to `laravel-eloquent-flag` will be documented in this file.
121127

122128
- `is_active` boolean flag added.
123129

130+
[3.8.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.7.0...3.8.0
124131
[3.7.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.6.0...3.7.0
125132
[3.6.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.5.0...3.6.0
126133
[3.5.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.4.0...3.5.0

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"docs": "https://github.com/cybercog/laravel-eloquent-flag/wiki"
5353
},
5454
"require": {
55-
"illuminate/database": "~5.2.0|~5.3.0|~5.4.0",
55+
"illuminate/database": "~5.2.0|~5.3.0",
5656
"php": "^5.6|^7.0"
5757
},
5858
"require-dev": {

src/Scopes/Classic/PublishedAtScope.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class PublishedAtScope implements Scope
3939
*/
4040
public function apply(Builder $builder, Model $model)
4141
{
42+
if (method_exists($model, 'shouldApplyPublishedAtScope') && !$model->shouldApplyPublishedAtScope()) {
43+
return $builder;
44+
}
45+
4246
return $builder->whereNotNull('published_at');
4347
}
4448

src/Scopes/Classic/PublishedFlagScope.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class PublishedFlagScope implements Scope
3838
*/
3939
public function apply(Builder $builder, Model $model)
4040
{
41+
if (method_exists($model, 'shouldApplyPublishedFlagScope') && !$model->shouldApplyPublishedFlagScope()) {
42+
return $builder;
43+
}
44+
4145
return $builder->where('is_published', 1);
4246
}
4347

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function destroyPackageMigrations()
7070
protected function migrateUnitTestTables()
7171
{
7272
$this->artisan('migrate', [
73-
'--realpath' => realpath(__DIR__ . '/database/migrations'),
73+
'--path' => '../../../../tests/database/migrations',
7474
]);
7575
}
7676

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) Anton Komarev <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Cog\Flag\Tests\Stubs\Models\Classic;
13+
14+
/**
15+
* Class EntityWithPublishedAtUnapplied.
16+
*
17+
* @package Cog\Flag\Tests\Stubs\Models\Classic
18+
*/
19+
class EntityWithPublishedAtUnapplied extends EntityWithPublishedAt
20+
{
21+
/**
22+
* Determine if PublishedAtScope should be applied by default.
23+
*
24+
* @return bool
25+
*/
26+
public function shouldApplyPublishedAtScope()
27+
{
28+
return false;
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) Anton Komarev <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Cog\Flag\Tests\Stubs\Models\Classic;
13+
14+
/**
15+
* Class EntityWithPublishedFlagUnapplied.
16+
*
17+
* @package Cog\Flag\Tests\Stubs\Models\Classic
18+
*/
19+
class EntityWithPublishedFlagUnapplied extends EntityWithPublishedFlag
20+
{
21+
/**
22+
* Determine if PublishedFlagScope should be applied by default.
23+
*
24+
* @return bool
25+
*/
26+
public function shouldApplyPublishedFlagScope()
27+
{
28+
return false;
29+
}
30+
}

tests/unit/Scopes/Classic/PublishedAtScopeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Carbon\Carbon;
1515
use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithPublishedAt;
16+
use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithPublishedAtUnapplied;
1617
use Cog\Flag\Tests\TestCase;
1718

1819
/**
@@ -109,4 +110,19 @@ public function it_can_unpublish_model()
109110

110111
$this->assertNull($model->published_at);
111112
}
113+
114+
/** @test */
115+
public function it_can_skip_apply()
116+
{
117+
factory(EntityWithPublishedAt::class, 3)->create([
118+
'published_at' => Carbon::now()->subDay(),
119+
]);
120+
factory(EntityWithPublishedAt::class, 2)->create([
121+
'published_at' => null,
122+
]);
123+
124+
$entities = EntityWithPublishedAtUnapplied::all();
125+
126+
$this->assertCount(5, $entities);
127+
}
112128
}

tests/unit/Scopes/Classic/PublishedFlagScopeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Cog\Flag\Tests\Unit\Scopes\Classic;
1313

1414
use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithPublishedFlag;
15+
use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithPublishedFlagUnapplied;
1516
use Cog\Flag\Tests\TestCase;
1617

1718
/**
@@ -108,4 +109,19 @@ public function it_can_unpublish_model()
108109

109110
$this->assertFalse($model->is_published);
110111
}
112+
113+
/** @test */
114+
public function it_can_skip_apply()
115+
{
116+
factory(EntityWithPublishedFlag::class, 3)->create([
117+
'is_published' => true,
118+
]);
119+
factory(EntityWithPublishedFlag::class, 2)->create([
120+
'is_published' => false,
121+
]);
122+
123+
$entities = EntityWithPublishedFlagUnapplied::all();
124+
125+
$this->assertCount(5, $entities);
126+
}
111127
}

0 commit comments

Comments
 (0)