From f73ceed0490ae0f0f84398a9ae5dc9dc67243a98 Mon Sep 17 00:00:00 2001 From: Lorand Gombos Date: Fri, 28 Jun 2019 15:28:52 +0300 Subject: [PATCH] Persistence for settings field (#5) * Use Facade instead of Helper * Persistence for settings field --- CHANGELOG.md | 4 ++++ README.md | 18 +++++++++++++++++ config/model_settings.php | 1 + src/Console/CreateSettingsFieldForModel.php | 2 +- src/Console/CreateSettingsTable.php | 2 +- src/Managers/FieldSettingsManager.php | 4 +++- src/Traits/HasSettingsField.php | 22 +++++++++++++++++++++ tests/FieldSettingsManagerTest.php | 13 +++++++++++- tests/Models/UserWithField.php | 2 ++ 9 files changed, 64 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 061fb42..d390f1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index d259387..b3fa34e 100644 --- a/README.md +++ b/README.md @@ -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) @@ -143,6 +144,23 @@ $user->settings()->has('some.setting'); $user->settings()->delete('some.setting'); ``` +#### Persistence for settings field +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 Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. diff --git a/config/model_settings.php b/config/model_settings.php index 5414462..02170bc 100644 --- a/config/model_settings.php +++ b/config/model_settings.php @@ -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), ]; diff --git a/src/Console/CreateSettingsFieldForModel.php b/src/Console/CreateSettingsFieldForModel.php index 788ed71..17a015d 100644 --- a/src/Console/CreateSettingsFieldForModel.php +++ b/src/Console/CreateSettingsFieldForModel.php @@ -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 { diff --git a/src/Console/CreateSettingsTable.php b/src/Console/CreateSettingsTable.php index 071e9db..e4036c8 100644 --- a/src/Console/CreateSettingsTable.php +++ b/src/Console/CreateSettingsTable.php @@ -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 { diff --git a/src/Managers/FieldSettingsManager.php b/src/Managers/FieldSettingsManager.php index 18727ff..7182806 100644 --- a/src/Managers/FieldSettingsManager.php +++ b/src/Managers/FieldSettingsManager.php @@ -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; } diff --git a/src/Traits/HasSettingsField.php b/src/Traits/HasSettingsField.php index 9602f1e..89a9d49 100644 --- a/src/Traits/HasSettingsField.php +++ b/src/Traits/HasSettingsField.php @@ -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 @@ -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); } @@ -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); } diff --git a/tests/FieldSettingsManagerTest.php b/tests/FieldSettingsManagerTest.php index 7cbb813..81fa7b7 100644 --- a/tests/FieldSettingsManagerTest.php +++ b/tests/FieldSettingsManagerTest.php @@ -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); } /** diff --git a/tests/Models/UserWithField.php b/tests/Models/UserWithField.php index bcdb005..02c5660 100644 --- a/tests/Models/UserWithField.php +++ b/tests/Models/UserWithField.php @@ -9,6 +9,8 @@ class UserWithField extends Model { use HasSettingsField; + //protected $persistSettings = true; + protected $table = 'users_with_field'; protected $guarded = [];