On 23/01/07, Guillermo Roditi <[EMAIL PROTECTED]> wrote:

Hey, any chance you could post the working code now that you fixed it.

There is no actual real working code for reaction out there, and it'd be
nice to look at some real code..

in other words, can I see your finished action and controller classes so i
can steal ideas and learn something?

I am working on a reaction app that I will post to public SVN as soon as
it actually runs and keep it public as i develop it so other people can get
an idea aswell


Hi Guillermo,
The application that I'm developing is for a client so I can't make it
public. But here are the pertinent action classes and controllers that i
have for now:

package MyApp::Model::Action::AuthUser;
use Reaction::Class;
use Reaction::Types::DBIC;
use Reaction::InterfaceModel::Action;

class AuthUser is 'Reaction::InterfaceModel::Action', which {
 has 'username' => (isa => 'NonEmptySimpleStr', is => 'rw',
set_or_lazy_fail('username'));
 has 'password' => (isa => 'Password',          is => 'rw',
set_or_lazy_fail('password'));

 after sync_all => sub {
   my $self = shift;
   my $c = $self->ctx;
   if ($c->login($self->username, $self->password)) {
       $c->response->redirect($c->uri_for(
         $c->request->params->{request_uri}
       ));
   }
   else {
       $self->error_message("Wrong Password");
   }
 };

 override error_for_attribute => sub {
   my ($self, $attr) = @_;
   if ($attr->name eq 'password' && $self->error_message) {
     return $self->error_message;
   }
   return super();
 };

 override can_apply => sub {
   my ($self) = @_;
   return 0 if $self->error_message;
   return super();
 };

};

1;


package MyApp::Model::Action::CreateUser;
use Reaction::Class;
extends 'Reaction::InterfaceModel::Action::DBIC::ResultSet::Create';

has 'email_address' => (isa => 'EmailAddress', is => 'rw',
set_or_lazy_fail('email_address'));
has 'password'      => (isa => 'StrongPassword', is => 'rw');
has 'confirm_password' => (isa => 'StrongPassword', is => 'rw');
has 'last_name'     => (isa => 'NonEmptySimpleStr', is => 'rw', predicate =>
'has_last_name');
has 'first_name'    => (isa => 'NonEmptySimpleStr', is => 'rw',
set_or_lazy_fail('first_name'));
has 'username'      => (isa => 'NonEmptySimpleStr', is => 'rw',
set_or_lazy_fail('username'));

override error_for_attribute => sub {
 my ($self, $attr) = @_;
 if ($attr->name eq 'confirm_password'
     && $self->confirm_password
     && $self->password
     && $self->confirm_password ne $self->password) {
   return "Passwords don't match";
 }
 return super();
};

override can_apply => sub {
 my ($self) = @_;
 return 0 if $self->confirm_password ne $self->password;
 return super();
};

1;

package MyApp::Controller::Login;
use strict;
use warnings;
use base 'Reaction::UI::Controller';
use Reaction::Class;
use aliased 'Reaction::UI::ViewPort::ActionForm';

sub base :Chained('/base') :PathPart('login') :CaptureArgs(0) {}

sub login :Chained('base') :PathPart('') :Args(0) {
   my ($self, $c) = @_;
   my $action = $c->model('Action::AuthUser')->new(ctx => $c);
   $self->push_viewport(
       ActionForm,
       action => $action,
       layout => 'login_form',
       column_order => [qw/username password/],
   );
}

1;


package MyApp::Controller::User;
use strict;
use warnings;
use base 'Reaction::UI::CRUDController';
use Reaction::Class;

__PACKAGE__->config(
 model_base => 'DBIC',
 model_name => 'User',
 action => { base => { Chained => '/base', PathPart => 'user' },
             list => { ViewPort => { layout => 'user_list' } },
             update => { ViewPort => { layout => 'user_form' } },
             create => { ViewPort => {
               layout => 'user_form',
               column_order => [qw/
                   first_name
                   last_name
                   username
                   password
                   confirm_password
                   email_address
               /],
             } } },
);

1;

Hope it helps. :)

Cheers,
--
Jonas
_______________________________________________
List: [email protected]
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/

Reply via email to