Skip to content

Commit

Permalink
Persistence for settings field (#5)
Browse files Browse the repository at this point in the history
* Use Facade instead of Helper
* Persistence for settings field
  • Loading branch information
glorand authored Jun 28, 2019
1 parent 4fa21c0 commit f73ceed
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 4 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.1.0 - 2019-06-28
### Added
- Configure persistence for settings in case of Field Type (HasSettingsField)

## 3.0.0 - 2019-03-21
### Added
- Command to create the table for settings
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Bug reports, feature requests, and pull requests can be submitted by following o
- [Add / Update setting](#add_update)
- [Check if the model has a specific setting](#check)
- [Remove a setting from a model](#remove)
- [Persistence](#persistence)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -143,6 +144,23 @@ $user->settings()->has('some.setting');
$user->settings()->delete('some.setting');
```

#### Persistence for settings field <a name="persistence"></a>
In case of field settings the auto-save is configurable

- Use an attribute on model
```php
protected $persistSettings = true; //boolean
```
- Environment (.env) variable
```dotenv
MODEL_SETTINGS_PERSISTENT=true
```
- Config value - model settings config file
```php
'settings_persistent' => env('MODEL_SETTINGS_PERSISTENT', false),
```
If the persistence is `false` you have to save the model after the operation.

## Changelog <a name="changelog"></a>
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Expand Down
1 change: 1 addition & 0 deletions config/model_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
return [
'settings_field_name' => env('MODEL_SETTINGS_FIELD_NAME', 'settings'),
'settings_table_name' => env('MODEL_SETTINGS_TABLE_NAME', 'model_settings'),
'settings_persistent' => env('MODEL_SETTINGS_PERSISTENT', true),
];
2 changes: 1 addition & 1 deletion src/Console/CreateSettingsFieldForModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Schema;
use Illuminate\Support\Facades\Schema;

class CreateSettingsFieldForModel extends Command
{
Expand Down
2 changes: 1 addition & 1 deletion src/Console/CreateSettingsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Schema;
use Illuminate\Support\Facades\Schema;

class CreateSettingsTable extends Command
{
Expand Down
4 changes: 3 additions & 1 deletion src/Managers/FieldSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class FieldSettingsManager extends AbstractSettingsManager
public function apply(array $settings = []): SettingsManagerContract
{
$this->model->{$this->model->getSettingsFieldName()} = json_encode($settings);
$this->model->save();
if ($this->model->isPersistSettings()) {
$this->model->save();
}

return $this;
}
Expand Down
22 changes: 22 additions & 0 deletions src/Traits/HasSettingsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
* @package Glorand\Model\Settings\Traits
* @property array $settings
* @property string $settingsFieldName
* @property boolean $persistSettings
*/
trait HasSettingsField
{
use HasSettings;

private $persistSettings = null;

/**
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
* @throws ModelSettingsException
Expand All @@ -36,6 +39,7 @@ public function getSettingsValue(): array
if (!array_has($attributes, $settingsFieldName)) {
throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}");
}

return json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true);
}

Expand All @@ -47,7 +51,25 @@ public function getSettingsFieldName(): string
return $this->settingsFieldName ?? config('model_settings.settings_field_name');
}

/**
* @return bool
*/
public function isPersistSettings(): bool
{
return boolval($this->persistSettings ?? config('model_settings.settings_persistent'));
}

/**
* @param bool $val
*/
public function setPersistSettings(bool $val = true)
{
$this->persistSettings = $val;
}

abstract public function getTable();

abstract public function getAttributes();

abstract public function getAttributeValue($key);
}
13 changes: 12 additions & 1 deletion tests/FieldSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ public function testGet()
public function testApply()
{
$this->model->settings()->apply($this->testArray);
$this->assertEquals($this->model->settings()->all(), $this->testArray);
$this->assertEquals($this->model->fresh()->settings()->all(), $this->testArray);

$this->model->settings()->delete();

$this->model->setPersistSettings(false);
$this->model->settings()->apply($this->testArray);
$this->assertEquals($this->model->fresh()->settings()->all(), []);

$this->model->fresh();
$this->model->setPersistSettings(true);
$this->model->settings()->apply($this->testArray);
$this->assertEquals($this->model->fresh()->settings()->all(), $this->testArray);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Models/UserWithField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class UserWithField extends Model
{
use HasSettingsField;

//protected $persistSettings = true;

protected $table = 'users_with_field';

protected $guarded = [];
Expand Down

0 comments on commit f73ceed

Please sign in to comment.