Skip to content

Commit 062da6a

Browse files
author
Pe Ell
authored
Add Accepted timestamp flag (#18)
1 parent 479564e commit 062da6a

11 files changed

+454
-2
lines changed

CHANGELOG.md

Lines changed: 6 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.2.0 - 2017-01-12
6+
7+
### Added
8+
9+
- `accepted_at` classic timestamp flag added.
10+
511
## 3.1.0 - 2017-01-11
612

713
### Added

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
2424

2525
| Trait name | Logic | Database columns | Flag type | Conflict |
2626
| ---------- | ----- | ---------------- | --------- | -------- |
27-
| `HasAcceptedFlag` | Classic | `is_accepted` | Boolean | - |
27+
| `HasAcceptedAt` | Classic | `accepted_at` | Timestamp | `HasAcceptedFlag` |
28+
| `HasAcceptedFlag` | Classic | `is_accepted` | Boolean | `HasAcceptedAt` |
2829
| `HasActiveFlag` | Classic | `is_active` | Boolean | - |
2930
| `HasApprovedFlag` | Classic | `is_approved` | Boolean | - |
3031
| `HasClosedFlag` | Inverse | `is_closed` | Boolean | - |
@@ -159,6 +160,8 @@ Post::where('id', 4)->deactivate();
159160

160161
### Setup an acceptable model
161162

163+
#### With boolean flag
164+
162165
```php
163166
<?php
164167

@@ -175,6 +178,24 @@ class Post extends Model
175178

176179
*Model must have boolean `is_accepted` column in database table.*
177180

181+
#### With timestamp flag
182+
183+
```php
184+
<?php
185+
186+
namespace App\Models;
187+
188+
use Cog\Flag\Traits\Classic\HasAcceptedAt;
189+
use Illuminate\Database\Eloquent\Model;
190+
191+
class Post extends Model
192+
{
193+
use HasAcceptedAt;
194+
}
195+
```
196+
197+
*Model must have nullable timestamp `accepted_at` column in database table.*
198+
178199
### Available functions
179200

180201
#### Get only accepted models
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 AcceptedAtScope.
21+
*
22+
* @package Cog\Flag\Scopes\Classic
23+
*/
24+
class AcceptedAtScope implements Scope
25+
{
26+
/**
27+
* All of the extensions to be added to the builder.
28+
*
29+
* @var array
30+
*/
31+
protected $extensions = ['Accept', 'Reject', 'WithRejected', 'WithoutRejected', 'OnlyRejected'];
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('accepted_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 `accept` extension to the builder.
60+
*
61+
* @param \Illuminate\Database\Eloquent\Builder $builder
62+
* @return void
63+
*/
64+
protected function addAccept(Builder $builder)
65+
{
66+
$builder->macro('accept', function (Builder $builder) {
67+
$builder->withRejected();
68+
69+
return $builder->update(['accepted_at' => Carbon::now()]);
70+
});
71+
}
72+
73+
/**
74+
* Add the `reject` extension to the builder.
75+
*
76+
* @param \Illuminate\Database\Eloquent\Builder $builder
77+
* @return void
78+
*/
79+
protected function addReject(Builder $builder)
80+
{
81+
$builder->macro('reject', function (Builder $builder) {
82+
return $builder->update(['accepted_at' => null]);
83+
});
84+
}
85+
86+
/**
87+
* Add the `withRejected` extension to the builder.
88+
*
89+
* @param \Illuminate\Database\Eloquent\Builder $builder
90+
* @return void
91+
*/
92+
protected function addWithRejected(Builder $builder)
93+
{
94+
$builder->macro('withRejected', function (Builder $builder) {
95+
return $builder->withoutGlobalScope($this);
96+
});
97+
}
98+
99+
/**
100+
* Add the `withoutRejected` extension to the builder.
101+
*
102+
* @param \Illuminate\Database\Eloquent\Builder $builder
103+
* @return void
104+
*/
105+
protected function addWithoutRejected(Builder $builder)
106+
{
107+
$builder->macro('withoutRejected', function (Builder $builder) {
108+
return $builder->withoutGlobalScope($this)->whereNotNull('accepted_at');
109+
});
110+
}
111+
112+
/**
113+
* Add the `onlyRejected` extension to the builder.
114+
*
115+
* @param \Illuminate\Database\Eloquent\Builder $builder
116+
* @return void
117+
*/
118+
protected function addOnlyRejected(Builder $builder)
119+
{
120+
$builder->macro('onlyRejected', function (Builder $builder) {
121+
return $builder->withoutGlobalScope($this)->whereNull('accepted_at');
122+
});
123+
}
124+
}

src/Traits/Classic/HasAcceptedAt.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 HasAcceptedAt.
16+
*
17+
* @package Cog\Flag\Traits\Classic
18+
*/
19+
trait HasAcceptedAt
20+
{
21+
use HasAcceptedAtHelpers,
22+
HasAcceptedAtScope;
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 HasAcceptedAtHelpers.
16+
*
17+
* @package Cog\Flag\Traits\Classic
18+
*/
19+
trait HasAcceptedAtHelpers
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\AcceptedAtScope;
15+
16+
/**
17+
* Class HasAcceptedAtScope.
18+
*
19+
* @package Cog\Flag\Traits\Classic
20+
*/
21+
trait HasAcceptedAtScope
22+
{
23+
/**
24+
* Boot the HasAcceptedAtScope trait for a model.
25+
*
26+
* @return void
27+
*/
28+
public static function bootHasAcceptedAtScope()
29+
{
30+
static::addGlobalScope(new AcceptedAtScope);
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\EntityWithAcceptedAt::class, function (\Faker\Generator $faker) {
13+
return [
14+
'name' => $faker->word,
15+
'accepted_at' => null,
16+
];
17+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
use Illuminate\Database\Migrations\Migration;
13+
use Illuminate\Database\Schema\Blueprint;
14+
use Illuminate\Support\Facades\Schema;
15+
16+
/**
17+
* Class CreateEntityWithAcceptedAtTable.
18+
*/
19+
class CreateEntityWithAcceptedAtTable extends Migration
20+
{
21+
/**
22+
* Run the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function up()
27+
{
28+
Schema::create('entity_with_accepted_at', function (Blueprint $table) {
29+
$table->increments('id');
30+
$table->string('name');
31+
$table->timestamp('accepted_at')->nullable();
32+
$table->timestamps();
33+
});
34+
}
35+
36+
/**
37+
* Reverse the migrations.
38+
*
39+
* @return void
40+
*/
41+
public function down()
42+
{
43+
Schema::dropIfExists('entity_with_accepted_at');
44+
}
45+
}
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) 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+
use Cog\Flag\Traits\Classic\HasAcceptedAt;
15+
use Illuminate\Database\Eloquent\Model;
16+
17+
/**
18+
* Class EntityWithAcceptedAt.
19+
*
20+
* @package Cog\Flag\Tests\Stubs\Models\Classic
21+
*/
22+
class EntityWithAcceptedAt extends Model
23+
{
24+
use HasAcceptedAt;
25+
26+
/**
27+
* The table associated with the model.
28+
*
29+
* @var string
30+
*/
31+
protected $table = 'entity_with_accepted_at';
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+
'accepted_at' => 'datetime',
49+
];
50+
}

0 commit comments

Comments
 (0)