Skip to content

Commit

Permalink
Add a "preserve_ids" global query parameter for REST requests that
Browse files Browse the repository at this point in the history
indicates that we shouldn't opportunistically convert ids into REST
urls.
  • Loading branch information
bharat committed Jun 8, 2010
1 parent 98fce83 commit 6425d41
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 27 deletions.
3 changes: 2 additions & 1 deletion modules/gallery/helpers/item_rest.php
Expand Up @@ -78,9 +78,10 @@ static function get($request) {
}
$orm->order_by($order_by);

$preserve_ids = isset($p->preserve_ids) ? (bool)$p->preserve_ids : false;
$result = array(
"url" => $request->url,
"entity" => $item->as_restful_array(),
"entity" => $item->as_restful_array($preserve_ids),
"relationships" => rest::relationships("item", $item));
if ($item->is_album()) {
$result["members"] = array();
Expand Down
14 changes: 8 additions & 6 deletions modules/gallery/helpers/items_rest.php
Expand Up @@ -36,6 +36,8 @@ class items_rest_Core {
*/
static function get($request) {
$items = array();
$preserve_ids = isset($request->params->preserve_ids) ?
(bool)$request->params->preserve_ids : false;
if (isset($request->params->urls)) {
foreach (json_decode($request->params->urls) as $url) {
if (isset($request->params->type)) {
Expand All @@ -45,10 +47,10 @@ static function get($request) {
if (access::can("view", $item)) {
if (isset($types)) {
if (in_array($item->type, $types)) {
$items[] = items_rest::_format_restful_item($item);
$items[] = items_rest::_format_restful_item($item, $preserve_ids);
}
} else {
$items[] = items_rest::_format_restful_item($item);
$items[] = items_rest::_format_restful_item($item, $preserve_ids);
}
}
}
Expand All @@ -57,9 +59,9 @@ static function get($request) {
if (!access::can("view", $item)) {
throw new Kohana_404_Exception();
}
$items[] = items_rest::_format_restful_item($item);
$items[] = items_rest::_format_restful_item($item, $preserve_ids);
while (($item = $item->parent()) != null) {
array_unshift($items, items_rest::_format_restful_item($item));
array_unshift($items, items_rest::_format_restful_item($item, $preserve_ids));
};
}

Expand All @@ -74,9 +76,9 @@ static function resolve($id) {
return $item;
}

private static function _format_restful_item($item) {
private static function _format_restful_item($item, $preserve_ids) {
$item_rest = array("url" => rest::url("item", $item),
"entity" => $item->as_restful_array(),
"entity" => $item->as_restful_array($preserve_ids),
"relationships" => rest::relationships("item", $item));
if ($item->type == "album") {
$members = array();
Expand Down
7 changes: 5 additions & 2 deletions modules/gallery/models/item.php
Expand Up @@ -923,12 +923,15 @@ public function read_only(Validation $v, $field) {

/**
* Same as ORM::as_array() but convert id fields into their RESTful form.
* Convert any item ids into REST urls
*
* @param bool preserve_ids true if we should preserve ids
*/
public function as_restful_array($convert_ids=true) {
public function as_restful_array($preserve_ids) {
// Convert item ids to rest URLs for consistency
$data = $this->as_array();

if ($convert_ids) {
if (!$preserve_ids) {
if ($tmp = $this->parent()) {
$data["parent"] = rest::url("item", $tmp);
}
Expand Down
4 changes: 2 additions & 2 deletions modules/gallery/tests/Item_Model_Test.php
Expand Up @@ -357,7 +357,7 @@ public function as_restful_array_test() {
$photo = test::random_photo($album);
$album->reload();

$result = $album->as_restful_array();
$result = $album->as_restful_array(false);
$this->assert_same(rest::url("item", item::root()), $result["parent"]);
$this->assert_same(rest::url("item", $photo), $result["album_cover"]);
$this->assert_true(!array_key_exists("parent_id", $result));
Expand All @@ -369,7 +369,7 @@ public function as_restful_array_with_ids_test() {
$photo = test::random_photo($album);
$album->reload();

$result = $album->as_restful_array(false);
$result = $album->as_restful_array(true);
$this->assert_same(item::root()->id, $result["parent_id"]);
$this->assert_same($photo->id, $result["album_cover_item_id"]);
$this->assert_true(!array_key_exists("parent", $result));
Expand Down
21 changes: 16 additions & 5 deletions modules/gallery/tests/Item_Rest_Helper_Test.php
Expand Up @@ -28,6 +28,17 @@ public function resolve_test() {
$this->assert_equal($album->id, $resolved->id);
}

public function get_with_ids_test() {
$photo1 = test::random_photo(item::root());
$request = new stdClass();
$request->url = rest::url("item", $photo1);
$request->params = new stdClass();
$request->params->preserve_ids = 1;

$response = item_rest::get($request);
$this->assert_equal(item::root()->id, $response["entity"]["parent_id"]); // Spot check
}

public function get_scope_test() {
$album1 = test::random_album();
$photo1 = test::random_photo($album1);
Expand All @@ -41,7 +52,7 @@ public function get_scope_test() {
$request->params = new stdClass();
$this->assert_equal_array(
array("url" => rest::url("item", $album1),
"entity" => $album1->as_restful_array(),
"entity" => $album1->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album1),
Expand All @@ -56,7 +67,7 @@ public function get_scope_test() {
$request->params->scope = "direct";
$this->assert_equal_array(
array("url" => rest::url("item", $album1),
"entity" => $album1->as_restful_array(),
"entity" => $album1->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album1),
Expand All @@ -71,7 +82,7 @@ public function get_scope_test() {
$request->params->scope = "all";
$this->assert_equal_array(
array("url" => rest::url("item", $album1),
"entity" => $album1->as_restful_array(),
"entity" => $album1->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album1),
Expand All @@ -98,7 +109,7 @@ public function get_children_like_test() {
$request->params->name = "foo";
$this->assert_equal_array(
array("url" => rest::url("item", $album1),
"entity" => $album1->as_restful_array(),
"entity" => $album1->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album1),
Expand All @@ -121,7 +132,7 @@ public function get_children_type_test() {
$request->params->type = "album";
$this->assert_equal_array(
array("url" => rest::url("item", $album1),
"entity" => $album1->as_restful_array(),
"entity" => $album1->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album1),
Expand Down
35 changes: 24 additions & 11 deletions modules/gallery/tests/Items_Rest_Helper_Test.php
Expand Up @@ -34,13 +34,13 @@ public function get_url_test() {
$this->assert_equal_array(
array(
array("url" => rest::url("item", $photo1),
"entity" => $photo1->as_restful_array(),
"entity" => $photo1->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $photo1),
"members" => array()))),
array("url" => rest::url("item", $album2),
"entity" => $album2->as_restful_array(),
"entity" => $album2->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album2),
Expand All @@ -67,7 +67,7 @@ public function get_url_filter_album_test() {
$this->assert_equal_array(
array(
array("url" => rest::url("item", $album2),
"entity" => $album2->as_restful_array(),
"entity" => $album2->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album2),
Expand All @@ -94,7 +94,7 @@ public function get_url_filter_photo_test() {
$this->assert_equal_array(
array(
array("url" => rest::url("item", $photo1),
"entity" => $photo1->as_restful_array(),
"entity" => $photo1->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $photo1),
Expand All @@ -119,13 +119,13 @@ public function get_url_filter_albums_photos_test() {
$this->assert_equal_array(
array(
array("url" => rest::url("item", $photo1),
"entity" => $photo1->as_restful_array(),
"entity" => $photo1->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $photo1),
"members" => array()))),
array("url" => rest::url("item", $album2),
"entity" => $album2->as_restful_array(),
"entity" => $album2->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album2),
Expand All @@ -146,7 +146,7 @@ public function get_ancestor_test() {
$root = ORM::factory("item", 1);
$restful_root = array(
"url" => rest::url("item", $root),
"entity" => $root->as_restful_array(),
"entity" => $root->as_restful_array(false),
"relationships" => rest::relationships("item", $root));
$restful_root["members"] = array();
foreach ($root->children() as $child) {
Expand All @@ -155,12 +155,12 @@ public function get_ancestor_test() {

$request = new stdClass();
$request->params = new stdClass();
$request->params->ancestor_for = rest::url("item", $photo2);
$request->params->ancestors_for = rest::url("item", $photo2);
$this->assert_equal_array(
array(
$restful_root,
array("url" => rest::url("item", $album1),
"entity" => $album1->as_restful_array(),
"entity" => $album1->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album1),
Expand All @@ -170,19 +170,32 @@ public function get_ancestor_test() {
rest::url("item", $album2)),
),
array("url" => rest::url("item", $album2),
"entity" => $album2->as_restful_array(),
"entity" => $album2->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album2),
"members" => array())),
"members" => array(
rest::url("item", $photo2))),
array("url" => rest::url("item", $photo2),
"entity" => $photo2->as_restful_array(),
"entity" => $photo2->as_restful_array(false),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $photo2),
"members" => array())))),
items_rest::get($request));
}

public function get_ancestor_with_ids_test() {
$album1 = test::random_album();
$photo1 = test::random_photo($album1);

$request = new stdClass();
$request->params = new stdClass();
$request->params->ancestors_for = rest::url("item", $photo1);
$request->params->preserve_ids = 1;

$response = items_rest::get($request);
$this->assert_same(item::root()->id, $response[1]["entity"]["parent_id"]); // Spot check
}
}

0 comments on commit 6425d41

Please sign in to comment.