Skip to content

Commit

Permalink
chore: Refactor code for improved readability and maintainability, an…
Browse files Browse the repository at this point in the history
…d add policy support
  • Loading branch information
ferdiunal committed Sep 8, 2024
1 parent d1d2293 commit e5bc0df
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ public function get(string $group, NovaRequest $request)
unset($resource['settings']);

return $resource;
})->values();
})
->filter(function ($resource) use ($request) {
$policy = str($resource['title'])->camel()->append('::view')->toString();

return $request->user()->can($policy);
})
->values();

abort_unless($resources->isNotEmpty(), 404);

Expand Down
31 changes: 19 additions & 12 deletions src/NovaSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ public function boot()
*/
public function menu(Request $request)
{
$resources = settingsResources()->pluck('group')->unique()->map(
fn ($group) => MenuItem::make(
str($group)->title()
->when(
str($group)->lower()->endsWith('settings'),
fn ($title) => str($title)->replace(' settings', '')->ucfirst()->__toString()
)
->append(' Settings')
->__toString(),
str($group)->lower()->slug()->prepend('/nova-settings/')->__toString()
)
);
$resources = settingsResources()
->filter(function ($resource) use ($request) {
$policy = str($resource['title'])->camel()->append('::view')->toString();

return $request->user()->can($policy);
})
->pluck('group')
->unique()->map(
fn ($group) => MenuItem::make(
str($group)->title()
->when(
str($group)->lower()->endsWith('settings'),
fn ($title) => str($title)->replace(' settings', '')->ucfirst()->__toString()
)
->append(' Settings')
->__toString(),
str($group)->lower()->slug()->prepend('/nova-settings/')->__toString()
)
);

return MenuSection::make('Nova Settings', $resources->toArray())
->collapsable()
Expand Down
5 changes: 0 additions & 5 deletions src/NovaSettingsToolServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Ferdiunal\NovaSettings\Console\MakeSettingResource;
use Ferdiunal\NovaSettings\Http\Middleware\Authorize;
use Illuminate\Log\Context\Repository as ContextRepository;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Laravel\Nova\Events\ServingNova;
Expand Down Expand Up @@ -36,10 +35,6 @@ public function boot()

$this->app->booted(function () {
$this->routes();
// $this->app->make(ContextRepository::class)->add(
// 'nova-settings-resources',
// $this->resources()
// );
});

Nova::serving(function (ServingNova $event) {
Expand Down

0 comments on commit e5bc0df

Please sign in to comment.