Skip to content

Commit 1bb71b9

Browse files
author
Pe Ell
authored
Merge pull request #1 from cybercog/feature/is-kept-flag
Add Kept flag
2 parents 238c13c + 985f8e6 commit 1bb71b9

14 files changed

+580
-24
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.2.0 - 2016-12-10
6+
7+
- `is_kept` flag added.
8+
59
## 1.1.0 - 2016-09-25
610

711
- `is_published` flag added.

README.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
[![Releases](https://img.shields.io/github/release/cybercog/laravel-eloquent-flag.svg?style=flat-square)](https://github.com/cybercog/laravel-eloquent-flag/releases)
66
[![License](https://img.shields.io/github/license/cybercog/laravel-eloquent-flag.svg?style=flat-square)](https://github.com/cybercog/laravel-eloquent-flag/blob/master/LICENSE)
77

8-
Eloquent flagged attributes behavior.
8+
Eloquent flagged attributes behavior. Add commonly used flags to models very quick and easy.
99

1010
## Available flags list
1111

1212
- `is_active`
1313
- `is_published`
14+
- `is_kept`
1415

1516
## Installation
1617

@@ -132,6 +133,87 @@ Post::where('id', 4)->publish();
132133
Post::where('id', 4)->unpublish();
133134
```
134135
136+
### Setup a keepable model
137+
138+
Keep functionality required when you are trying to attach related models before parent one isn't saved.
139+
140+
**Issue:**
141+
142+
1. User press `Create Post` button.
143+
2. Create post form has image uploader.
144+
3. On image uploading user can't attach image to post before it wouldn't been stored in database.
145+
146+
**Solution:**
147+
148+
1. Add `HasKeptFlag` trait to model (and add boolean `is_kept` column to database).
149+
2. Create empty model on form loading (it will has `is_kept = 0` by default).
150+
3. Feel free to add any relations to the model.
151+
152+
*Model will be marked as required to be kept as soon as client will save\update model for the first time.*
153+
154+
*Remove the unkept models on a predetermined schedule (once a week for example).*
155+
156+
```php
157+
<?php
158+
159+
namespace App\Models;
160+
161+
use Cog\Flag\Traits\HasKeptFlag;
162+
use Illuminate\Database\Eloquent\Model;
163+
164+
class Post extends Model
165+
{
166+
use HasKeptFlag;
167+
}
168+
```
169+
170+
Your model is now can be marked to be kept!
171+
172+
*Model must have boolean `is_kept` column in database table.*
173+
174+
By default all records that have a `is_kept` equals to 0 will be excluded from your query results. To include unkept records, all you need to do is call the `withUnkept()` method on your query.
175+
176+
### Available functions
177+
178+
#### Get only kept models
179+
180+
```shell
181+
Post::all();
182+
Post::withoutUnkept();
183+
```
184+
185+
#### Get only unkept models
186+
187+
```shell
188+
Post::onlyUnkept();
189+
```
190+
191+
#### Get kept + unkept models
192+
193+
```shell
194+
Post::withUnkept();
195+
```
196+
197+
#### Keep model
198+
199+
```shell
200+
Post::where('id', 4)->keep();
201+
```
202+
203+
#### Unkeep model
204+
205+
```shell
206+
Post::where('id', 4)->unkeep();
207+
```
208+
209+
#### Get unkept models which older than hours
210+
211+
```shell
212+
Post::onlyUnkeptOlderThanHours(4);
213+
```
214+
215+
Output will have all unkept models created earlier than 4 hours ago.
216+
135217
## Testing
136218
137219
Run the tests with:

src/Scopes/ActiveFlagScope.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Cog\Flag\Scopes;
1313

14-
use Illuminate\Database\Eloquent\Scope;
1514
use Illuminate\Database\Eloquent\Model;
15+
use Illuminate\Database\Eloquent\Scope;
1616
use Illuminate\Database\Eloquent\Builder;
1717

1818
/**
@@ -55,7 +55,7 @@ public function extend(Builder $builder)
5555
}
5656

5757
/**
58-
* Add the activate extension to the builder.
58+
* Add the `activate` extension to the builder.
5959
*
6060
* @param \Illuminate\Database\Eloquent\Builder $builder
6161
* @return void
@@ -70,7 +70,7 @@ protected function addActivate(Builder $builder)
7070
}
7171

7272
/**
73-
* Add the deactivate extension to the builder.
73+
* Add the `deactivate` extension to the builder.
7474
*
7575
* @param \Illuminate\Database\Eloquent\Builder $builder
7676
* @return void
@@ -83,7 +83,7 @@ protected function addDeactivate(Builder $builder)
8383
}
8484

8585
/**
86-
* Add the with-inactive extension to the builder.
86+
* Add the `withInactive` extension to the builder.
8787
*
8888
* @param \Illuminate\Database\Eloquent\Builder $builder
8989
* @return void
@@ -96,7 +96,7 @@ protected function addWithInactive(Builder $builder)
9696
}
9797

9898
/**
99-
* Add the without-inactive extension to the builder.
99+
* Add the `withoutInactive` extension to the builder.
100100
*
101101
* @param \Illuminate\Database\Eloquent\Builder $builder
102102
* @return void
@@ -109,7 +109,7 @@ protected function addWithoutInactive(Builder $builder)
109109
}
110110

111111
/**
112-
* Add the only-inactive extension to the builder.
112+
* Add the `onlyInactive` extension to the builder.
113113
*
114114
* @param \Illuminate\Database\Eloquent\Builder $builder
115115
* @return void

src/Scopes/KeptFlagScope.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 KeptFlagScope.
20+
*
21+
* @package Cog\Flag\Scopes
22+
*/
23+
class KeptFlagScope implements Scope
24+
{
25+
/**
26+
* All of the extensions to be added to the builder.
27+
*
28+
* @var array
29+
*/
30+
protected $extensions = ['Keep', 'Unkeep', 'WithUnkept', 'WithoutUnkept', 'OnlyUnkept'];
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_kept', 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 `keep` extension to the builder.
59+
*
60+
* @param \Illuminate\Database\Eloquent\Builder $builder
61+
* @return void
62+
*/
63+
protected function addKeep(Builder $builder)
64+
{
65+
$builder->macro('keep', function (Builder $builder) {
66+
$builder->withUnkept();
67+
68+
return $builder->update(['is_kept' => 1]);
69+
});
70+
}
71+
72+
/**
73+
* Add the `unkeep` extension to the builder.
74+
*
75+
* @param \Illuminate\Database\Eloquent\Builder $builder
76+
* @return void
77+
*/
78+
protected function addUnkeep(Builder $builder)
79+
{
80+
$builder->macro('unkeep', function (Builder $builder) {
81+
return $builder->update(['is_kept' => 0]);
82+
});
83+
}
84+
85+
/**
86+
* Add the `withUnkept` extension to the builder.
87+
*
88+
* @param \Illuminate\Database\Eloquent\Builder $builder
89+
* @return void
90+
*/
91+
protected function addWithUnkept(Builder $builder)
92+
{
93+
$builder->macro('withUnkept', function (Builder $builder) {
94+
return $builder->withoutGlobalScope($this);
95+
});
96+
}
97+
98+
/**
99+
* Add the `withoutUnkept` extension to the builder.
100+
*
101+
* @param \Illuminate\Database\Eloquent\Builder $builder
102+
* @return void
103+
*/
104+
protected function addWithoutUnkept(Builder $builder)
105+
{
106+
$builder->macro('withoutUnkept', function (Builder $builder) {
107+
return $builder->withoutGlobalScope($this)->where('is_kept', 1);
108+
});
109+
}
110+
111+
/**
112+
* Add the `onlyUnkept` extension to the builder.
113+
*
114+
* @param \Illuminate\Database\Eloquent\Builder $builder
115+
* @return void
116+
*/
117+
protected function addOnlyUnkept(Builder $builder)
118+
{
119+
$builder->macro('onlyUnkept', function (Builder $builder) {
120+
return $builder->withoutGlobalScope($this)->where('is_kept', 0);
121+
});
122+
}
123+
}

src/Scopes/PublishedFlagScope.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Cog\Flag\Scopes;
1313

14-
use Illuminate\Database\Eloquent\Scope;
1514
use Illuminate\Database\Eloquent\Model;
15+
use Illuminate\Database\Eloquent\Scope;
1616
use Illuminate\Database\Eloquent\Builder;
1717

1818
/**
@@ -55,7 +55,7 @@ public function extend(Builder $builder)
5555
}
5656

5757
/**
58-
* Add the publish extension to the builder.
58+
* Add the `publish` extension to the builder.
5959
*
6060
* @param \Illuminate\Database\Eloquent\Builder $builder
6161
* @return void
@@ -70,7 +70,7 @@ protected function addPublish(Builder $builder)
7070
}
7171

7272
/**
73-
* Add the unpublish extension to the builder.
73+
* Add the `unpublish` extension to the builder.
7474
*
7575
* @param \Illuminate\Database\Eloquent\Builder $builder
7676
* @return void
@@ -83,7 +83,7 @@ protected function addUnpublish(Builder $builder)
8383
}
8484

8585
/**
86-
* Add the with-unpublished extension to the builder.
86+
* Add the `withUnpublished` extension to the builder.
8787
*
8888
* @param \Illuminate\Database\Eloquent\Builder $builder
8989
* @return void
@@ -96,7 +96,7 @@ protected function addWithUnpublished(Builder $builder)
9696
}
9797

9898
/**
99-
* Add the without-unpublished extension to the builder.
99+
* Add the `withoutUnpublished` extension to the builder.
100100
*
101101
* @param \Illuminate\Database\Eloquent\Builder $builder
102102
* @return void
@@ -109,7 +109,7 @@ protected function addWithoutUnpublished(Builder $builder)
109109
}
110110

111111
/**
112-
* Add the only-unpublished extension to the builder.
112+
* Add the `onlyUnpublished` extension to the builder.
113113
*
114114
* @param \Illuminate\Database\Eloquent\Builder $builder
115115
* @return void

0 commit comments

Comments
 (0)