Skip to content

Commit 9030aab

Browse files
author
Pe Ell
authored
Merge pull request #3 from cybercog/feature/is-accepted
Add Accepted flag
2 parents 5d661ba + 782c148 commit 9030aab

8 files changed

+432
-0
lines changed

CHANGELOG.md

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

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

5+
## 1.3.0 - 2016-12-14
6+
7+
- `is_accepted` flag added.
8+
59
## 1.2.0 - 2016-12-10
610

711
- `is_kept` flag added.

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The main logic of the flags: If flag is `false` - entity should be hidden from t
1717

1818
## Available flags list
1919

20+
- `is_accepted`
2021
- `is_active`
2122
- `is_published`
2223
- `is_kept`
@@ -90,6 +91,57 @@ Post::where('id', 4)->activate();
9091
Post::where('id', 4)->deactivate();
9192
```
9293
94+
### Setup an acceptable model
95+
96+
```php
97+
<?php
98+
99+
namespace App\Models;
100+
101+
use Cog\Flag\Traits\HasAcceptedFlag;
102+
use Illuminate\Database\Eloquent\Model;
103+
104+
class Post extends Model
105+
{
106+
use HasAcceptedFlag;
107+
}
108+
```
109+
110+
*Model must have boolean `is_accepted` column in database table.*
111+
112+
### Available functions
113+
114+
#### Get only accepted models
115+
116+
```shell
117+
Post::all();
118+
Post::withoutUnaccepted();
119+
```
120+
121+
#### Get only unaccepted models
122+
123+
```shell
124+
Post::onlyUnaccepted();
125+
```
126+
127+
#### Get accepted + unaccepted models
128+
129+
```shell
130+
Post::withUnaccepted();
131+
```
132+
133+
#### Accept model
134+
135+
```shell
136+
Post::where('id', 4)->accept();
137+
```
138+
139+
#### Deactivate model
140+
141+
```shell
142+
Post::where('id', 4)->unaccept();
143+
```
144+
93145
### Setup a publishable model
94146
95147
```php

