Skip to content

Commit e359215

Browse files
author
Pe Ell
authored
Merge pull request #28 from cybercog/feature/skip-verify-scope-auto-apply
Add ability to skip auto apply Verified flags scopes
2 parents 9775674 + e27232c commit e359215

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
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.10.0] - 2017-02-13
6+
7+
### Added
8+
9+
- `shouldApplyVerifiedAtScope` & `shouldApplyVerifiedFlagScope` methods to skip Verified flags global scope auto apply.
10+
511
## [3.9.0] - 2017-02-03
612

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

134140
- `is_active` boolean flag added.
135141

142+
[3.10.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.9.0...3.10.0
136143
[3.9.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.8.0...3.9.0
137144
[3.8.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.7.0...3.8.0
138145
[3.7.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.6.0...3.7.0

src/Scopes/Classic/VerifiedAtScope.php

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

src/Scopes/Classic/VerifiedFlagScope.php

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

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 EntityWithVerifiedAtUnapplied.
16+
*
17+
* @package Cog\Flag\Tests\Stubs\Models\Classic
18+
*/
19+
class EntityWithVerifiedAtUnapplied extends EntityWithVerifiedAt
20+
{
21+
/**
22+
* Determine if VerifiedAtScope should be applied by default.
23+
*
24+
* @return bool
25+
*/
26+
public function shouldApplyVerifiedAtScope()
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 EntityWithVerifiedFlagUnapplied.
16+
*
17+
* @package Cog\Flag\Tests\Stubs\Models\Classic
18+
*/
19+
class EntityWithVerifiedFlagUnapplied extends EntityWithVerifiedFlag
20+
{
21+
/**
22+
* Determine if VerifiedFlagScope should be applied by default.
23+
*
24+
* @return bool
25+
*/
26+
public function shouldApplyVerifiedFlagScope()
27+
{
28+
return false;
29+
}
30+
}

tests/unit/Scopes/Classic/VerifiedAtScopeTest.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\EntityWithVerifiedAt;
16+
use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithVerifiedAtUnapplied;
1617
use Cog\Flag\Tests\TestCase;
1718

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

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

tests/unit/Scopes/Classic/VerifiedFlagScopeTest.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\EntityWithVerifiedFlag;
15+
use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithVerifiedFlagUnapplied;
1516
use Cog\Flag\Tests\TestCase;
1617

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

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

0 commit comments

Comments
 (0)