Skip to content

Commit

Permalink
Added auto url generation functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyleeh committed Aug 1, 2016
1 parent c20c59e commit d1f0d79
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function validateCanHaveParentOfType()
return;
}

throw new InvalidParentNodeTypeException('Parent does not allow node type of name "' . $this->getNodeTypeName() .'" as child.');
throw new InvalidParentNodeTypeException('Parent does not allow node type of name "' . $this->getNodeTypeName() . '" as child.');
}

/**
Expand Down Expand Up @@ -1123,6 +1123,26 @@ public function scopeRecentlyCreated($query, $limit = null)
return $query;
}

/**
* Gets the full url for node
*
* @param string $locale
* @return string
*/
public function getNodeUrl($locale = null)
{
$node = $this;
$uri = '';

do
{
$uri = '/' . $node->getTranslationAttribute('node_name', $locale) . $uri;
$node = $node->parent;
} while ( ! is_null($node));

return url($uri);
}

/**
* Determines the default edit link for node
*
Expand Down
68 changes: 68 additions & 0 deletions tests/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,74 @@ function it_scopes_nodes_with_type()
);
}

/** @test */
function it_gets_node_url()
{
$root = $this->getNode();
$root->fill([
'en' => [
'title' => 'Root',
'node_name' => 'root'
],
'tr' => [
'title' => 'Kök',
'node_name' => 'kok'
]
])->save();

$this->assertEquals(
$root->getNodeUrl(),
'http://localhost/root'
);

$this->assertEquals(
$root->getNodeUrl('en'),
'http://localhost/root'
);

$this->assertEquals(
$root->getNodeUrl('tr'),
'http://localhost/kok'
);

$mid = $this->getNode();
$mid->fill([
'title' => 'Mid',
'node_name' => 'mid'
]);
$mid->appendToNode($root);
$mid->save();

$leaf = $this->getNode();
$leaf->fill([
'en' => [
'title' => 'Leaf',
'node_name' => 'leaf'
],
'tr' => [
'title' => 'Yaprak',
'node_name' => 'yaprak'
]
]);
$leaf->appendToNode($mid);
$leaf->save();

$this->assertEquals(
$leaf->getNodeUrl(),
'http://localhost/root/mid/leaf'
);

$this->assertEquals(
$leaf->getNodeUrl('en'),
'http://localhost/root/mid/leaf'
);

$this->assertEquals(
$leaf->getNodeUrl('tr'),
'http://localhost/kok/mid/yaprak'
);
}

/** @test */
function it_makes_the_default_edit_link()
{
Expand Down

0 comments on commit d1f0d79

Please sign in to comment.