Skip to content

Commit 79bc628

Browse files
committed
Added published flag
1 parent ba0cde9 commit 79bc628

13 files changed

+481
-31
lines changed

README.md

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ Eloquent flagged attributes behavior.
1010
## Flags list
1111

1212
- Is Active
13-
- Is Published (todo)
14-
- Is Confirmed (todo)
13+
- Is Published
1514

1615
## Installation
1716

@@ -29,7 +28,9 @@ And then include the service provider within `app/config/app.php`.
2928
];
3029
```
3130

32-
## Setup a Model
31+
## Usage
32+
33+
### Setup an activatable model
3334

3435
```php
3536
<?php
@@ -45,6 +46,88 @@ class Post extends Model
4546
}
4647
```
4748

49+
### Available functions
50+
51+
#### Get only active models
52+
53+
```shell
54+
Post::all();
55+
Post::withoutInactive();
56+
```
57+
58+
#### Get only inactive models
59+
60+
```shell
61+
Post::onlyInactive();
62+
```
63+
64+
#### Get active + inactive models
65+
66+
```shell
67+
Post::withInactive();
68+
```
69+
70+
#### Activate model
71+
72+
```shell
73+
Post::where('id', 4)->activate();
74+
```
75+
76+
#### Deactivate model
77+
78+
```shell
79+
Post::where('id', 4)->deactivate();
80+
```
81+
82+
### Setup an publishable model
83+
84+
```php
85+
<?php
86+
87+
namespace App\Models;
88+
89+
use Cog\Flag\Traits\HasPublishedFlag;
90+
use Illuminate\Database\Eloquent\Model;
91+
92+
class Post extends Model
93+
{
94+
use HasPublishedFlag;
95+
}
96+
```
97+
98+
### Available functions
99+
100+
#### Get only published models
101+
102+
```shell
103+
Post::all();
104+
Post::withoutUnpublished();
105+
```
106+
107+
#### Get only unpublished models
108+
109+
```shell
110+
Post::onlyUnpublished();
111+
```
112+
113+
#### Get published + unpublished models
114+
115+
```shell
116+
Post::withUnpublished();
117+
```
118+
119+
#### Publish model
120+
121+
```shell
122+
Post::where('id', 4)->publish();
123+
```
124+
125+
#### Unpublish model
126+
127+
```shell
128+
Post::where('id', 4)->unpublish();
129+
```
130+
48131
## Testing
49132
50133
Run the tests with:
@@ -64,6 +147,11 @@ If you discover any security related issues, please email [email protected] in
64147
## Credits
65148
66149
- [Anton Komarev](https://github.com/a-komarev)
150+
- [All Contributors](../../contributors)
151+
152+
## Alternatives
153+
154+
*Not found.*
67155
68156
## License
69157

src/Scopes/ActiveFlagScope.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function addDeactivate(Builder $builder)
8585
/**
8686
* Add the with-inactive extension to the builder.
8787
*
88-
* @param \Illuminate\Database\Eloquent\Builder $builder
88+
* @param \Illuminate\Database\Eloquent\Builder $builder
8989
* @return void
9090
*/
9191
protected function addWithInactive(Builder $builder)
@@ -98,7 +98,7 @@ protected function addWithInactive(Builder $builder)
9898
/**
9999
* Add the without-inactive extension to the builder.
100100
*
101-
* @param \Illuminate\Database\Eloquent\Builder $builder
101+
* @param \Illuminate\Database\Eloquent\Builder $builder
102102
* @return void
103103
*/
104104
protected function addWithoutInactive(Builder $builder)
@@ -111,7 +111,7 @@ protected function addWithoutInactive(Builder $builder)
111111
/**
112112
* Add the only-inactive extension to the builder.
113113
*
114-
* @param \Illuminate\Database\Eloquent\Builder $builder
114+
* @param \Illuminate\Database\Eloquent\Builder $builder
115115
* @return void
116116
*/
117117
protected function addOnlyInactive(Builder $builder)

src/Scopes/PublishedFlagScope.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\Scope;
15+
use Illuminate\Database\Eloquent\Model;
16+
use Illuminate\Database\Eloquent\Builder;
17+
18+
/**
19+
* Class PublishedFlagScope.
20+
*
21+
* @package Cog\Flag\Scopes
22+
*/
23+
class PublishedFlagScope implements Scope
24+
{
25+
/**
26+
* All of the extensions to be added to the builder.
27+
*
28+
* @var array
29+
*/
30+
protected $extensions = ['Publish', 'Unpublish', 'WithUnpublished', 'WithoutUnpublished', 'OnlyUnpublished'];
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_published', 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 publish extension to the builder.
59+
*
60+
* @param \Illuminate\Database\Eloquent\Builder $builder
61+
* @return void
62+
*/
63+
protected function addPublish(Builder $builder)
64+
{
65+
$builder->macro('publish', function (Builder $builder) {
66+
$builder->withUnpublished();
67+
68+
return $builder->update(['is_published' => 1]);
69+
});
70+
}
71+
72+
/**
73+
* Add the unpublish extension to the builder.
74+
*
75+
* @param \Illuminate\Database\Eloquent\Builder $builder
76+
* @return void
77+
*/
78+
protected function addUnpublish(Builder $builder)
79+
{
80+
$builder->macro('unpublish', function (Builder $builder) {
81+
return $builder->update(['is_published' => 0]);
82+
});
83+
}
84+
85+
/**
86+
* Add the with-unpublished extension to the builder.
87+
*
88+
* @param \Illuminate\Database\Eloquent\Builder $builder
89+
* @return void
90+
*/
91+
protected function addWithUnpublished(Builder $builder)
92+
{
93+
$builder->macro('withUnpublished', function (Builder $builder) {
94+
return $builder->withoutGlobalScope($this);
95+
});
96+
}
97+
98+
/**
99+
* Add the without-unpublished extension to the builder.
100+
*
101+
* @param \Illuminate\Database\Eloquent\Builder $builder
102+
* @return void
103+
*/
104+
protected function addWithoutUnpublished(Builder $builder)
105+
{
106+
$builder->macro('withoutUnpublished', function (Builder $builder) {
107+
return $builder->withoutGlobalScope($this)->where('is_published', 1);
108+
});
109+
}
110+
111+
/**
112+
* Add the only-unpublished extension to the builder.
113+
*
114+
* @param \Illuminate\Database\Eloquent\Builder $builder
115+
* @return void
116+
*/
117+
protected function addOnlyUnpublished(Builder $builder)
118+
{
119+
$builder->macro('onlyUnpublished', function (Builder $builder) {
120+
return $builder->withoutGlobalScope($this)->where('is_published', 0);
121+
});
122+
}
123+
}

src/Traits/HasActiveFlag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
trait HasActiveFlag
2222
{
2323
/**
24-
* Boot the soft deleting trait for a model.
24+
* Boot the has active flag trait for a model.
2525
*
2626
* @return void
2727
*/

src/Traits/HasPublishedFlag.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\PublishedFlagScope;
15+
16+
/**
17+
* Class HasPublishedFlag.
18+
*
19+
* @package Cog\Flag\Traits
20+
*/
21+
trait HasPublishedFlag
22+
{
23+
/**
24+
* Boot the has published flag trait for a model.
25+
*
26+
* @return void
27+
*/
28+
public static function bootHasPublishedFlag()
29+
{
30+
static::addGlobalScope(new PublishedFlagScope);
31+
}
32+
}

tests/database/factories/EntityFactory.php renamed to tests/database/factories/EntityWithActiveFlagFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
$factory->define(\Cog\Flag\Tests\Stubs\Models\Entity::class, function (\Faker\Generator $faker) {
12+
$factory->define(\Cog\Flag\Tests\Stubs\Models\EntityWithActiveFlag::class, function (\Faker\Generator $faker) {
1313
return [
1414
'name' => $faker->word,
15+
'is_active' => true,
1516
];
1617
});
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\EntityWithPublishedFlag::class, function (\Faker\Generator $faker) {
13+
return [
14+
'name' => $faker->word,
15+
'is_published' => true,
16+
];
17+
});

tests/database/migrations/2016_09_25_182750_create_entity_table.php renamed to tests/database/migrations/2016_09_25_182750_create_entity_with_active_flag_table.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use Illuminate\Database\Migrations\Migration;
1313

1414
/**
15-
* Class CreateEntityTable.
15+
* Class CreateEntityWithActiveFlagTable.
1616
*/
17-
class CreateEntityTable extends Migration
17+
class CreateEntityWithActiveFlagTable extends Migration
1818
{
1919
/**
2020
* Run the migrations.
@@ -23,7 +23,7 @@ class CreateEntityTable extends Migration
2323
*/
2424
public function up()
2525
{
26-
Schema::create('entity', function ($table) {
26+
Schema::create('entity_with_active_flag', function ($table) {
2727
$table->increments('id');
2828
$table->string('name');
2929
$table->boolean('is_active');
@@ -38,6 +38,6 @@ public function up()
3838
*/
3939
public function down()
4040
{
41-
Schema::drop('entity');
41+
Schema::drop('entity_with_active_flag');
4242
}
4343
}

0 commit comments

Comments
 (0)