Skip to content

Commit 479564e

Browse files
author
Pe Ell
authored
Published at flag (#17)
Add PublishedAt timestamp flag
1 parent dccf78d commit 479564e

19 files changed

+529
-23
lines changed

CHANGELOG.md

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

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

5+
## 3.1.0 - 2017-01-11
6+
7+
### Added
8+
9+
- `Timestamp` flag types introduced.
10+
- `published_at` classic timestamp flag added.
11+
512
## 3.0.0 - 2017-01-07
613

714
### Added

README.md

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,46 @@
99

1010
## Introduction
1111

12-
Eloquent flagged attributes behavior. Enhance eloquent models with commonly used flags like `Active`, `Published`, `Approved` and other in a minutes!
12+
Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent models with commonly used state flags like `Active`, `Published`, `Approved` and others in a minutes!
1313

1414
## Features
1515

1616
- Designed to work with Laravel Eloquent models
1717
- Each model can has as many flags as required
1818
- Each flag adds global query scopes to models
19+
- 2 logical groups of flags: `Classic`, `Inverse`
20+
- 2 types of flags: `Boolean`, `Timestamp`
1921
- Covered with unit tests
2022

2123
## Available flags list
2224

23-
| Trait name | Logic | Database columns | Flag type |
24-
| ---------- | ----- | ---------------- | --------- |
25-
| `HasAcceptedFlag` | Classic | `is_accepted` | Boolean |
26-
| `HasActiveFlag` | Classic | `is_active` | Boolean |
27-
| `HasApprovedFlag` | Classic | `is_approved` | Boolean |
28-
| `HasClosedFlag` | Inverse | `is_closed` | Boolean |
29-
| `HasExpiredFlag` | Inverse | `is_expired` | Boolean |
30-
| `HasKeptFlag` | Classic | `is_kept` | Boolean |
31-
| `HasPublishedFlag` | Classic | `is_published` | Boolean |
32-
| `HasVerifiedFlag` | Classic | `is_verified` | Boolean |
25+
| Trait name | Logic | Database columns | Flag type | Conflict |
26+
| ---------- | ----- | ---------------- | --------- | -------- |
27+
| `HasAcceptedFlag` | Classic | `is_accepted` | Boolean | - |
28+
| `HasActiveFlag` | Classic | `is_active` | Boolean | - |
29+
| `HasApprovedFlag` | Classic | `is_approved` | Boolean | - |
30+
| `HasClosedFlag` | Inverse | `is_closed` | Boolean | - |
31+
| `HasExpiredFlag` | Inverse | `is_expired` | Boolean | - |
32+
| `HasKeptFlag` | Classic | `is_kept` | Boolean | - |
33+
| `HasPublishedAt` | Classic | `published_at` | Timestamp | `HasPublishedFlag` |
34+
| `HasPublishedFlag` | Classic | `is_published` | Boolean | `HasPublishedAt` |
35+
| `HasVerifiedFlag` | Classic | `is_verified` | Boolean | - |
36+
37+
Any entity can has more than one flag at the same time. If flags can't work for the same entity simultaneously they are listed in `Conflict` column.
3338

3439
## How it works
3540

3641
Eloquent Flag is an easy way to add flagged attributes to eloquent models. All flags has their own trait which adds global scopes to desired entity.
3742

43+
There are 2 types of flags:
44+
45+
- `Boolean` flags are the common ones. Stored in database as `BOOLEAN` or `TINYINT(1)` value.
46+
- `Timestamp` flags represented in database as nullable `TIMESTAMP` column. Useful when you need to know when action was performed.
47+
3848
All flags separated on 2 logical groups:
3949

40-
- `Classic` flags displays only entities with flag setted as `true`.
41-
- `Inverse` flags displays only entities with flag setted as `false`.
50+
- `Classic` flags displays only entities with `true` or `timestamp` flag value.
51+
- `Inverse` flags displays only entities with `false` or `null` flag value.
4252

4353
Omitted entities could be retrieved by using special global scope methods, unique for each flag.
4454

@@ -62,6 +72,40 @@ And then include the service provider within `app/config/app.php`.
6272

6373
## Usage
6474

75+
### Prepare database
76+
77+
#### Boolean flag
78+
79+
```php
80+
public function up()
81+
{
82+
Schema::create('post', function (Blueprint $table) {
83+
$table->increments('id');
84+
$table->string('title');
85+
$table->boolean('is_published');
86+
$table->timestamps();
87+
});
88+
}
89+
```
90+
91+
*Change `is_published` on any other `Boolean` flag database column name.*
92+
93+
#### Timestamp flag
94+
95+
```php
96+
public function up()
97+
{
98+
Schema::create('post', function (Blueprint $table) {
99+
$table->increments('id');
100+
$table->string('title');
101+
$table->timestamp('published_at')->nullable();
102+
$table->timestamps();
103+
});
104+
}
105+
```
106+
107+
*Change `published_at` on any other `Timestamp` flag database column name.*
108+
65109
### Setup an activatable model
66110

67111
```php
@@ -217,6 +261,8 @@ Post::where('id', 4)->disapprove();
217261

218262
### Setup a publishable model
219263

264+
#### With boolean flag
265+
220266
```php
221267
<?php
222268

@@ -233,6 +279,24 @@ class Post extends Model
233279

234280
*Model must have boolean `is_published` column in database table.*
235281

282+
#### With timestamp flag
283+
284+
```php
285+
<?php
286+
287+
namespace App\Models;
288+
289+
use Cog\Flag\Traits\Classic\HasPublishedAt;
290+
use Illuminate\Database\Eloquent\Model;
291+
292+
class Post extends Model
293+
{
294+
use HasPublishedAt;
295+
}
296+
```
297+
298+
*Model must have nullable timestamp `published_at` column in database table.*
299+
236300
### Available functions
237301

238302
#### Get only published models
@@ -500,7 +564,7 @@ Post::where('id', 4)->close();
500564
#### Remove close flag from model
501565

502566
```php
503-
Post::where('id', 4)->unclose();
567+
Post::where('id', 4)->open();
504568
```
505569

506570
## Testing

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cybercog/laravel-eloquent-flag",
3-
"description": "Laravel Eloquent flagged attributes behavior.",
3+
"description": "Laravel Eloquent boolean & timestamp flagged attributes behavior.",
44
"type": "library",
55
"license": "MIT",
66
"keywords": [
@@ -10,6 +10,8 @@
1010
"trait",
1111
"scopes",
1212
"flag",
13+
"state",
14+
"status",
1315
"boolean",
1416
"timestamp",
1517
"accepted",
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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\Scopes\Classic;
13+
14+
use Carbon\Carbon;
15+
use Illuminate\Database\Eloquent\Builder;
16+
use Illuminate\Database\Eloquent\Model;
17+
use Illuminate\Database\Eloquent\Scope;
18+
19+
/**
20+
* Class PublishedAtScope.
21+
*
22+
* @package Cog\Flag\Scopes\Classic
23+
*/
24+
class PublishedAtScope implements Scope
25+
{
26+
/**
27+
* All of the extensions to be added to the builder.
28+
*
29+
* @var array
30+
*/
31+
protected $extensions = ['Publish', 'Unpublish', 'WithUnpublished', 'WithoutUnpublished', 'OnlyUnpublished'];
32+
33+
/**
34+
* Apply the scope to a given Eloquent query builder.
35+
*
36+
* @param \Illuminate\Database\Eloquent\Builder $builder
37+
* @param \Illuminate\Database\Eloquent\Model $model
38+
* @return \Illuminate\Database\Eloquent\Builder
39+
*/
40+
public function apply(Builder $builder, Model $model)
41+
{
42+
return $builder->whereNotNull('published_at');
43+
}
44+
45+
/**
46+
* Extend the query builder with the needed functions.
47+
*
48+
* @param \Illuminate\Database\Eloquent\Builder $builder
49+
* @return void
50+
*/
51+
public function extend(Builder $builder)
52+
{
53+
foreach ($this->extensions as $extension) {
54+
$this->{"add{$extension}"}($builder);
55+
}
56+
}
57+
58+
/**
59+
* Add the `publish` extension to the builder.
60+
*
61+
* @param \Illuminate\Database\Eloquent\Builder $builder
62+
* @return void
63+
*/
64+
protected function addPublish(Builder $builder)
65+
{
66+
$builder->macro('publish', function (Builder $builder) {
67+
$builder->withUnpublished();
68+
69+
return $builder->update(['published_at' => Carbon::now()]);
70+
});
71+
}
72+
73+
/**
74+
* Add the `unpublish` extension to the builder.
75+
*
76+
* @param \Illuminate\Database\Eloquent\Builder $builder
77+
* @return void
78+
*/
79+
protected function addUnpublish(Builder $builder)
80+
{
81+
$builder->macro('unpublish', function (Builder $builder) {
82+
return $builder->update(['published_at' => null]);
83+
});
84+
}
85+
86+
/**
87+
* Add the `withUnpublished` extension to the builder.
88+
*
89+
* @param \Illuminate\Database\Eloquent\Builder $builder
90+
* @return void
91+
*/
92+
protected function addWithUnpublished(Builder $builder)
93+
{
94+
$builder->macro('withUnpublished', function (Builder $builder) {
95+
return $builder->withoutGlobalScope($this);
96+
});
97+
}
98+
99+
/**
100+
* Add the `withoutUnpublished` extension to the builder.
101+
*
102+
* @param \Illuminate\Database\Eloquent\Builder $builder
103+
* @return void
104+
*/
105+
protected function addWithoutUnpublished(Builder $builder)
106+
{
107+
$builder->macro('withoutUnpublished', function (Builder $builder) {
108+
return $builder->withoutGlobalScope($this)->whereNotNull('published_at');
109+
});
110+
}
111+
112+
/**
113+
* Add the `onlyUnpublished` extension to the builder.
114+
*
115+
* @param \Illuminate\Database\Eloquent\Builder $builder
116+
* @return void
117+
*/
118+
protected function addOnlyUnpublished(Builder $builder)
119+
{
120+
$builder->macro('onlyUnpublished', function (Builder $builder) {
121+
return $builder->withoutGlobalScope($this)->whereNull('published_at');
122+
});
123+
}
124+
}

src/Traits/Classic/HasPublishedAt.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\Traits\Classic;
13+
14+
/**
15+
* Class HasPublishedAt.
16+
*
17+
* @package Cog\Flag\Traits\Classic
18+
*/
19+
trait HasPublishedAt
20+
{
21+
use HasPublishedAtHelpers,
22+
HasPublishedAtScope;
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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\Traits\Classic;
13+
14+
/**
15+
* Class HasPublishedAtHelpers.
16+
*
17+
* @package Cog\Flag\Traits\Classic
18+
*/
19+
trait HasPublishedAtHelpers
20+
{
21+
//
22+
}
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) 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\Traits\Classic;
13+
14+
use Cog\Flag\Scopes\Classic\PublishedAtScope;
15+
16+
/**
17+
* Class HasPublishedAtScope.
18+
*
19+
* @package Cog\Flag\Traits\Classic
20+
*/
21+
trait HasPublishedAtScope
22+
{
23+
/**
24+
* Boot the HasPublishedAtScope trait for a model.
25+
*
26+
* @return void
27+
*/
28+
public static function bootHasPublishedAtScope()
29+
{
30+
static::addGlobalScope(new PublishedAtScope);
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) 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+
$factory->define(\Cog\Flag\Tests\Stubs\Models\Classic\EntityWithPublishedAt::class, function (\Faker\Generator $faker) {
13+
return [
14+
'name' => $faker->word,
15+
'published_at' => null,
16+
];
17+
});

0 commit comments

Comments
 (0)