<?xml version="1.0" encoding="windows-1252"?>
<node id="773350" title="CatalystX::Comments - RFC" created="2009-06-21 05:59:12" updated="2009-06-21 05:59:12">
<type id="120">
perlmeditation</type>
<author id="228394">
zby</author>
<data>
<field name="doctext">
This is a repost from my blog.  In &lt;a href="http://perlalchemy.blogspot.com/2009/06/packaging-cross-cutting-catalyst.html"&gt;Packaging cross cutting Catalyst features&lt;/a&gt; I promised to start developing a generic Catalyst comment sub-system.  Now I have incorporated the code samples into another experiment - &lt;a href="http://github.com/gshank/ravlog/tree/master"&gt;ravlog&lt;/a&gt; (a blog engine by Gerda Shank), and I would like to ask you what you think about it before releasing it to CPAN.
&lt;p&gt;
Here is the current API - any comments are welcome. First you need to declare the controller using the new Moose style:
&lt;code&gt;
package RavLog::Controller::View;

use Moose;
BEGIN {
   extends 'Catalyst::Controller';
   with 'CatalystX::Comments::ControllerFormRole';
}

has model_name =&gt; ( is =&gt; 'ro', isa =&gt; 'Str', default =&gt; 'DB' );
&lt;/code&gt;

The important part for us is of course &lt;code&gt;with 'CatalystX::Comments::ControllerFormRole'&lt;/code&gt;, the &lt;code&gt;model_name&lt;/code&gt; attribute should hold the name of the model CatalystX::Comments will use to store the comments.
After the declarations you can use CatalystX::Comments to put the comment form on the stash:
&lt;code&gt;
sub view : Chained('base') PathPart('view') Args(0)
{
   my ( $self, $c, $id ) = @_;

...
   
   $self-&gt;stash_comment_form( $c, $self-&gt;article-&gt;id );
}
&lt;/code&gt;
That's the whole controller part - what it handles is generating the form for display and saving the submitted comments.  In the view templates you need to add: 
&lt;code&gt;
[% comment_form.render %]
&lt;/code&gt;
For displaying the comments themselves ravlog already had some template code and I did not change it.
&lt;p&gt;
Next is the model part.  The library assumes that the DBIC model contains a Comment Result class like &lt;a href="http://github.com/gshank/ravlog/blob/2c88dd45416b4ad15a7ec6ca7309d72aa6181279/lib/RavLog/Schema/DB/Comment.pm"&gt;that one in ravlog&lt;/a&gt;.  Of course it does not need to have all of the columns, and can also have other columns - but these are the columns that will be filled by the CatalystX::Comments form.  Ideally that model part should be a Result base class - so that it can be subclassed and adapted to local needs (for example the belongs_to relationship to article needs to be changed), but for now &lt;a href="http://lists.scsys.co.uk/pipermail/dbix-class/2009-June/007984.html"&gt;I don't know how to do that&lt;/a&gt;.
&lt;p&gt;
Now some more details. Thanks to &lt;code&gt;html_prefix&lt;/code&gt; all parameters sent from this form are prefixed with the form name - so this form can be added to pages with other forms, it will recognize it's parameters.  The comments are saved only when the HTTP method used is POST, and after a successful comment creation the user is redirected to the same page (this seems a bit constraining - but see next paragraph - this is only meant to be temporary solution - I try to keep the API simple).
&lt;p&gt;
Of course I understand that this can never cover the needs of a comment sub-system in a mature social web site.  I am thinking about it as more of &lt;a href="http://perlalchemy.blogspot.com/2009/06/on-scaffoldings.html"&gt;scaffolding&lt;/a&gt; - code that let's you quickly develop a feature, see how it integrates with the rest of the user experience, formulate a more complete requirements list - and replace it part by part.</field>
</data>
</node>
