Commit: 97c3ac1b2f60dcf927dc7fa9dd69448e25da034d

Author: gwoo | Date: 2009-12-07 22:59:37 -0800
refactoring code
diff --git a/config/routes.php b/config/routes.php index d99bc0a..b2652b7 100644 --- a/config/routes.php +++ b/config/routes.php @@ -8,35 +8,21 @@ use \lithium\http\Router; -/** - * Uncomment the line below to enable routing for admin actions. - * @todo Implement me. - */ -// Router::namespace('/admin', array('admin' => true)); - -Router::connect('/test/{:args}', array('controller' => '\lithium\test\Controller')); -Router::connect('/test', array('controller' => '\lithium\test\Controller')); - Router::connect('/', array('controller' => 'pastes', 'action' => 'add')); -Router::connect('/latest', array('controller' => 'pastes', 'action' => 'index')); -Router::connect('/pastes/index/page:{:page:[0-9]+}', array('controller' => 'pastes', 'action' => 'index'), array('page' => 1)); -Router::connect('/pastes/index/page:{:page:[0-9]+}/limit:{:limit:[0-9]+}', array('controller' => 'pastes', 'action' => 'index'), array('page' => 1, 'limit' => 10)); +Router::connect('/latest', array('controller' => 'pastes', 'action' => 'index')); +Router::connect('/latest/page:{:page:[0-9]+}', array( + 'controller' => 'pastes', 'action' => 'index', 'page' => 1 +)); +Router::connect('/latest/page:{:page}/limit:{:limit}', array( + 'controller' => 'pastes', 'action' => 'index', 'page' => 1, 'limit' => 10 +)); Router::connect('/add/{:args}', array('controller' => 'pastes', 'action' => 'add')); Router::connect('/edit/{:args}', array('controller' => 'pastes', 'action' => 'edit')); Router::connect('/view/{:args}', array('controller' => 'pastes', 'action' => 'view')); +Router::connect('/test/{:args}', array('controller' => '\lithium\test\Controller')); +Router::connect('/test', array('controller' => '\lithium\test\Controller')); -/** - * ...and connect the rest of 'Pages' controller's urls. - */ -Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'view')); - -/** - * Finally, connect the default routes. - */ -Router::connect('/{:controller}/{:action}/{:id:[0-9]+}.{:type}', array('id' => null)); -Router::connect('/{:controller}/{:action}/{:id:[0-9]+}'); -Router::connect('/{:controller}/{:action}/{:args}'); ?> \ No newline at end of file diff --git a/controllers/PastesController.php b/controllers/PastesController.php index 3be7797..d9accae 100644 --- a/controllers/PastesController.php +++ b/controllers/PastesController.php @@ -5,7 +5,6 @@ namespace app\controllers; use \app\models\Paste; use \app\models\PasteView; use \lithium\storage\Session; -use \lithium\data\Connections; /** * Controller that decides what data is available to the different actions (urls) @@ -20,39 +19,25 @@ use \lithium\data\Connections; class PastesController extends \lithium\action\Controller { /** - * Run once (or until get OK message) to setup database) - * - * @return array - */ - public function install() { - Connections::get('default')->put('/'.Paste::meta('source')); - PasteView::create()->save(); - $view = PasteView::find('_design/paste'); - return compact('view'); - } - - /** * Asks the model for the data to be rendered at /latest * showing the 10 latest pastes made. * * @return array */ public function index() { - $options = array( - 'design' => 'paste', 'view' => 'all', 'limit' => 4, 'descending' => 'true' - ); $page = 1; + $limit = 10; + $order = array('descending' => 'true'); if (isset($this->request->params['page'])) { - if (isset($this->request->params['limit'])) { - $options['limit'] = $this->request->params['limit']; - } - $options['skip'] = ($this->request->params['page']-1) * $options['limit']; $page = $this->request->params['page']; + if (!empty($this->request->params['limit'])) { + $limit = $this->request->params['limit']; + } } - $limit = $options['limit']; - $latest = Paste::find('all',array('conditions' => $options)); + $conditions = array('design' => 'paste', 'view' => 'all', 'skip' => ($page - 1) * $limit); $total = Paste::find('count'); - return compact('latest','limit','page','total'); + $latest = Paste::find('all', compact('conditions', 'limit', 'order')); + return compact('latest', 'limit', 'page', 'total'); } /** @@ -70,8 +55,7 @@ class PastesController extends \lithium\action\Controller { if ($paste == null) { $this->redirect(array('controller' => 'pastes', 'action' => 'index')); } - $binJs = true; - return compact('paste','binJs'); + return compact('paste'); } /** @@ -91,30 +75,20 @@ class PastesController extends \lithium\action\Controller { $data = (array) json_decode($saved); } else { $data = compact('author', 'language'); - } + } $paste = Paste::create($data); } else { $paste = Paste::create($this->request->data); - $remember = $paste->remember; - unset($paste->remember); if ($paste->save()) { - if ($remember) { - Session::write('paste', json_encode(array( - 'author' => $paste->author, - 'permanent' => ($paste->permanent == "1"), - 'remember' => true, - 'language' => $paste->language - ))); - } else { - Session::write('paste', null); - } + $this->_remember($paste); $this->redirect(array( 'controller' => 'pastes', 'action' => 'view', 'args' => array($paste->id) )); } } $languages = Paste::languages(); - $this->set(compact('paste', 'languages')); + $url = array('controller' => 'pastes', 'action' => 'add'); + $this->set(compact('url', 'paste', 'languages')); $this->render('form'); } @@ -133,34 +107,38 @@ class PastesController extends \lithium\action\Controller { if (empty($this->request->data)) { $paste = Paste::find($id); if ($paste == null) { - $this->redirect(array( - 'controller' => 'pastes', 'action' => 'add' - )); + $this->redirect(array('controller' => 'pastes', 'action' => 'add')); } } else { $paste = Paste::find($this->request->data['id']); - $remember = $paste->remember; - unset($paste->remember); if ($paste && $paste->save($this->request->data)) { - if ($remember) { - Session::write('paste', json_encode(array( - 'author' => $paste->author, - 'permanent' => ($paste->permanent == "1"), - 'remember' => true, - 'language' => $paste->language - ))); - } else { - Session::write('paste', null); - } + $this->_remember($paste); $this->redirect(array( 'controller' => 'pastes', 'action' => 'view', 'args' => array($paste->id) )); } } $languages = Paste::languages(); - $this->set(compact('paste', 'languages')); + $url = array('controller' => 'pastes', 'action' => 'edit', 'args' => array($paste->id)); + $this->set(compact('url', 'paste', 'languages')); $this->render('form'); } + + /** + * Remember the current user of the paste + * + * @param object $paste + * @return void + */ + protected function _remember($paste) { + if (!empty($this->request->data['remember'])) { + Session::write('paste', json_encode(array( + 'author' => $paste->author, 'remember' => true, 'language' => $paste->language + ))); + return; + } + Session::write('paste', null); + } } ?> \ No newline at end of file diff --git a/extensions/adapters/empty b/extensions/adapters/empty deleted file mode 100644 index e69de29..0000000 diff --git a/extensions/behaviors/empty b/extensions/behaviors/empty deleted file mode 100644 index e69de29..0000000 diff --git a/extensions/command/Bin.php b/extensions/command/Bin.php new file mode 100644 index 0000000..2b009e4 --- /dev/null +++ b/extensions/command/Bin.php @@ -0,0 +1,49 @@ +<?php + +namespace app\extensions\command; + +use \app\models\Paste; +use \app\models\PasteView; + +/** + * Command to assist in setup and management of Lithium Bin + * + */ +class Bin extends \lithium\console\Command { + + /** + * Run the install method to create database and views + * + * @return boolea + */ + public function install() { + $this->header('Lithium Bin'); + $result = Paste::install(); + PasteView::create()->save(); + $view = PasteView::find('_design/paste'); + if (!empty($view->reason)) { + switch($view->reason) { + case 'no_db_file': + $this->out(array( + 'Database does not exist.', + 'Please make sure CouchDB is running and refresh to try again.' + )); + break; + case 'missing': + $this->out(array( + 'Database created.', 'Design views were not created.', + 'Please run the command again.' + )); + break; + } + } + if (isset($view->id) && $view->id == '_design/paste' && count($view->views) == 2) { + $this->out('Installation successful.'); + return true; + } + return false; + } + +} + +?> \ No newline at end of file diff --git a/extensions/commands/empty b/extensions/commands/empty deleted file mode 100644 index e69de29..0000000 diff --git a/extensions/components/empty b/extensions/components/empty deleted file mode 100644 index e69de29..0000000 diff --git a/extensions/data/source/http/adapter/Couch.php b/extensions/data/source/http/adapter/Couch.php deleted file mode 100644 index c908c09..0000000 --- a/extensions/data/source/http/adapter/Couch.php +++ /dev/null @@ -1,19 +0,0 @@ -<?php - -namespace app\extensions\data\source\http\adapter; - -class Couch extends \lithium\data\source\Http { - - protected function _prepare($data = array()) { - return parent::_prepare(json_encode($data)); - } - - protected function _send($path = null) { - $this->request->headers('Content-Type', 'application/json'); - $data = parent::_send($path); - if (get_magic_quotes_gpc() == true) { - $data = stripslashes($data); - } - return json_decode($data); - } -} \ No newline at end of file diff --git a/extensions/g11n/empty b/extensions/g11n/empty deleted file mode 100644 index e69de29..0000000 diff --git a/extensions/helpers/empty b/extensions/helpers/empty deleted file mode 100644 index e69de29..0000000 diff --git a/libraries/empty b/libraries/empty deleted file mode 100644 index e69de29..0000000 diff --git a/models/Paste.php b/models/Paste.php index fce117e..3c8c917 100644 --- a/models/Paste.php +++ b/models/Paste.php @@ -5,6 +5,7 @@ namespace app\models; use \Geshi; use \lithium\core\Libraries; use \lithium\util\Validator; +use \lithium\data\Connections; /** * Data model for access the pastebin documents. @@ -52,11 +53,13 @@ class Paste extends \lithium\data\Model { public $validates = array( 'content' => 'You seem to be missing the content.', 'author' => array( - 'rule' => 'isAlphaNumeric', 'message' => 'You forgot your alphanumeric name?'), + 'rule' => 'isAlphaNumeric', 'message' => 'You forgot your alphanumeric name?' + ), 'language' => array( - 'rule' => 'validLanguage', 'message' => 'Invalid language.') + 'rule' => 'validLanguage', 'message' => 'Invalid language.' + ) ); - + /** * Init method called by `Libraries::load()`. It applies filters on the save method. * @@ -79,25 +82,26 @@ class Paste extends \lithium\data\Model { parent::__init($options); $self = static::_instance(); $self->_finders['count'] = function($self, $params, $chain) { - $http = new \lithium\data\source\Http(array( - 'host' => '127.0.0.1', - 'port' => '5984' - )); - $result = json_decode($http->get('/'.$self::meta('source').'/_design/paste/_view/count')); - return $result->total_rows; + $result = Connections::get('default')->get( + Paste::meta('source') . '/_design/paste/_view/count' + ); + if (empty($result->total_rows)) { + return 0; + } + return $result->total_rows; }; Paste::applyFilter('save', function($self, $params, $chain) { - if (empty($params['data'])) { - $document = $params['record']; - $document->parsed = Paste::parse($document->content, $document->language); - $document->preview = substr($document->content,0,100); + $document = $params['record']; + if (!$document->id) { $document->created = date('Y-m-d h:i:s'); - $params['record'] = $document; - } else { - $params['data']['preview'] = substr($params['data']['content'],0,100); - $params['data']['parsed'] = Paste::parse($params['data']['content'], $params['data']['language']); - $params['data']['modified'] = date('Y-m-d h:i:s'); } + if (!empty($params['data'])) { + $document->set($params['data']); + } + $document->parsed = Paste::parse($document->content, $document->language); + $document->preview = substr($document->content, 0, 100); + $document->modified = date('Y-m-d h:i:s'); + $params['record'] = $document; return $chain->next($self, $params, $chain); }); Validator::add('validLanguage', function ($value, $format, $options) { @@ -120,7 +124,7 @@ class Paste extends \lithium\data\Model { return $content; } $language = 'text'; - } + } $geshi = new GeSHi($content, $language); $geshi->enable_classes(); $geshi->enable_keyword_links(false); @@ -146,6 +150,14 @@ class Paste extends \lithium\data\Model { return static::$languages; } + /** + * Creates a new database + * + * @return void + */ + public static function install() { + return Connections::get('default')->put(static::meta('source')); + } } ?> \ No newline at end of file diff --git a/models/PasteView.php b/models/PasteView.php index 7b4a66f..6968f4e 100644 --- a/models/PasteView.php +++ b/models/PasteView.php @@ -1,62 +1,65 @@ -<?php - -namespace app\models; - -/** - * This model is used to store Couch design views to the `Paste` database - * It also defines it. Do not call a 'find' on this model. To view the view, use - * the 'design' condition in a 'find' call on the `Paste` model, ie : - * {{{ - * $latest = Paste::find('all', array('conditions' => array( - * 'design' => 'latest, - * 'limit' => 10 - * ))); - * }}} - * - * When the find call in the example above returns a NULL, that means the view does not - * exist in the `Paste` database. To insert it use: - * {{{ - * PasteView::create()->save(); - * }}} - */ -class PasteView extends \lithium\data\Model { - - /** - * Metadata - * - * @var array array of meta data to link the model with the couchdb datasource - * - source : the name of the table (called database in couchdb) - */ - protected $_meta = array( - 'source' => 'lithium_bin' - ); - - /** - * Predefined views. Only used to store in db if not already there. - */ - protected $_schema = array( - 'id' => array('default' => '_design/paste'), - 'language' => array('default' => 'javascript'), - 'views' => array('default' => array( - 'all' => array( -'map' => 'function(doc) { - if (doc.permanent == "1") { - emit(doc.created, { - author: doc.author, language: doc.language, - preview: doc.preview, created: doc.created - }); - } -}'), - 'count' => array( -'map' => 'function(doc) { - if (doc.permanent == "1") { - emit(doc._id, null); - } -}'), - )) - ); - - -} - +<?php + +namespace app\models; + +/** + * This model is used to store Couch design views to the `Paste` database + * It also defines it. Do not call a 'find' on this model. To view the view, use + * the 'design' condition in a 'find' call on the `Paste` model, ie : + * {{{ + * $latest = Paste::find('all', array('conditions' => array( + * 'design' => 'latest, + * 'limit' => 10 + * ))); + * }}} + * + * When the find call in the example above returns a NULL, that means the view does not + * exist in the `Paste` database. To insert it use: + * {{{ + * PasteView::create()->save(); + * }}} + */ +class PasteView extends \lithium\data\Model { + + /** + * Metadata + * + * @var array array of meta data to link the model with the couchdb datasource + * - source : the name of the table (called database in couchdb) + */ + protected $_meta = array( + 'source' => 'lithium_bin' + ); + + /** + * Predefined views. Only used to store in db if not already there. + */ + protected $_schema = array( + 'id' => array('default' => '_design/paste'), + 'language' => array('default' => 'javascript'), + 'views' => array('default' => array( + 'all' => array( +'map' => 'function(doc) { + if (doc.permanent == "1") { + emit(doc.created, { + author: doc.author, language: doc.language, + preview: doc.preview, created: doc.created + }); + } +}' + ), + 'count' => array( +'map' => 'function(doc) { + if (doc.permanent == "1") { + emit(doc._id, null); + } +}' + ), + + )) + ); + + +} + ?> \ No newline at end of file diff --git a/tests/cases/models/PasteTest.php b/tests/cases/models/PasteTest.php index 5e4c3dd..bc806fa 100644 --- a/tests/cases/models/PasteTest.php +++ b/tests/cases/models/PasteTest.php @@ -5,6 +5,7 @@ namespace app\tests\cases\models; use \app\tests\mocks\models\MockPaste; class PasteTest extends \lithium\test\Unit { + public function testUsesDocument() { $paste = new MockPaste(); diff --git a/tests/cases/models/PasteViewTest.php b/tests/cases/models/PasteViewTest.php index a309230..15c4082 100644 --- a/tests/cases/models/PasteViewTest.php +++ b/tests/cases/models/PasteViewTest.php @@ -1,64 +1,62 @@ -<?php - -namespace app\tests\cases\models; - -use \app\tests\mocks\models\MockPaste; -use \app\tests\mocks\models\MockPasteView; - -class PasteViewTest extends \lithium\test\Unit { - - - - public function testCreate() { - $view = MockPasteView::create(); - - $result = $view instanceof \lithium\data\model\Document; - $this->assertTrue($result); - $this->skipIf(!$result, 'Not a Document result'); - - $expected = '_design/paste'; - $result = $view->id; - $this->assertEqual($expected, $result); - - $this->assertFalse(is_null($view->language)); - $this->assertFalse(is_null($view->views)); - } - - public function testCreateLatest() { - $view = MockPasteView::create(); - - $result = $view instanceof \lithium\data\model\Document; - $this->assertTrue($result); - $this->skipIf(!$result, 'Not a Document result'); - - $expected = '_design/paste'; - $result = $view->id; - $this->assertEqual($expected, $result); - - $expected = 'javascript'; - $result = $view->language; - $this->assertEqual($expected, $result); - - $expected = array( - 'all' => array( -'map' => 'function(doc) { - if (doc.permanent == "1") { - emit(doc.created, { - author: doc.author, language: doc.language, - preview: doc.preview, created: doc.created - }); - } -}'), - 'count' => array( -'map' => 'function(doc) { - if (doc.permanent == "1") { - emit(doc._id, null); - } -}'), - ); - $result = $view->views->data(); - $this->assertEqual($expected, $result); - } -} - +<?php + +namespace app\tests\cases\models; + +use \app\tests\mocks\models\MockPaste; +use \app\tests\mocks\models\MockPasteView; + +class PasteViewTest extends \lithium\test\Unit { + + public function testCreate() { + $view = MockPasteView::create(); + + $result = $view instanceof \lithium\data\model\Document; + $this->assertTrue($result); + $this->skipIf(!$result, 'Not a Document result'); + + $expected = '_design/paste'; + $result = $view->id; + $this->assertEqual($expected, $result); + + $this->assertFalse(is_null($view->language)); + $this->assertFalse(is_null($view->views)); + } + + public function testCreateLatest() { + $view = MockPasteView::create(); + + $result = $view instanceof \lithium\data\model\Document; + $this->assertTrue($result); + $this->skipIf(!$result, 'Not a Document result'); + + $expected = '_design/paste'; + $result = $view->id; + $this->assertEqual($expected, $result); + + $expected = 'javascript'; + $result = $view->language; + $this->assertEqual($expected, $result); + + $expected = array( + 'all' => array( +'map' => 'function(doc) { + if (doc.permanent == "1") { + emit(doc.created, { + author: doc.author, language: doc.language, + preview: doc.preview, created: doc.created + }); + } +}'), + 'count' => array( +'map' => 'function(doc) { + if (doc.permanent == "1") { + emit(doc._id, null); + } +}'), + ); + $result = $view->views->data(); + $this->assertEqual($expected, $result); + } +} + ?> \ No newline at end of file diff --git a/tests/fixtures/empty b/tests/fixtures/empty deleted file mode 100644 index e69de29..0000000 diff --git a/tests/groups/empty b/tests/groups/empty deleted file mode 100644 index e69de29..0000000 diff --git a/tests/integration/PasteTest.php b/tests/integration/PasteTest.php index 3aa10c9..413b032 100644 --- a/tests/integration/PasteTest.php +++ b/tests/integration/PasteTest.php @@ -133,14 +133,14 @@ class PasteTest extends \lithium\test\Unit { public function testCount() { $this->_tasks(array('PutTable','FillTableFull')); - + $expected = 11; $result = MockIntegrationPaste::find('count'); - $this->assertEqual($expected, $result); - + $this->assertEqual($expected, $result); + $this->_tasks(array('DeleteTable')); } - + /** TEST SETUPS **/ protected function _tasks($tasks) { @@ -150,7 +150,7 @@ class PasteTest extends \lithium\test\Unit { } protected function _taskPutTable() { - Connections::get("test")->put('/test_pastes'); + Connections::get("test")->put('test_pastes'); MockIntegrationPasteView::create()->save(); } @@ -170,10 +170,11 @@ class PasteTest extends \lithium\test\Unit { $paste = MockIntegrationPaste::create($data); $paste->save(); } - + protected function _taskFillTableFull() { $data = array( 'id' => 'a1', + 'rev' => '1-1', 'author' => 'alkemann', 'created' => '2009-01-01 01:01:10', 'language' => 'text', diff --git a/views/pastes/form.html.php b/views/pastes/form.html.php index 0aba824..f95c6f1 100644 --- a/views/pastes/form.html.php +++ b/views/pastes/form.html.php @@ -1,5 +1,5 @@ <?php -echo $this->form->create($paste, array('method' => 'POST')); +echo $this->form->create($paste, array('url' => $url, 'method' => 'POST')); $errors = $paste->errors(); diff --git a/views/pastes/index.html.php b/views/pastes/index.html.php index 1fccdd2..8bdb19b 100644 --- a/views/pastes/index.html.php +++ b/views/pastes/index.html.php @@ -12,24 +12,32 @@ endif; <?=$row->author?> @ <?=$row->created?> &middot; <?=$row->language?> - <?php echo $this->html->link('view', '/view/' . $row->id)?> + <?php echo $this->html->link('view', array( + 'controller' => 'pastes', 'action' => 'view', 'args' => array($row->id) + ));?> <p><?=$row->preview?></p> </li> <?php endforeach;?> </ul> <ul id="actions"> - <li><?php + <li><?php if ($total <= $limit || $page == 1) { echo '<<-First</li><li><-Previous'; } else { - echo $this->html->link('<<-First', array('action' => 'index', 'args' => array('page:1','limit:'.$limit))); + echo $this->html->link('<<-First', array( + 'controller' => 'pastes', 'action' => 'index', + 'page' => 1, 'limit' => $limit + )); echo '</li><li>'; - echo $this->html->link('<-Previous', array('action' => 'index', 'args' => array('page:'.($page-1),'limit:'.$limit))); - + echo $this->html->link('<-Previous', array( + 'controller' => 'pastes', 'action' => 'index', + 'page' => $page - 1, 'limit' => $limit + )); + } ?> </li> - <?php - + <?php + $p = 0; $count = $total; while ($count > 0) { $p++; $count -= $limit; @@ -37,18 +45,27 @@ endif; if ($p == $page) { echo '['.$p.']'; } else { - echo $this->html->link('['.$p.']', array('action' => 'index', 'args' => array('page:'.$p,'limit:'.$limit))); + echo $this->html->link('['.$p.']', array( + 'controller' => 'pastes', 'action' => 'index', + 'page' => $p, 'limit' => $limit + )); } echo '</li>'; } - ?> - <li><?php + ?> + <li><?php if ($total <= $limit || $page == $p) { echo 'Next-></li><li>Last->>'; } else { - echo $this->html->link('Next->', array('action' => 'index', 'args' => array('page:'.($page+1),'limit:'.$limit))); + echo $this->html->link('Next->', array( + 'controller' => 'pastes', 'action' => 'index', + 'page' => $page + 1, 'limit' => $limit + )); echo '</li><li>'; - echo $this->html->link('Last->>', array('action' => 'index', 'args' => array('page:'.$p,'limit:'.$limit))); + echo $this->html->link('Last->>', array( + 'controller' => 'pastes', 'action' => 'index', + 'page' => $total, 'limit' => $limit + )); }?> </li> </ul> \ No newline at end of file diff --git a/views/pastes/install.html.php b/views/pastes/install.html.php deleted file mode 100644 index 9611897..0000000 --- a/views/pastes/install.html.php +++ /dev/null @@ -1,23 +0,0 @@ -<h2>Install</h2> -<br> -<?php - - if (isset($view->reason) && $view->reason == 'no_db_file') { - echo '<h2 style="color:red;">'; - echo 'Database does not exist. Please make sure CouchDB is running and refresh to try again.'; - echo '</h2>'; - echo $this->html->link('REFRESH',array('action' => 'install')); - } elseif (isset($view->reason) && $view->reason == 'missing') { - echo '<h2 style="color:red;">'; - echo 'Database exist, but design views not. Please refresh to try again.'; - echo '</h2>'; - echo $this->html->link('REFRESH',array('action' => 'install')); - } elseif(isset($view->id) && $view->id == '_design/paste' && count($view->views) == 2) { - echo '<h2 style="color:green;">'; - echo 'Everything is OK.'; - echo '</h2>'; - echo $this->html->link('Add a paste',array('action' => 'add')); - } - - - ?> \ No newline at end of file