Skip to content

Commit a208839

Browse files
authored
Version 2 (#2)
* Non backwards compatible API changes * Allow the published status to be set from a boolean value * Better tests and more coverage
1 parent 1c7dc28 commit a208839

14 files changed

Lines changed: 778 additions & 4535 deletions

.github/workflows/run-tests.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Run tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
php: [7.4, 7.3, 7.2]
12+
laravel: [^7.0, ^6.0, ^5.8.0]
13+
dependency-versions: [prefer-lowest, prefer-stable]
14+
include:
15+
- laravel: ^7.0
16+
testbench: ^5.0
17+
- laravel: ^6.0
18+
testbench: ^4.0
19+
- laravel: ^5.8.0
20+
testbench: ^3.8.0
21+
22+
name: Laravel ${{ matrix.laravel }} | PHP ${{ matrix.php }} | ${{ matrix.dependency-versions }}
23+
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v1
27+
28+
- name: Cache dependencies
29+
uses: actions/cache@v1
30+
with:
31+
path: ~/.composer/cache/files
32+
key: dependencies-composer-${{ hashFiles('composer.json') }}-php-${{ matrix.php }}-laravel-${{ matrix.laravel }}
33+
34+
- name: Setup PHP
35+
uses: shivammathur/setup-php@v2
36+
with:
37+
php-version: ${{ matrix.php }}
38+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, mysql, mysqli, pdo_mysql, bcmath, soap, intl, gd, exif, iconv, imagick
39+
coverage: none
40+
41+
- name: Install dependencies
42+
run: |
43+
composer require "symfony/console:>=4.4" "illuminate/database:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
44+
composer update --${{ matrix.dependency-versions }} --prefer-dist --no-interaction --no-suggest
45+
46+
- name: Execute tests
47+
run: vendor/bin/phpunit

.gitignore

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
/.idea
2-
/vendor
3-
.phpunit.result.cache
1+
/.idea
2+
/.vscode
3+
/vendor
4+
.DS_Store
5+
.phpunit.result.cache
6+
composer.lock
7+
phpunit.xml

.styleci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preset: laravel

LICENSE.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
MIT License
2-
3-
Copyright (c) 2018 Optix Solutions <dev@optixsolutions.co.uk>
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
MIT License
2+
3+
Copyright (c) 2019 Optix Solutions <dev@optixsolutions.co.uk>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 105 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,105 @@
1-
# Laravel Draftable
2-
3-
Draft and publish your eloquent models.
4-
5-
```php
6-
// Get published posts.
7-
Post::all();
8-
9-
// Get all posts.
10-
Post::withDrafts()->get();
11-
12-
// Get drafted posts.
13-
Post::onlyDrafts()->get();
14-
```
15-
16-
## Installation
17-
18-
You can install the package via composer:
19-
20-
```bash
21-
composer require optix/draftable
22-
```
23-
24-
## Usage
25-
26-
Add the following column to your model's table:
27-
28-
```php
29-
$table->timestamp('published_at')->nullable();
30-
```
31-
32-
Then use the `Optix\Draftable\Draftable` trait in your model.
33-
34-
```php
35-
<?php
36-
37-
namespace App;
38-
39-
use Optix\Draftable\Draftable;
40-
use Illuminate\Database\Eloquent\Model;
41-
42-
class Post extends Model
43-
{
44-
use Draftable;
45-
}
46-
```
47-
48-
Posts are "published" when the `published_at` column is not null and in the past.
49-
50-
Posts are "drafted" when the `published_at` column is null or in the future.
51-
52-
```php
53-
Post::create([
54-
'published_at' => Carbon::now() // Published
55-
// Carbon::tomorrow() - Drafted until tomorrow
56-
// null - Indefinitely drafted
57-
]);
58-
```
59-
60-
## License
61-
62-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
1+
# Eloquent Draftable
2+
3+
![Packagist Version](https://img.shields.io/packagist/v/optix/eloquent-draftable)
4+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/optixsolutions/eloquent-draftable/Run%20tests)
5+
![StyleCI](https://styleci.io/repos/133484703/shield)
6+
7+
Add draftable functionality to your eloquent models.
8+
9+
## Installation
10+
11+
You can install this package via composer.
12+
13+
```bash
14+
composer require optix/eloquent-draftable
15+
```
16+
17+
## Setup
18+
19+
1. Add a nullable timestamp `published_at` column to your model's database table.
20+
21+
```php
22+
$table->timestamp('published_at')->nullable();
23+
```
24+
25+
2. Include the `Optix\Draftable\Draftable` trait in your model.
26+
27+
```php
28+
class Post extends Model
29+
{
30+
use Draftable;
31+
}
32+
```
33+
34+
## Usage
35+
36+
**Query scopes**
37+
38+
When the `Draftable` trait is included in a model, a global scope will be registered to automatically exclude
39+
draft records from query results. Therefore, in order to query draft records you must apply one of the local
40+
scopes outlined below.
41+
42+
```php
43+
// Only retrieve published records...
44+
$onlyPublished = Post::all();
45+
46+
// Retrieve draft & published records...
47+
$withDrafts = Post::withDrafts()->get();
48+
49+
// Only retrieve draft records...
50+
$onlyDrafts = Post::onlyDrafts()->get();
51+
```
52+
53+
**Publish a model**
54+
55+
```php
56+
$post = Post::withDrafts()->first();
57+
58+
// Publish without saving...
59+
$post->setPublished(true);
60+
61+
// Publish and save...
62+
$post->publish(); // or $post->publish(true);
63+
```
64+
65+
When you attempt to publish a model that's already been published, the `published_at` timestamp will not be updated.
66+
67+
**Draft a model**
68+
69+
```php
70+
// Draft without saving...
71+
$post->setPublished(false);
72+
73+
// Draft and save...
74+
$post->draft(); // or $post->publish(false);
75+
```
76+
77+
**Schedule a model to be published**
78+
79+
```php
80+
$publishDate = Carbon::now()->addWeek();
81+
// $publishDate = '2020-01-01 00:00:00';
82+
// $publishDate = '+1 week';
83+
84+
// Schedule without saving...
85+
$post->setPublishedAt($publishDate);
86+
87+
// Schedule and save...
88+
$post->publishAt($publishDate);
89+
```
90+
91+
The methods outlined above both require a `$date` parameter of type `DateTimeInterface|string|null`.
92+
93+
**Get the published status of a model**
94+
95+
```php
96+
// Determine if the model is published...
97+
$post->isPublished();
98+
99+
// Determine if the model is draft...
100+
$post->isDraft();
101+
```
102+
103+
## License
104+
105+
This package is licensed under the [MIT license](LICENSE.md).

composer.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"name": "optix/draftable",
2+
"name": "optix/eloquent-draftable",
3+
"description": "Add draftable functionality to your eloquent models.",
34
"license": "MIT",
45
"authors": [
56
{
@@ -8,10 +9,11 @@
89
}
910
],
1011
"require": {
11-
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0"
12+
"php": "^7.2",
13+
"illuminate/database": "^7.0|^6.0|~5.8.0"
1214
},
1315
"require-dev": {
14-
"orchestra/testbench": "^3.8|^4.0"
16+
"orchestra/testbench": "^5.0|^4.0|~3.8.0"
1517
},
1618
"autoload": {
1719
"psr-4": {
@@ -22,5 +24,10 @@
2224
"psr-4": {
2325
"Optix\\Draftable\\Tests\\": "tests/"
2426
}
25-
}
27+
},
28+
"config": {
29+
"sort-packages": true
30+
},
31+
"minimum-stability": "dev",
32+
"prefer-stable": true
2633
}

0 commit comments

Comments
 (0)