Commit: aeb62635748221adcece295d4d8d50f1718c1ecb
Author: gwoo | Date: 2009-11-18 17:24:58 -0800
diff --git a/controllers/PastesController.php b/controllers/PastesController.php
index f814332..b7ede1a 100644
--- a/controllers/PastesController.php
+++ b/controllers/PastesController.php
@@ -23,12 +23,17 @@ class PastesController extends \lithium\action\Controller {
* @return array
*/
public function index() {
- return array('latest' => Paste::find('all', array('conditions'=> array(
- 'design' => 'latest',
- 'view' => 'all',
- 'limit' => '10',
- 'descending' => 'true'
- ))));
+ $latest = Paste::find('all', array('conditions'=> array(
+ 'design' => 'latest', 'view' => 'all', 'limit' => '10', 'descending' => 'true'
+ )));
+ if ($latest === null) {
+ if (Paste::create(array('design' => 'latest'))->save()) {
+ $latest = Paste::find('all', array('conditions'=> array(
+ 'design' => 'latest', 'view' => 'all', 'limit' => '10', 'descending' => 'true'
+ )));
+ }
+ }
+ return compact('latest');
}
/**
diff --git a/models/Paste.php b/models/Paste.php
index b686dfb..cb6dcd6 100644
--- a/models/Paste.php
+++ b/models/Paste.php
@@ -3,7 +3,6 @@
namespace app\models;
use \Geshi;
-use \lithium\data\model\Document;
use \lithium\util\Validator;
/**
@@ -31,7 +30,7 @@ class Paste extends \lithium\data\Model {
*
* @var array
*/
- public static $languages = array('php','html','javascript','text');
+ public static $languages = array('php', 'diff', 'html', 'javascript', 'text');
/**
* Metadata
@@ -59,26 +58,33 @@ class Paste extends \lithium\data\Model {
);
/**
+ * Error messages for validation
+ *
+ * @var array
+ */
+ protected static $_errors = array(
+ 'author' => 'You forgot your alphanumeric name?',
+ 'content' => 'You seem to be missing the content.',
+ 'language' => 'Invalid language.'
+ );
+
+ /**
* Views Document
*/
public static $_views = array(
- 'latest' => array(
- 'id' => '_design/latest',
- 'language' => 'javascript',
+ 'latest' => array('id' => '_design/latest', 'language' => 'javascript',
'views' => array(
- 'all' => array(
- 'map' => 'function(doc) {
- if (doc.permanent == "1") {
- var preview = String.substring(doc.content, 0, 100);
- emit(doc.created, {
- author:doc.author, language:doc.language,
- preview: preview, created: doc.created
- });
- }
- }'
- )
+ 'all' => array('map' => 'function(doc) {
+ if (doc.permanent == "1") {
+ var preview = String.substring(doc.content, 0, 100);
+ emit(doc.created, {
+ author:doc.author, language:doc.language,
+ preview: preview, created: doc.created
+ });
+ }
+ }'),
)
- )
+ ),
);
/**
@@ -87,25 +93,22 @@ class Paste extends \lithium\data\Model {
public static function __init($options = array()) {
parent::__init($options);
Paste::applyFilter('find', function($self, $params, $chain) {
- if (isset($params['options']['conditions']['design']) &&
- $params['options']['conditions']['design'] == 'latest') {
- $conditions = $params['options']['conditions'];
- $result = $chain->next($self, $params, $chain);
- if ($result === null) {
- Paste::createView()->save();
- return null; //static::find('all', $conditions);
- }
- foreach ($result as $paste) {
- $paste->preview = rawurldecode($paste->preview);
- }
- return $result;
- } else {
- $result = $chain->next($self, $params, $chain);
- $result->content = rawurldecode($result->content);
- $result->parsed = rawurldecode($result->parsed);
- return $result;
+ if (isset($params['options']['conditions']['design'])) {
+ $result = $chain->next($self, $params, $chain);
+ if ($result === null) {
+ return null;
+ }
+ foreach ($result as $paste) {
+ $paste->preview = rawurldecode($paste->preview);
}
- });
+ return $result;
+ } else {
+ $result = $chain->next($self, $params, $chain);
+ $result->content = rawurldecode($result->content);
+ $result->parsed = rawurldecode($result->parsed);
+ return $result;
+ }
+ });
Paste::applyFilter('save', function($self, $params, $chain) {
if ($params['record']->id != '_design/latest') {
$document = $params['record'];
@@ -131,29 +134,14 @@ class Paste extends \lithium\data\Model {
if (!($doc instanceof \lithium\data\model\Document)) {
return null;
}
-
$geshi = new GeSHi($doc->content, $doc->language);
$geshi->enable_classes();
$geshi->enable_keyword_links(false);
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS,2);
$doc->parsed = $geshi->parse_code();
-
return $doc;
}
-
- /**
- * Used to create and then save the design view 'latest' to couch, ie:
- * {{{
- * Paste::createView()->save();
- * }}}
- *
- * @return Document
- */
- public static function createView() {
- return parent::create(static::$_views['latest']);
- }
-
/**
* Sets default values and calls the parent create()
*
@@ -161,12 +149,16 @@ class Paste extends \lithium\data\Model {
* @return Document
*/
public static function create($data = array()) {
+ if (isset($data['design'])) {
+ if (!isset(static::$_views[$data['design']])) {
+ return false;
+ }
+ return parent::create(static::$_views[$data['design']]);
+ }
if (isset($data['Paste'])) {
$data = $data['Paste'];
}
- $data += static::$_defaults;
- if (!isset($data['created']))
- $data['created'] = date('Y-m-d h:m:s');
+ $data += static::$_defaults + array('created' => date('Y-m-d h:m:s'));
return parent::create($data);
}
@@ -179,22 +171,22 @@ class Paste extends \lithium\data\Model {
* @return boolean
*/
public function validates($record, $options = array()) {
- $success = true; $errors = array();
- if (!Validator::isAlphaNumeric($record->author)) {
- $success = false;
- $errors['author'] = 'This field can only be alphanumeric';
+ $errors = static::$_errors;
+
+ if (Validator::isAlphaNumeric($record->author)) {
+ unset($errors['author']);
+ }
+ if (Validator::isNotEmpty($record->content)) {
+ unset($errors['content']);
}
- if (!Validator::isNotEmpty($record->content)) {
- $success = false;
- $errors['content'] = 'This field can not be left empty';
+ if (in_array($record->language, static::$languages)) {
+ unset($errors['language']);
}
- if (!in_array($record->language, static::$languages)) {
- $success = false;
- $errors['language'] = 'You have messed with the HTML that is not valid language';
+ if (empty($errors)){
+ return true;
}
- if (!$success)
- $record->set(array('errors' => $errors));
- return $success;
+ $record->set(array('errors' => $errors));
+ return false;
}
}
diff --git a/tests/cases/models/PasteTest.php b/tests/cases/models/PasteTest.php
index b367efb..32dd60a 100644
--- a/tests/cases/models/PasteTest.php
+++ b/tests/cases/models/PasteTest.php
@@ -116,7 +116,7 @@ class PasteTest extends \lithium\test\Unit {
}
public function testCreateView() {
- $view = MockPaste::createView();
+ $view = MockPaste::create(array('design' => 'latest'));
$expected = '_design/latest';
$result = $view->id;
@@ -167,9 +167,9 @@ class PasteTest extends \lithium\test\Unit {
$this->skipIf(!is_a($paste, '\lithium\data\model\Document'));
$this->assertTrue(is_a($paste->errors, '\lithium\data\model\Document'));
$expected = array(
- 'author' => 'This field can only be alphanumeric',
- 'content' => 'This field can not be left empty',
- 'language' => 'You have messed with the HTML that is not valid language'
+ 'author' => 'You forgot your alphanumeric name?',
+ 'content' => 'You seem to be missing the content.',
+ 'language' => 'Invalid language.'
);
$result = $paste->errors->data();
$this->assertEqual($expected, $result);
diff --git a/tests/integration/PasteTest.php b/tests/integration/PasteTest.php
index a3c5299..6e8ca57 100644
--- a/tests/integration/PasteTest.php
+++ b/tests/integration/PasteTest.php
@@ -193,7 +193,7 @@ class PasteTest extends \lithium\test\Unit {
$conditions = $params['options']['conditions'];
$result = $chain->next($self, $params, $chain);
if ($result === null) {
- MockPaste::createView()->save();
+ MockPaste::create(array('design' => 'latest'))->save();
return null; //MockPaste::find('all', $conditions);
}
return $result;
diff --git a/webroot/css/bin.css b/webroot/css/bin.css
index b8f291b..e0bb6d8 100644
--- a/webroot/css/bin.css
+++ b/webroot/css/bin.css
@@ -4,17 +4,17 @@
}
html {
- height:100%;
+ height:100%;
}
body {
min-height:101%;
}
-html, body {
- margin:0;
- padding:0;
- background:white url(../img/accent.png) no-repeat bottom right fixed;
- color:#333;
- font-family:Helvetica, Arial, sans-serif;
+html, body {
+ margin:0;
+ padding:0;
+ background:white url(../img/accent.png) no-repeat bottom right fixed;
+ color:#333;
+ font-family:Helvetica, Arial, sans-serif;
}
a {
color: #333;
@@ -54,6 +54,9 @@ h3 {
ul.latest, ul.latest li {
margin: 1em;
}
+form {
+ margin-bottom: 40px;
+}
input[type=text] {
display: block;
width: 70%;
@@ -96,8 +99,7 @@ a#toggle {
margin-left: 2em;
}
div#footer {
- position: absolute;
- padding: 30px 0 35px;
+ position: fixed;
text-align: center;
font-size: 70%;
bottom: 0;