diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a2ca2e..163ced2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to `qcod/laravel-app-settings` will be documented in this file +## 1.1.0 - 2019-08-14 + +- Group Settings by name ## 1.0.2 - 2016-10-10 diff --git a/README.md b/README.md index 978e83f..a44b4bf 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,25 @@ If your app needs different url to access the settings page you can change from // http://yourapp.com/app-settings ``` +### Use Group Setting + +Many time you want to store settings in a group. With version `1.1` you can define a group name from your `config/app_settings.php`. You have a closer to return the name of group as string + +```php +return [ + + // All the sections for the settings page + 'sections' => [...] + + ... + // settings group + 'setting_group' => function() { + return 'user_'.auth()->id(); + } +``` + +In this case you can have different settings for each user. + ### Use without UI If you want to just store the key-value pair into DB and don't want the UI to manage settings for example in API? You should use [qcod/laravel-settings package](https://github.com/qcod/laravel-settings) instead. This package uses it under the hood to persist the settings. diff --git a/src/Setting/AppSettings.php b/src/Setting/AppSettings.php index 6a6f03d..cef48c5 100644 --- a/src/Setting/AppSettings.php +++ b/src/Setting/AppSettings.php @@ -20,7 +20,14 @@ class AppSettings */ public function __construct(SettingStorage $settingStorage) { - $this->settingStorage = $settingStorage; + $groupName = $this->getSettingsGroupName(); + + if( $groupName && is_callable($groupName) ) { + $groupName = $this->runCallback($groupName, 'setting_group', null); + $this->settingStorage = $settingStorage->group($groupName); + } else { + $this->settingStorage = $settingStorage; + } } /** @@ -334,4 +341,14 @@ private function deleteFile($oldFile, $disk): void Storage::disk($disk)->delete($oldFile); } } + + /** + * Get the name of settings group + * + * @return string + */ + protected function getSettingsGroupName() + { + return config('app_settings.setting_group'); + } } diff --git a/src/config/app_settings.php b/src/config/app_settings.php index ab8046a..2f354be 100644 --- a/src/config/app_settings.php +++ b/src/config/app_settings.php @@ -90,4 +90,10 @@ // Controller to show and handle save setting 'controller' => '\QCod\AppSettings\Controllers\AppSettingController', + + // settings group + 'setting_group' => function() { + // return 'user_'.auth()->id(); + return 'default'; + } ]; diff --git a/tests/AppSettingsTest.php b/tests/AppSettingsTest.php index 8931eb4..3722725 100644 --- a/tests/AppSettingsTest.php +++ b/tests/AppSettingsTest.php @@ -306,4 +306,33 @@ public function it_can_access_settings_via_facade() \AppSettings::set('app_maker', 'apple'); $this->assertEquals('apple', \AppSettings::get('app_maker')); } + + /** + * it can set the group defined in config for settings + * + * @test + */ + public function it_can_set_the_group_defined_in_config_for_settings() + { + config()->set('app_settings.setting_group', function () { + return 'test_1'; + }); + + $this->configureInputs([ + [ + 'name' => 'app_name', + 'type' => 'text' + ] + ]); + + setting()->set('app_name', 'Cool App'); + + $this->assertEquals('Cool App', setting('app_name')); + + $this->assertDatabaseHas('settings', [ + 'name' => 'app_name', + 'val' => 'Cool App', + 'group' => 'test_1' + ]); + } } diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index e674358..8cb21a1 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -75,6 +75,38 @@ public function it_saves_setting_into_db_on_submit_form() $this->assertDatabaseHas('settings', $appNameSetting); } + /** + * it use group defined in settings config to store the settings + * + * @test + */ + public function it_use_group_defined_in_settings_config_to_store_the_settings() + { + config()->set('app_settings.setting_group', function () { + return 'test_1'; + }); + + $this->configureInputs([ + [ + 'name' => 'app_name', + 'type' => 'text', + 'rules' => 'required' + ] + ]); + + $appNameSetting = ['name' => 'app_name', 'val' => 'QCode App']; + + $this->assertDatabaseMissing('settings', $appNameSetting); + + $this->post('settings', ['app_name' => 'QCode App']) + ->assertRedirect() + ->assertSessionHas([ + 'status' => config('app_settings.submit_success_message', 'Settings Saved.') + ]); + + $this->assertDatabaseHas('settings', $appNameSetting + ['group' => 'test_1']); + } + /** * it dont removes abandoned settings if its set in config *