[Catalyst] To plugin or not to plugin; style/best-practices
Brandon Black
blblack at gmail.com
Wed Dec 14 20:02:20 CET 2005
On 12/14/05, David K Storrs <dstorrs at dstorrs.com> wrote:
>
> On Dec 13, 2005, at 7:40 PM, apv wrote:
>
> > On Tuesday, December 13, 2005, at 04:34 PM, Matt S Trout wrote:
> >> On Tue, Dec 13, 2005 at 04:10:52PM -0800, apv wrote:
> >>> I'm working on a user supplied comment. I want to apply some auto
> >>> formatting to it (paras but with somewhat complicated rules for
> >>> where
> >>> they go, no simple splitting on "\n\n," so it's not going to be very
> >>> compact code) in addition to HTML sanitizing.
> >>>
> >>> Should this be a plugin or a controller? If a plugin makes sense, is
> >>> there one already out there I should look at for good learning code?
> >>
> >> Sounds like view logic to me :)
> > I rather agree in part but it's information that is going into the
> > DB as opposed
> > to--directly, anyway--the screen.
>
> In that case, shouldn't it be in the model? Have the Controller send
> raw text with \n\n and let the Model class sort out what to write to
> the DB.
>
You could go a lot of ways with this since it is such a wierd case.
Personally, I think I would probably transform the raw user comment
into a valid HTML representation in the Controller which receives the
user comment data, before it is inserted into the Model which doesn't
know about such things. And then if it needed to be automagic so that
you don't duplicate code all over multiple Controllers, I'd make a
plugin or module out of the code. If you receive user comments in
many different controllers, but use the same standardized parameter
name for the data everywhere, a Plugin might make sense, which can
look for $c->req->param->{user_comment} and autoformat it before a
controller sees it. OTOH, you could also make a straight up non-OO
module MyApp::CommentFormatter which exports a "format_comment()"
function which various controllers can use to do the magic when one of
their parameters is a user comment.
Perhps a more correct way to do it (but not arguably the most
practical) would be to have the formatter module/plugin mentioned
above merely validate and parse the comment into some internal
representation that is neutral wrt specific display formats like HTML,
PDF, etc, and then store it into the Model in that neutral format
(say, a special xml dtd you invent for user comments). Then various
Views can in turn process the neutral format into whatever they need
to see it as. But you won't see any practical benefit to going
through all that trouble until or if you ever have another type of
View on user comments.
-- Brandon
More information about the Catalyst
mailing list