Skip to content

Commit

Permalink
Merge pull request #82 from drewroberts/pdbreen/feature/79
Browse files Browse the repository at this point in the history
Change flag name and simplify redirect logic #79
  • Loading branch information
drewroberts authored Apr 14, 2021
2 parents 495c222 + 8ab2b83 commit 4105048
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function up()
$table->string('slug')->index();
$table->string('title')->unique();
$table->foreignIdFor(app('layout'))->nullable(); // Will remove nullable and default a basic layout for pages. Allows some pages to have different layout (AMP or regular html & other variations)
$table->boolean('location_based')->default(false);
$table->boolean('is_location')->default(false);
$table->foreignIdFor(app('page'), 'parent_id')->nullable(); // Parent Page
$table->text('content')->nullable(); // Will be written in Markdown.

Expand Down
19 changes: 3 additions & 16 deletions src/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,11 @@ class PageController extends BaseController
{
public function __invoke(Request $request, Page $page, ?Page $childPage = null, Page $grandChildPage = null)
{
// If location based grand child is only child, redirect!!
if ($grandChildPage) {
if ($grandChildPage->location_based && $grandChildPage->is_only_child) {
return redirect(url($grandChildPage->path));
}
} elseif ($childPage) {
// If location based child is only child, redirect!!!
if ($childPage->location_based && $childPage->is_only_child) {
return redirect(url($childPage->path));
}
} else {
// If there is only location based root page, redirect home!
if ($page->is_only_root_location) {
return redirect(url($page->path));
}
$leafPage = $grandChildPage ?: ($childPage ?: $page);
if ($request->path() !== $leafPage->path) {
return redirect(url($leafPage->path));
}

$leafPage = $grandChildPage ?: ($childPage ?: $page);
LayoutManager::setLayout($leafPage->layout);

return view(LayoutManager::getViewName('blog::page.base'), [
Expand Down
18 changes: 7 additions & 11 deletions src/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @property int id
* @property string slug
* @property string title
* @property bool location_based
* @property bool is_location
* @property Page parent
* @property bool is_leaf
* @property bool is_root
Expand Down Expand Up @@ -67,7 +67,7 @@ class Page extends BaseModel
HasPageViews;

protected $casts = [
'location_based' => 'boolean',
'is_location' => 'boolean',
'parent_id' => 'integer',
'webpage_id' => 'integer',
'image_id' => 'integer',
Expand All @@ -83,7 +83,7 @@ class Page extends BaseModel
'parent_id',
'slug',
'title',
'location_based',
'is_location',
];

protected static function boot()
Expand Down Expand Up @@ -139,27 +139,23 @@ public function layout()

public function getPathAttribute(): ?string
{
if ($this->is_only_root_location) {
return '/';
}

$path = [];
$parent = $this;
while ($parent) {
// Start accumulating slugs when not location based or not only child
if ($path || ! $parent->location_based || ! $parent->is_only_child) {
if ($path || ! $parent->is_location || (! $parent->is_only_child && ! $parent->is_only_root_location)) {
$path[] = $parent->slug;
}
$parent = $parent->parent;
}

return implode('/', array_reverse($path));
return empty($path) ? '/' : implode('/', array_reverse($path));
}

public function getIsOnlyRootLocationAttribute(): bool
{
return ($this->location_based && $this->is_root &&
static::query()->whereNull('parent_id')->where('location_based', true)->count() === 1);
return ($this->is_location && $this->is_root &&
static::query()->whereNull('parent_id')->where('is_location', true)->count() === 1);
}

public function getIsRootAttribute(): bool
Expand Down
46 changes: 23 additions & 23 deletions tests/Unit/Http/Controllers/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PageControllerTest extends TestCase
public function index_top_level_page_no_children()
{
$page = Page::factory()->create([
'location_based' => false,
'is_location' => false,
]);

$this->get($this->webUrl("/{$page->slug}"))
Expand All @@ -28,11 +28,11 @@ public function index_top_level_page_no_children()
public function index_top_level_page_with_children()
{
$page = Page::factory()->create([
'location_based' => false,
'is_location' => false,
]);

Page::factory()->create([
'location_based' => false,
'is_location' => false,
])->setParent($page);

$this->get($this->webUrl("/{$page->slug}"))
Expand All @@ -44,18 +44,18 @@ public function index_top_level_page_with_children()
public function location_based_single_child_redirects_to_parent()
{
$page = Page::factory()->create([
'location_based' => true,
'is_location' => true,
]);

$child = Page::factory()->create([
'location_based' => true,
'is_location' => true,
])->setParent($page);

$this->get($this->webUrl("/{$page->slug}"))
->assertRedirect('/');

Page::factory()->create([
'location_based' => true,
'is_location' => true,
]);

$this->get($this->webUrl("/{$page->slug}/{$child->slug}"))
Expand All @@ -66,15 +66,15 @@ public function location_based_single_child_redirects_to_parent()
public function location_based_single_grand_child_redirects_to_parent()
{
$page = Page::factory()->count(2)->create([
'location_based' => true,
'is_location' => true,
])->first();

$child1 = Page::factory()->create([
'location_based' => true,
'is_location' => true,
])->setParent($page);

$grandChild = Page::factory()->create([
'location_based' => true,
'is_location' => true,
])->setParent($child1);

$this->get($this->webUrl("/{$page->slug}"))
Expand All @@ -85,7 +85,7 @@ public function location_based_single_grand_child_redirects_to_parent()
->assertRedirect("/{$page->slug}");

$child2 = Page::factory()->create([
'location_based' => true,
'is_location' => true,
])->setParent($page);

$this->get($this->webUrl("/{$page->slug}/{$child1->slug}/{$grandChild->slug}"))
Expand All @@ -96,15 +96,15 @@ public function location_based_single_grand_child_redirects_to_parent()
public function location_based_multi_child_does_not_redirect()
{
$page = Page::factory()->count(2)->create([
'location_based' => true,
'is_location' => true,
])->first();

$child1 = Page::factory()->create([
'location_based' => true,
'is_location' => true,
])->setParent($page);

$child2 = Page::factory()->create([
'location_based' => true,
'is_location' => true,
])->setParent($page);

$this->get($this->webUrl("/{$page->slug}"))
Expand Down Expand Up @@ -135,10 +135,10 @@ public function index_child_page_no_grand_children()
public function index_child_page_with_children()
{
$page = Page::factory()->create([
'location_based' => false,
'is_location' => false,
]);
$child = Page::factory()->create([
'location_based' => false,
'is_location' => false,
])->setParent($page);

Page::factory()->create()->setParent($child);
Expand All @@ -154,10 +154,10 @@ public function index_child_page_all_sequence()
$this->logToStderr();

$page = Page::factory()->create([
'location_based' => false,
'is_location' => false,
]);
$child = Page::factory()->create([
'location_based' => false,
'is_location' => false,
])->setParent($page);

$segments = [$page->slug, $child->slug];
Expand All @@ -178,13 +178,13 @@ public function index_child_page_all_sequence()
public function index_grand_child_page()
{
$page = Page::factory()->create([
'location_based' => false,
'is_location' => false,
]);
$child = Page::factory()->create([
'location_based' => false,
'is_location' => false,
])->setParent($page);
$grandChild = Page::factory()->create([
'location_based' => false,
'is_location' => false,
])->setParent($child);

$this->get($this->webUrl("/{$page->slug}/{$child->slug}/{$grandChild->slug}"))
Expand All @@ -196,13 +196,13 @@ public function index_grand_child_page()
public function index_grand_child_page_all_sequence()
{
$page = Page::factory()->create([
'location_based' => false,
'is_location' => false,
]);
$child = Page::factory()->create([
'location_based' => false,
'is_location' => false,
])->setParent($page);
$grandChild = Page::factory()->create([
'location_based' => false,
'is_location' => false,
])->setParent($child);

$segments = [$page->slug, $child->slug, $grandChild->slug];
Expand Down

0 comments on commit 4105048

Please sign in to comment.