basic (non-AJAX) chat implemented
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 20 Oct 2011 03:37:33 +0000 (21:37 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 20 Oct 2011 04:01:15 +0000 (22:01 -0600)
lib/Chatty/Controller/Chat.pm
lib/Chatty/Form/MessageCreate.pm [new file with mode: 0644]
root/tt/chat/view.tt

index 9be0e796e28449eefb4e31fbaad02c3e7cef997d..73fe2744e99c32ecfcfba61b6a222feabad8857c 100644 (file)
@@ -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 (file)
index 0000000..72b616e
--- /dev/null
@@ -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;
index bfb459018436831f5d5806e28cdedb90432a7045..19fe0c0d4c49d2e0ad9770f95e9d1d9b6b1ae83a 100644 (file)
@@ -3,11 +3,7 @@
 $('form').validationEngine();
 [% END -%]
 <h1>Room: [% room.name %]</h1>
-[% IF messages.count == 0 -%]
 [% FOREACH msg IN messages -%]
 <p>[% msg.author.username %] ([% msg.posted %]): [% msg.content %]</p>
 [% END -%]
-[% ELSE -%]
-<p>No messages, yet.</p>
-[% END -%]
 [% form.render -%]
This page took 0.027335 seconds and 4 git commands to generate.