Skip to content

Commit

Permalink
Default Settings (#20)
Browse files Browse the repository at this point in the history
* Default Settings
  • Loading branch information
glorand authored Aug 29, 2019
1 parent 4bf842a commit d9eb194
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `glorand/laravel-model-settings` will be documented in this file

## 3.3.0 - 2019-08-29
### Added
- Default settings on Model

## 3.2.0 - 2019-08-07
### Added
- Compatibility with PSR-16, CacheInterface
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<p align="center">
<a href="https://packagist.org/packages/glorand/laravel-model-settings">
<img src="https://poser.pugx.org/glorand/laravel-model-settings/v/stable" alt="Latest Stable Version">
</a>
<a href="https://packagist.org/packages/glorand/laravel-model-settings">
<img src="https://poser.pugx.org/glorand/laravel-model-settings/downloads" alt="Total Downloads">
</a>
<a href="https://travis-ci.com/glorand/laravel-model-settings">
<img src="https://travis-ci.com/glorand/laravel-model-settings.svg?branch=master" alt="Build Status">
Expand Down Expand Up @@ -37,6 +40,7 @@ Bug reports, feature requests, and pull requests can be submitted by following o
- [Updating your Eloquent Models](#update_models)
- [Option 1 - `HasSettingsField` trait](#update_models_1)
- [Option 2 - `HasSettingsTable` trait](#update_models_2)
- [Default Settings](#default_settings)
- [Usage](#usage)
- [Get all model's settings](#get_all)
- [Get a specific setting](#get)
Expand Down Expand Up @@ -109,6 +113,19 @@ class User extends Model
}
```

##Default settings <a name="default_settings"></a>

```php
use Glorand\Model\Settings\Traits\HasSettingsTable;

class User extends Model
{
public $defaultSettings = [
'key_1' => 'val_1',
];
}
```

## Usage <a name="usage"></a>

```php
Expand Down
7 changes: 5 additions & 2 deletions src/Managers/AbstractSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ abstract class AbstractSettingsManager implements SettingsManagerContract
/** @var \Illuminate\Database\Eloquent\Model */
protected $model;

/** @var array */
protected $defaultSettings = [];

/**
* AbstractSettingsManager constructor.
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Model|HasSettings $model
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function __construct(Model $model)
Expand All @@ -34,7 +37,7 @@ public function __construct(Model $model)
*/
public function all(): array
{
return $this->model->getSettingsValue();
return array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue());
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Traits/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

namespace Glorand\Model\Settings\Traits;

use Illuminate\Support\Arr;

trait HasSettings
{
public function getDefaultSettings(): array
{
if (property_exists($this, 'defaultSettings')) {
return Arr::wrap($this->defaultSettings);
}

return [];
}

/**
* @return array
*/
Expand Down
18 changes: 17 additions & 1 deletion tests/FieldSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class FieldSettingsManagerTest extends TestCase
'email' => "[email protected]",
],
];
/** @var array */
protected $defaultSettingsTestArray = [
'project' => 'Main Project',
];

public function setUp(): void
{
Expand All @@ -31,13 +35,25 @@ public function testInit()
}

/**
* @throws \Exception
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testAll()
{
$this->assertEquals($this->model->settings()->all(), []);
}

/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testDefaultValue()
{
$this->model->defaultSettings = $this->defaultSettingsTestArray;
$this->assertEquals($this->defaultSettingsTestArray, $this->model->settings()->all());

$this->model->settings()->apply($this->testArray);
$this->assertEquals($this->model->settings()->all(), array_merge($this->defaultSettingsTestArray, $this->testArray));
}

/**
* @expectedException \Glorand\Model\Settings\Exceptions\ModelSettingsException
* @expectedExceptionMessage Unknown field
Expand Down
2 changes: 2 additions & 0 deletions tests/Models/UserWithField.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ class UserWithField extends Model
protected $guarded = [];

protected $fillable = ['id', 'name'];

public $defaultSettings = [];
}
2 changes: 2 additions & 0 deletions tests/Models/UsersWithTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class UsersWithTable extends Model
protected $guarded = [];

protected $fillable = ['id', 'name'];

public $defaultSettings = [];
}
21 changes: 19 additions & 2 deletions tests/TableSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Glorand\Model\Settings\Tests;

use Glorand\Model\Settings\Models\ModelSettings;
use Glorand\Model\Settings\Tests\Models\UsersWithDefaultSettingsTable;
use Glorand\Model\Settings\Tests\Models\UsersWithTable as User;
use Glorand\Model\Settings\Traits\HasSettingsTable;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Facades\DB;

class TableSettingsManagerTest extends TestCase
{
/** @var \Glorand\Model\Settings\Tests\Models\UsersWithTable */
private $model;
/** @var array */
protected $testArray = [
'user' => [
Expand All @@ -18,8 +21,10 @@ class TableSettingsManagerTest extends TestCase
'email' => "[email protected]",
],
];
/** @var \Glorand\Model\Settings\Tests\Models\UsersWithTable */
private $model;
/** @var array */
protected $defaultSettingsTestArray = [
'project' => 'Main Project',
];

public function setUp(): void
{
Expand All @@ -41,6 +46,18 @@ public function testAll()
$this->assertEquals($this->model->settings()->all(), []);
}

/**
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testDefaultValue()
{
$this->model->defaultSettings = $this->defaultSettingsTestArray;
$this->assertEquals($this->defaultSettingsTestArray, $this->model->settings()->all());

$this->model->settings()->apply($this->testArray);
$this->assertEquals($this->model->settings()->all(), array_merge($this->defaultSettingsTestArray, $this->testArray));
}

/**
* @throws \Exception
*/
Expand Down

0 comments on commit d9eb194

Please sign in to comment.