Skip to content

Commit

Permalink
refactored in the case module has different name from model
Browse files Browse the repository at this point in the history
  • Loading branch information
zeezo887 committed Mar 22, 2024
1 parent d0cc397 commit dc2e5ca
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/Helpers/modules_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ function getModelByModuleName($moduleName)
if (!function_exists('getModuleNameByModel')) {
function getModuleNameByModel($model)
{
return Str::plural(lcfirst(class_basename($model)));
try {
return TwillCapsules::getCapsuleForModel($model)->getModule();
} catch (NoCapsuleFoundException) {
return Str::plural(lcfirst(class_basename($model)));
}
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/Repositories/Behaviors/HandleBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,16 +465,11 @@ protected function getBlockBrowsers($block)

$items = Collection::make(array_values($sortedRelatedItems))->filter(function ($value) {
return is_object($value);
})->map(function ($relatedElement) use ($relation) {
})->map(function ($relatedElement) {

Check warning on line 468 in src/Repositories/Behaviors/HandleBlocks.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleBlocks.php#L468

Added line #L468 was not covered by tests
return [
'id' => $relatedElement->id,
'name' => $relatedElement->titleInBrowser ?? $relatedElement->title,
'edit' => moduleRoute(
$relation,
config('twill.block_editor.browser_route_prefixes.' . $relation),
'edit',
$relatedElement->id
),
'edit' => $this->getAdminEditUrl($relatedElement),

Check warning on line 472 in src/Repositories/Behaviors/HandleBlocks.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleBlocks.php#L472

Added line #L472 was not covered by tests
] + (classHasTrait($relatedElement, HasMedias::class) ? [
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []);
Expand Down
11 changes: 4 additions & 7 deletions src/Repositories/Behaviors/HandleBrowsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,17 @@ public function getFormFieldsForRelatedBrowser($object, $relation, $titleKey = '
* @param $object
* @return mixed|string
*/
private function getAdminEditUrl($object): mixed
public function getAdminEditUrl($object): mixed
{
if (!empty($object->adminEditUrl)) {
return $object->adminEditUrl;

Check warning on line 249 in src/Repositories/Behaviors/HandleBrowsers.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleBrowsers.php#L249

Added line #L249 was not covered by tests
}

$relatedType = $object->getRelation('pivot')->related_type;
$relation = str_contains($relatedType, '\\')
? getModuleNameByModel($relatedType)
: $relatedType;
$module = getModuleNameByModel($object);

return moduleRoute(
$relation,
config('twill.block_editor.browser_route_prefixes.' . $relation),
$module,
config('twill.block_editor.browser_route_prefixes.' . $module),
'edit',
$object->id
);
Expand Down

0 comments on commit dc2e5ca

Please sign in to comment.