Skip to content

Commit 5333aa4

Browse files
authored
Merge pull request #13 from qcod/grouped_settings
group support for settings resolves #11
2 parents 5d40bba + 6d52940 commit 5333aa4

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
All notable changes to `qcod/laravel-app-settings` will be documented in this file
44

5+
## 1.1.0 - 2019-08-14
6+
7+
- Group Settings by name
58

69
## 1.0.2 - 2016-10-10
710

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,25 @@ If your app needs different url to access the settings page you can change from
139139
// http://yourapp.com/app-settings
140140
```
141141

142+
### Use Group Setting
143+
144+
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
145+
146+
```php
147+
return [
148+
149+
// All the sections for the settings page
150+
'sections' => [...]
151+
152+
...
153+
// settings group
154+
'setting_group' => function() {
155+
return 'user_'.auth()->id();
156+
}
157+
```
158+
159+
In this case you can have different settings for each user.
160+
142161
### Use without UI
143162

144163
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.

src/Setting/AppSettings.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ class AppSettings
2020
*/
2121
public function __construct(SettingStorage $settingStorage)
2222
{
23-
$this->settingStorage = $settingStorage;
23+
$groupName = $this->getSettingsGroupName();
24+
25+
if( $groupName && is_callable($groupName) ) {
26+
$groupName = $this->runCallback($groupName, 'setting_group', null);
27+
$this->settingStorage = $settingStorage->group($groupName);
28+
} else {
29+
$this->settingStorage = $settingStorage;
30+
}
2431
}
2532

2633
/**
@@ -334,4 +341,14 @@ private function deleteFile($oldFile, $disk): void
334341
Storage::disk($disk)->delete($oldFile);
335342
}
336343
}
344+
345+
/**
346+
* Get the name of settings group
347+
*
348+
* @return string
349+
*/
350+
protected function getSettingsGroupName()
351+
{
352+
return config('app_settings.setting_group');
353+
}
337354
}

src/config/app_settings.php

+6
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,10 @@
9090

9191
// Controller to show and handle save setting
9292
'controller' => '\QCod\AppSettings\Controllers\AppSettingController',
93+
94+
// settings group
95+
'setting_group' => function() {
96+
// return 'user_'.auth()->id();
97+
return 'default';
98+
}
9399
];

tests/AppSettingsTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,33 @@ public function it_can_access_settings_via_facade()
306306
\AppSettings::set('app_maker', 'apple');
307307
$this->assertEquals('apple', \AppSettings::get('app_maker'));
308308
}
309+
310+
/**
311+
* it can set the group defined in config for settings
312+
*
313+
* @test
314+
*/
315+
public function it_can_set_the_group_defined_in_config_for_settings()
316+
{
317+
config()->set('app_settings.setting_group', function () {
318+
return 'test_1';
319+
});
320+
321+
$this->configureInputs([
322+
[
323+
'name' => 'app_name',
324+
'type' => 'text'
325+
]
326+
]);
327+
328+
setting()->set('app_name', 'Cool App');
329+
330+
$this->assertEquals('Cool App', setting('app_name'));
331+
332+
$this->assertDatabaseHas('settings', [
333+
'name' => 'app_name',
334+
'val' => 'Cool App',
335+
'group' => 'test_1'
336+
]);
337+
}
309338
}

tests/SettingsTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,38 @@ public function it_saves_setting_into_db_on_submit_form()
7575
$this->assertDatabaseHas('settings', $appNameSetting);
7676
}
7777

78+
/**
79+
* it use group defined in settings config to store the settings
80+
*
81+
* @test
82+
*/
83+
public function it_use_group_defined_in_settings_config_to_store_the_settings()
84+
{
85+
config()->set('app_settings.setting_group', function () {
86+
return 'test_1';
87+
});
88+
89+
$this->configureInputs([
90+
[
91+
'name' => 'app_name',
92+
'type' => 'text',
93+
'rules' => 'required'
94+
]
95+
]);
96+
97+
$appNameSetting = ['name' => 'app_name', 'val' => 'QCode App'];
98+
99+
$this->assertDatabaseMissing('settings', $appNameSetting);
100+
101+
$this->post('settings', ['app_name' => 'QCode App'])
102+
->assertRedirect()
103+
->assertSessionHas([
104+
'status' => config('app_settings.submit_success_message', 'Settings Saved.')
105+
]);
106+
107+
$this->assertDatabaseHas('settings', $appNameSetting + ['group' => 'test_1']);
108+
}
109+
78110
/**
79111
* it dont removes abandoned settings if its set in config
80112
*

0 commit comments

Comments
 (0)