Skip to content

Instantly share code, notes, and snippets.

@jjn1056
Last active July 3, 2022 13:58
Show Gist options
  • Save jjn1056/afb72a5ab7f4f35421ff4346cbd0ddbf to your computer and use it in GitHub Desktop.
Save jjn1056/afb72a5ab7f4f35421ff4346cbd0ddbf to your computer and use it in GitHub Desktop.
package Example::Controller::Register;
use Moose;
use MooseX::MethodAttributes;
use Example::Syntax;
extends 'Example::ControllerPerRequest',
'Example::ViewModelController';
has registration => (
is => 'ro',
lazy => 1,
required => 1,
default => sub($self) { $self->ctx->users->registration },
);
sub root :Chained(/root) PathPart(register) Args(0) Does(Verbs) View(Components::Register) ($self, $c) {
return $c->redirect_to_action('#home') && $c->detach if $c->user->registered;
}
sub GET :Action ($self, $c) { return $c->res->code(200) }
sub POST :Action RequestModel(RegistrationRequest) ($self, $c, $request) {
$self->registration->register($request); ## Avoid DBIC specific API
return $self->registration->valid ?
$c->redirect_to_action('#login') :
$c->res->code(400);
}
__PACKAGE__->meta->make_immutable;
__PACKAGE__->config(view_model=>'RegistrationView');
package MyApp::Model::RegistrationView;
use Moose;
extends 'Catalyst::Model::ViewModel';
has 'registration' => (is=>'ro', required=>1);
__PACKAGE__->meta->make_immutable;
__PACKAGE__->config(
build => {
registration => 'Controller', ## Maybe better to use meta foo and just copy
},
);
package Example::HTML::Components::Register;
use Moo;
use Example::HTML::Components 'Layout', 'FormFor';
use Valiant::HTML::TagBuilder ':html';
use Example::Syntax;
with 'Valiant::HTML::Component';
with 'CatalystX::ControllerAttributes'; # map init_args to controller attributes
has 'ctx' => (is=>'ro', required=>1);
has 'registration' => (
is => 'ro',
required => 1,
lazy => 1,
default => sub($self) {
return $self->ctx->controller->attribute_value_for_view('registration')
},
);
__PACKAGE__->config(
build => {
registration => 'Controller',
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment