Commit: 68a6d3b80e88afccee5098c8e81b4e64a0935f8d

Author: alkemann | Date: 2009-11-17 19:53:25 +0100
filters for `Paste` with tests
diff --git a/models/Paste.php b/models/Paste.php index 40f21f9..c8d7d23 100644 --- a/models/Paste.php +++ b/models/Paste.php @@ -83,6 +83,57 @@ class Paste extends \lithium\data\Model { ); /** + * Apply find and save filter + */ + public static function __init($options = array()) { + parent::__init($options); + static::applyFilter('find', function($self, $params, $chain) { + if ($params['options']['conditions']['design'] = 'latest') { + $conditions = $params['options']['conditions']; + $result = $chain->next($self, $params, $chain); + if ($result === null) { + static::createView()->save(); + return null; //static::find('all', $conditions); + } + return $result; + } else { + return $chain->next($self, $params, $chain); + } + }); + static::applyFilter('save', function($self, $params, $chain) { + $document = $params['record']; + if ($document->language != 'text' && + in_array($document->language, static::$languages)) { + $document = static::parse($document); + } + $doc->parsed = rawurlencode($doc->parsed); + $doc->content = rawurlencode($doc->content); + return $document ; + }); + } + + /** + * Takes a reference to a Document, and parses the content + * + * @param Document $doc + * @return Document + */ + public static function &parse(&$doc) { + 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(); diff --git a/tests/cases/models/PasteTest.php b/tests/cases/models/PasteTest.php index 49036c7..e2f094f 100644 --- a/tests/cases/models/PasteTest.php +++ b/tests/cases/models/PasteTest.php @@ -15,7 +15,7 @@ class MockPaste extends \app\models\Paste { 'connections' => '\lithium\data\Connections' ); - protected $_meta = null; + protected $_meta = array(); public function classes() { return $this->_classes; @@ -32,7 +32,6 @@ class MockPaste extends \app\models\Paste { class PasteTest extends \lithium\test\Unit { - public function testUsesDocument() { $paste = new MockPaste(); @@ -198,6 +197,30 @@ class PasteTest extends \lithium\test\Unit { $this->assertEqual($expected, $result->parsed); } + public function testGeShiFilter() { + MockPaste::applyFilter('save', function($self, $params, $chain) { + $document = $params['record']; + if ($document->language != 'text' && + in_array($document->language, MockPaste::$languages)) { + $document = \app\models\Paste::parse($document); + } + return $document ; + }); + + $data = array( + 'content' => 'echo', + 'author' => 'TomGood', + 'language' => 'php' + ); + $paste = MockPaste::create($data); + $doc = $paste->save(); + + $expected = '<pre class="php" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="kw1">echo</span></div></li></ol></pre>'; + $result = $doc->parsed; + $this->assertEqual($expected, $result); + + } + } ?> \ No newline at end of file