From b1babc5fbdaf0cfe573d1a126b9a6d88deb64352 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Wed, 19 Oct 2011 21:37:33 -0600 Subject: [PATCH 1/1] basic (non-AJAX) chat implemented --- lib/Chatty/Controller/Chat.pm | 28 ++++++++++++++++++++++++++++ lib/Chatty/Form/MessageCreate.pm | 13 +++++++++++++ root/tt/chat/view.tt | 4 ---- 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 lib/Chatty/Form/MessageCreate.pm diff --git a/lib/Chatty/Controller/Chat.pm b/lib/Chatty/Controller/Chat.pm index 9be0e79..73fe274 100644 --- a/lib/Chatty/Controller/Chat.pm +++ b/lib/Chatty/Controller/Chat.pm @@ -7,6 +7,7 @@ BEGIN { extends 'Catalyst::Controller' } #__PACKAGE__->config(namespace => 'room'); use Chatty::Form::RoomCreate; +use Chatty::Form::MessageCreate; has 'roomcreate_form' => ( isa => 'Chatty::Form::RoomCreate', @@ -15,6 +16,13 @@ has 'roomcreate_form' => ( default => sub { Chatty::Form::RoomCreate->new } ); +has 'messagecreate_form' => ( + isa => 'Chatty::Form::MessageCreate', + is => 'rw', + lazy => 1, + default => sub { Chatty::Form::MessageCreate->new } +); + =head1 NAME Chatty::Controller::Chat - Catalyst Controller @@ -93,6 +101,26 @@ sub view :Chained(room) :PathPart('') :Args(1) { $c->detach('/missing') if !$c->stash->{room}; $c->stash(messages => [$c->model('DB::Message')->search(room => $room)]); + + $c->stash(form => $self->messagecreate_form); + + my $new_message = $c->model('DB::Message')->new_result({ + author => $c->user->obj->id, + room => $c->stash->{room}->id + }); + $self->messagecreate_form->process( + item => $new_message, + params => $c->req->params + ); + + if (!$self->messagecreate_form->is_valid) { + if ($c->req->method eq 'POST') { + $c->stash->{error} = "The form has a validation error. Try again..."; + } + return; + } + + $c->res->redirect($c->uri_for_action('/chat/view', $c->stash->{room}->id)); } =head1 AUTHOR diff --git a/lib/Chatty/Form/MessageCreate.pm b/lib/Chatty/Form/MessageCreate.pm new file mode 100644 index 0000000..72b616e --- /dev/null +++ b/lib/Chatty/Form/MessageCreate.pm @@ -0,0 +1,13 @@ +package Chatty::Form::MessageCreate; + +use HTML::FormHandler::Moose; +extends 'HTML::FormHandler::Model::DBIC'; +use namespace::autoclean; + +has '+item_class' => (default => 'Message'); + +has_field 'content' => (input_class => 'validate[required]', label => 'Message', required => 1); +has_field 'submit' => (type => 'Submit', value => 'Create'); + +__PACKAGE__->meta->make_immutable; +1; diff --git a/root/tt/chat/view.tt b/root/tt/chat/view.tt index bfb4590..19fe0c0 100644 --- a/root/tt/chat/view.tt +++ b/root/tt/chat/view.tt @@ -3,11 +3,7 @@ $('form').validationEngine(); [% END -%]

Room: [% room.name %]

-[% IF messages.count == 0 -%] [% FOREACH msg IN messages -%]

[% msg.author.username %] ([% msg.posted %]): [% msg.content %]

[% END -%] -[% ELSE -%] -

No messages, yet.

-[% END -%] [% form.render -%] -- 2.43.0