diff options
Diffstat (limited to 'modules/gallery')
-rw-r--r-- | modules/gallery/models/item.php | 17 | ||||
-rw-r--r-- | modules/gallery/tests/Item_Model_Test.php | 12 |
2 files changed, 29 insertions, 0 deletions
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index ae6e4cc9..fd121a5a 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -914,4 +914,21 @@ class Item_Model extends ORM_MPTT { $v->add_error($field, "read_only"); } } + + /** + * Same as ORM::as_array() but convert id fields into their RESTful form. + */ + public function as_restful_array() { + // Convert item ids to rest URLs for consistency + $data = $this->as_array(); + if ($tmp = $this->parent()) { + $data["parent"] = rest::url("item", $tmp); + } + unset($data["parent_id"]); + if ($tmp = $this->album_cover()) { + $data["album_cover"] = rest::url("item", $tmp); + } + unset($data["album_cover_item_id"]); + return $data; + } } diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index eb9ecc99..9f632fb5 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -346,4 +346,16 @@ class Item_Model_Test extends Gallery_Unit_Test_Case { } $this->assert_true(false, "Shouldn't get here"); } + + public function as_restful_array_test() { + $album = test::random_album(); + $photo = test::random_photo($album); + $album->reload(); + + $result = $album->as_restful_array(); + $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)); + $this->assert_true(!array_key_exists("album_cover_item_id", $result)); + } } |