src/Scopes/AcceptedFlagScope.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <[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\Scopes;
13+
14+
use Illuminate\Database\Eloquent\Model;
15+
use Illuminate\Database\Eloquent\Scope;
16+
use Illuminate\Database\Eloquent\Builder;
17+
18+
/**
19+
* Class AcceptedFlagScope.
20+
*
21+
* @package Cog\Flag\Scopes
22+
*/
23+
class AcceptedFlagScope implements Scope
24+
{
25+
/**
26+
* All of the extensions to be added to the builder.
27+
*
28+
* @var array
29+
*/
30+
protected $extensions = ['Accept', 'Unaccept', 'WithUnaccepted', 'WithoutUnaccepted', 'OnlyUnaccepted'];
31+
32+
/**
33+
* Apply the scope to a given Eloquent query builder.
34+
*
35+
* @param \Illuminate\Database\Eloquent\Builder $builder
36+
* @param \Illuminate\Database\Eloquent\Model $model
37+
* @return \Illuminate\Database\Eloquent\Builder
38+
*/
39+
public function apply(Builder $builder, Model $model)
40+
{
41+
return $builder->where('is_accepted', 1);
42+
}
43+
44+
/**
45+
* Extend the query builder with the needed functions.
46+
*
47+
* @param \Illuminate\Database\Eloquent\Builder $builder
48+
* @return void
49+
*/
50+
public function extend(Builder $builder)
51+
{
52+
foreach ($this->extensions as $extension) {
53+
$this->{"add{$extension}"}($builder);
54+
}
55+
}
56+
57+
/**
58+
* Add the `accept` extension to the builder.
59+
*
60+
* @param \Illuminate\Database\Eloquent\Builder $builder
61+
* @return void
62+
*/
63+
protected function addAccept(Builder $builder)
64+
{
65+
$builder->macro('accept', function (Builder $builder) {
66+
$builder->withUnaccepted();
67+
68+
return $builder->update(['is_accepted' => 1]);
69+
});
70+
}
71+
72+
/**
73+
* Add the `unaccept` extension to the builder.
74+
*
75+
* @param \Illuminate\Database\Eloquent\Builder $builder
76+
* @return void
77+
*/
78+
protected function addUnaccept(Builder $builder)
79+
{
80+
$builder->macro('unaccept', function (Builder $builder) {
81+
return $builder->update(['is_accepted' => 0]);
82+
});
83+
}
84+
85+
/**
86+
* Add the `withUnaccepted` extension to the builder.
87+
*
88+
* @param \Illuminate\Database\Eloquent\Builder $builder
89+
* @return void
90+
*/
91+
protected function addWithUnaccepted(Builder $builder)
92+
{
93+
$builder->macro('withUnaccepted', function (Builder $builder) {
94+
return $builder->withoutGlobalScope($this);
95+
});
96+
}
97+
98+
/**
99+
* Add the `withoutUnaccepted` extension to the builder.
100+
*
101+
* @param \Illuminate\Database\Eloquent\Builder $builder
102+
* @return void
103+
*/
104+
protected function addWithoutUnaccepted(Builder $builder)
105+
{
106+
$builder->macro('withoutUnaccepted', function (Builder $builder) {
107+
return $builder->withoutGlobalScope($this)->where('is_accepted', 1);
108+
});
109+
}
110+
111+
/**
112+
* Add the `onlyUnaccepted` extension to the builder.
113+
*
114+
* @param \Illuminate\Database\Eloquent\Builder $builder
115+
* @return void
116+
*/
117+
protected function addOnlyUnaccepted(Builder $builder)
118+
{
119+
$builder->macro('onlyUnaccepted', function (Builder $builder) {
120+
return $builder->withoutGlobalScope($this)->where('is_accepted', 0);
121+
});
122+
}
123+
}

src/Traits/HasAcceptedFlag.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <[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\Traits;
13+
14+
use Cog\Flag\Scopes\AcceptedFlagScope;
15+
16+
/**
17+
* Class HasAcceptedFlag.
18+
*
19+
* @package Cog\Flag\Traits
20+
*/
21+
trait HasAcceptedFlag
22+
{
23+
/**
24+
* Boot the has accepted flag trait for a model.
25+
*
26+
* @return void
27+
*/
28+
public static function bootHasAcceptedFlag()
29+
{
30+
static::addGlobalScope(new AcceptedFlagScope);
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <[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+
$factory->define(\Cog\Flag\Tests\Stubs\Models\EntityWithAcceptedFlag::class, function (\Faker\Generator $faker) {
13+
return [
14+
'name' => $faker->word,
15+
'is_accepted' => true,
16+
];
17+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <[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+
use Illuminate\Database\Migrations\Migration;
13+
14+
/**
15+
* Class CreateEntityWithAcceptedFlagTable.
16+
*/
17+
class CreateEntityWithAcceptedFlagTable extends Migration
18+
{
19+
/**
20+
* Run the migrations.
21+
*
22+
* @return void
23+
*/
24+
public function up()
25+
{
26+
Schema::create('entity_with_accepted_flag', function ($table) {
27+
$table->increments('id');
28+
$table->string('name');
29+
$table->boolean('is_accepted');
30+
$table->timestamps();
31+
});
32+
}
33+
34+
/**
35+
* Reverse the migrations.
36+
*
37+
* @return void
38+
*/
39+
public function down()
40+
{
41+
Schema::drop('entity_with_accepted_flag');
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <[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;
13+
14+
use Cog\Flag\Traits\HasAcceptedFlag;
15+
use Illuminate\Database\Eloquent\Model;
16+
17+
/**
18+
* Class EntityWithAcceptedFlag.
19+
*
20+
* @package Cog\Flag\Tests\Stubs\Models
21+
*/
22+
class EntityWithAcceptedFlag extends Model
23+
{
24+
use HasAcceptedFlag;
25+
26+
/**
27+
* The table associated with the model.
28+
*
29+
* @var string
30+
*/
31+
protected $table = 'entity_with_accepted_flag';
32+
33+
/**
34+
* The attributes that are mass assignable.
35+
*
36+
* @var array
37+
*/
38+
protected $fillable = [
39+
'name',
40+
];
41+
42+
/**
43+
* The attributes that should be cast to native types.
44+
*
45+
* @var array
46+
*/
47+
protected $casts = [
48+
'is_accepted' => 'bool',
49+
];
50+
}

0 commit comments

Comments
 (0)