Permalink
Browse files

Fix issue where saved pastes shared a tempid.

The tempid is supposed to be a random unique value, not shared between
various versions of a paste.
  • Loading branch information...
1 parent b9984f8 commit fe6ed5cba8ed26448622f1bdd6a34d7d2ff7f1d0 @markstory markstory committed Oct 18, 2012
Showing with 9 additions and 22 deletions.
  1. +5 −11 Model/Paste.php
  2. +3 −0 Test/Case/Model/PasteTest.php
  3. +1 −11 View/Pastes/view.ctp
View
@@ -57,16 +57,6 @@ class Paste extends AppModel {
'Tag' => array('className' => 'Tag'),
);
-/**
- * Before save callback, sets some default data.
- */
- public function beforeValidate($options = array()) {
- if (empty($this->data['Paste']['temp'])) {
- $this->data['Paste']['temp'] = abs(mt_rand());
- }
- return true;
- }
-
/**
* Before saving the paste, save the tags so they can be associated
*/
@@ -98,9 +88,13 @@ public function beforeSave($options = array()) {
// If this paste has a parent always create a new one.
if (!empty($this->data['Paste']['paste_id'])) {
$this->id = false;
- unset($this->data['Paste']['id']);
+ unset($this->data['Paste']['id'], $this->data['Paste']['temp']);
$this->data['Paste']['created'] = date('Y-m-d H:i:s');
}
+
+ if (empty($this->data['Paste']['temp'])) {
+ $this->data['Paste']['temp'] = abs(mt_rand());
+ }
return true;
}
@@ -151,6 +151,8 @@ public function testSaveVersion() {
$this->assertTrue((bool)$result);
$originalId = $this->Paste->id;
+ $originalTemp = $result['Paste']['temp'];
+
$data = array(
'Paste' => array(
'id' => $originalId,
@@ -165,6 +167,7 @@ public function testSaveVersion() {
$this->assertTrue((bool)$result);
$this->assertNotEquals($this->Paste->id, $originalId);
+ $this->assertNotEquals($result['Paste']['temp'], $originalTemp);
$result = $this->Paste->read();
$this->assertEquals($originalId, $result['Paste']['paste_id']);
View
@@ -53,22 +53,12 @@
<div class="row form-field">
<?php echo $this->Html->link(
- 'View latest raw version',
+ 'View original',
array('action' => 'raw', $paste['Paste']['temp']),
array('class' => 'button tiny blue')
); ?>
</div>
- <?php if ($this->request->params['action'] == 'saved') : ?>
- <div class="row form-field">
- <?php echo $this->Html->link(
- 'View this as raw',
- array('action' => 'saved', $paste['Paste']['id'], 'html'),
- array('class' => 'button tiny blue')
- ); ?>
- </div>
- <?php endif; ?>
-
<div class="row form-field">
<a href="#modify" class="tiny red button modify-paste">
Modify this Paste

0 comments on commit fe6ed5c

Please sign in to comment.