Skip to content

Commit

Permalink
group suppor for settings resolves #11
Browse files Browse the repository at this point in the history
  • Loading branch information
saqueib committed Aug 14, 2019
1 parent 5d40bba commit 6d52940
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 18 additions & 1 deletion src/Setting/AppSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -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');
}
}
6 changes: 6 additions & 0 deletions src/config/app_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
];
29 changes: 29 additions & 0 deletions tests/AppSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]);
}
}
32 changes: 32 additions & 0 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 6d52940

Please sign in to comment.