Commit: f02f3181d6805baa5f2b86c31d12f64cc5b31f22

Author: alkemann | Date: 2009-11-19 03:13:30 +0100
moving the view logic to seperate model
diff --git a/controllers/PastesController.php b/controllers/PastesController.php index 6752c10..62ed8e3 100644 --- a/controllers/PastesController.php +++ b/controllers/PastesController.php @@ -3,6 +3,7 @@ namespace app\controllers; use \app\models\Paste; +use \app\models\PasteView; /** * Controller that decides what data is available to the different actions (urls) @@ -27,7 +28,7 @@ class PastesController extends \lithium\action\Controller { 'design' => 'latest', 'view' => 'all', 'limit' => '10', 'descending' => 'true' ))); if ($latest === null) { - if (Paste::create(array('design' => 'latest'))->save()) { + if (PasteView::create('latest')->save()) { $latest = Paste::find('all', array('conditions'=> array( 'design' => 'latest', 'view' => 'all', 'limit' => '10', 'descending' => 'true' ))); diff --git a/models/Paste.php b/models/Paste.php index bb85545..15e1c7e 100644 --- a/models/Paste.php +++ b/models/Paste.php @@ -20,13 +20,6 @@ use \lithium\util\Validator; class Paste extends \lithium\data\Model { /** - * public name of the model - * - * @var string - */ - public static $alias = 'Paste'; - - /** * Available languages * * @var array @@ -70,26 +63,6 @@ class Paste extends \lithium\data\Model { ); /** - * Views Document - */ - public static $_views = array( - 'latest' => array('id' => '_design/latest', 'language' => 'javascript', - 'views' => 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 - }); - } - }' - ) - ) - ), - ); - - /** * Apply find and save filter * * Find filter : @@ -123,16 +96,14 @@ class Paste extends \lithium\data\Model { } }); Paste::applyFilter('save', function($self, $params, $chain) { - if ($params['record']->id != '_design/latest') { - $document = $params['record']; - if (in_array($document->language, Paste::languages())) { - $document = Paste::parse($document); - } - $document->preview = rawurlencode(substr($document->content,0,100)); - $document->parsed = rawurlencode($document->parsed); - $document->content = rawurlencode($document->content); - $params['record'] = $document; + $document = $params['record']; + if (in_array($document->language, Paste::languages())) { + $document = Paste::parse($document); } + $document->preview = rawurlencode(substr($document->content,0,100)); + $document->parsed = rawurlencode($document->parsed); + $document->content = rawurlencode($document->content); + $params['record'] = $document; return $chain->next($self, $params, $chain); }); } @@ -173,12 +144,6 @@ 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']; } diff --git a/models/PasteView.php b/models/PasteView.php new file mode 100644 index 0000000..dcf0f2e --- /dev/null +++ b/models/PasteView.php @@ -0,0 +1,51 @@ +<?php + +namespace app\models; + +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 static $_views = array( + 'latest' => array( + 'id' => '_design/latest', + 'language' => 'javascript', + 'views' => 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 + }); + } + }' + ) + ) + ), + ); + + public static function create($data = 'latest') { + if (is_string($data) && isset(static::$_views[$data])) { + return parent::create(static::$_views[$data]); + } elseif (is_array($data)) { + return parent::create($data); + } + return false; + } + + +} + +?> \ No newline at end of file