summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-20 22:49:32 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-20 22:49:32 +0000
commit99c481897b8d1574bf984a1d5ea732fe63482fe0 (patch)
tree00bfc1fd883d234292292ff6cc788e5c9564fed9
parent05f26fa2d4d45d99082c81f03fa71922770d18dc (diff)
Add "created" and "updated" timestamps to the items table.
-rw-r--r--core/helpers/core_installer.php2
-rw-r--r--core/models/item.php13
-rw-r--r--core/tests/Item_Model_Test.php63
3 files changed, 78 insertions, 0 deletions
diff --git a/core/helpers/core_installer.php b/core/helpers/core_installer.php
index 4ab54da9..399b6bf5 100644
--- a/core/helpers/core_installer.php
+++ b/core/helpers/core_installer.php
@@ -44,6 +44,7 @@ class core_installer {
ENGINE=InnoDB DEFAULT CHARSET=utf8;");
$db->query("CREATE TABLE `items` (
+ `created` int(9) default NULL,
`description` char(255) default NULL,
`height` int(9) default NULL,
`id` int(9) NOT NULL auto_increment,
@@ -60,6 +61,7 @@ class core_installer {
`thumb_width` int(9) default NULL,
`title` char(255) default NULL,
`type` char(32) NOT NULL,
+ `updated` int(9) default NULL,
`view_count` int(9) default 0,
`width` int(9) default NULL,
PRIMARY KEY (`id`),
diff --git a/core/models/item.php b/core/models/item.php
index e5eb4039..d71414d8 100644
--- a/core/models/item.php
+++ b/core/models/item.php
@@ -237,4 +237,17 @@ class Item_Model extends ORM_MPTT {
return parent::__get($column);
}
}
+
+ /**
+ * @see ORM::save()
+ */
+ public function save() {
+ if (!empty($this->changed) && $this->changed != array("view_count" => "view_count")) {
+ $this->updated = time();
+ if (!$this->loaded) {
+ $this->created = $this->updated;
+ }
+ }
+ return parent::save();
+ }
}
diff --git a/core/tests/Item_Model_Test.php b/core/tests/Item_Model_Test.php
new file mode 100644
index 00000000..6024366b
--- /dev/null
+++ b/core/tests/Item_Model_Test.php
@@ -0,0 +1,63 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Item_Model_Test extends Unit_Test_Case {
+ public function saving_sets_created_and_updated_dates_test() {
+ $item = ORM::factory("item");
+ $item->name = rand();
+ $item->save();
+ $this->assert_true(!empty($item->created));
+ $this->assert_true(!empty($item->updated));
+ }
+
+ public function updating_doesnt_change_created_date_test() {
+ $item = ORM::factory("item");
+ $item->name = rand();
+ $item->save();
+
+ // Force the creation date to something well known
+ $db = Database::instance();
+ $db->query("UPDATE `items` SET `created` = 0 WHERE `id` = $item->id");
+ $db->query("UPDATE `items` SET `updated` = 0 WHERE `id` = $item->id");
+ $item->reload();
+ $item->title = "foo"; // force a change
+ $item->save();
+
+ $this->assert_true(empty($item->created));
+ $this->assert_true(!empty($item->updated));
+ }
+
+ public function updating_view_count_only_doesnt_change_updated_date_test() {
+ $item = ORM::factory("item");
+ $item->name = rand();
+ $item->save();
+ $item->reload();
+ $this->assert_same(0, $item->view_count);
+
+ // Force the updated date to something well known
+ $db = Database::instance();
+ $db->query("UPDATE `items` SET `updated` = 0 WHERE `id` = $item->id");
+ $item->reload();
+ $item->view_count++;
+ $item->save();
+
+ $this->assert_same(1, $item->view_count);
+ $this->assert_true(empty($item->updated));
+ }
+}