Ado
view release on metacpan or search on metacpan
lib/Ado/Command/generate/crud.pm view on Meta::CPAN
'O|overwrite' => \$args->{overwrite},
#'P|password=s' => \$args->{password},
'T|templates_root=s' => \$args->{templates_root},
't|tables=s@' => \$args->{tables},
'H|home_dir=s' => \$args->{home_dir},
#'U|user=s' => \$args->{user},
);
@{$args->{tables}} = split(/\,/, join(',', @{$args->{tables}}));
Carp::croak $self->usage unless scalar @{$args->{tables}};
my $app = $self->app;
$args->{controller_namespace} //= $app->routes->namespaces->[0];
$args->{model_namespace} //=
(first { ref($_) eq 'HASH' and $_->{name} eq 'DSC' } @{$app->config('plugins')})
->{config}{namespace};
$args->{home_dir} //= $app->home;
$args->{lib} //= catdir($args->{home_dir}, 'lib');
$args->{templates_root} //= $app->renderer->paths->[0];
$self->{_initialised} = 1;
return $self;
}
sub run {
my ($self) = shift->initialise(@_);
my $args = $self->args;
my $app = $self->app;
foreach my $t (@{$args->{tables}}) {
# Controllers
my $class_name = camelize($t);
$args->{class} = $args->{controller_namespace} . '::' . $class_name;
my $c_file = catfile($args->{lib}, class_to_path($args->{class}));
$args->{t} = lc $t;
$self->render_to_file('class', $c_file, $args);
# Templates
my $template_dir = decamelize($class_name);
my $template_root = $args->{templates_root};
my $t_file = catfile($template_root, $template_dir, 'list.html.ep');
$self->render_to_file('list_template', $t_file, $args);
$t_file = catfile($template_root, $template_dir, 'create.html.ep');
$self->render_to_file('create_template', $t_file, $args);
$t_file = catfile($template_root, $template_dir, 'read.html.ep');
$self->render_to_file('read_template', $t_file, $args);
$t_file = catfile($template_root, $template_dir, 'delete.html.ep');
$self->render_to_file('delete_template', $t_file, $args);
} # end foreach tables
return $self;
}
1;
=pod
=encoding utf8
=head1 NAME
Ado::Command::generate::crud - Generates MVC set of files
=head1 SYNOPSIS
Usage:
#on the command-line
# for one or more tables.
$ bin/ado generate crud --tables='news,articles'
#programatically
use Ado::Command::generate::crud;
my $v = Ado::Command::generate::crud->new;
$v->run(-t => 'news,articles');
=head1 DESCRIPTION
B<Disclaimer: I<This command is highly experimental!>
The generated code is not even expected to work properly.>
L<Ado::Command::generate::crud> generates directory structure for
a fully functional
L<MVC|http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller>
set of files, based on existing tables in the database.
You only need to create the tables. The Model (M) classes are generated on the fly
from the tables when the controller classes are loaded by L<Ado> for the first time.
You can dump them to disk if you want using the C<dsc_dump_schema.pl> script that
comes with L<DBIx::Simple::Class>. You may decide to use only L<DBIx::Simple>
via the C<$c-E<gt>dbix> helper or L<DBI> via C<$c-E<gt>dbix-E<gt>dbh>.
That's up to you.
This tool's purpose is to promote
L<RAD|http://en.wikipedia.org/wiki/Rapid_application_development>
by generating the boilerplate code for controllers (C)
and help programmers new to L<Ado> and L<Mojolicious> to quickly create
well structured, fully functional applications.
In the generated actions you will find I<eventually working> code
for reading, creating, updating and deleting records from the tables you
specified on the command-line.
The generated code is just boilerplate to give you a jump start, so you can
concentrate on writing your business-specific code. It is assumed that you will
modify the generated code to suit your specific needs.
=head1 OPTIONS
Below are the options this command accepts, described in L<Getopt::Long> notation.
=head2 C|controller_namespace=s
Optional. The namespace for the controller classes to be generated.
Defaults to C<app-E<gt>routes-E<gt>namespaces-E<gt>[0]>, usually
L<Ado::Control>. If you decide to use another namespace for the controllers,
do not forget to add it to the list C<app-E<gt>routes-E<gt>namespaces>
in C<etc/ado.conf> or your plugin configuration file.
=head2 H|home_dir=s
lib/Ado/Command/generate/crud.pm view on Meta::CPAN
#permissions => '-rwxr-xr-x',
);
}||$c->stash(error=>$@);#very rude!!!
$c->debug('$error:'.$c->stash('error')) if $c->stash('error');
my $data = $res->data;
return $c->respond_to(
json => {data => $data},
html => {data => $data}
);
}
# Reads a resource from table <%= $a->{t} %>. A naive example.
sub read {
my $c = shift;
#This could be validated by a stricter route
my ($id) = $c->stash('id') =~/(\d+)/;
my $data = $table_class->find($id)->data;
$c->debug('$data:'.$c->dumper($data));
return $c->respond_to(
json => {article => $data},
html => {article => $data}
);
}
# Updates a resource in table <%= $a->{t} %>.
sub update {
my $c = shift;
my $v = $c->validation;
my ($id) = $c->stash('id') =~/(\d+)/;
my $res = $table_class->find($id);
$c->reply->not_found() unless $res->data;
$c->debug('$data:'.$c->dumper($res->data));
if($v->has_data && $res->data){
$v->optional('title')->size(3, 50);
$v->optional('body')->size(3, 1 * 1024 * 1024);#1MB
$res->title($v->param('title'))->body($v->param('body'))
->update() unless $v->has_error;
}
my $data = $res->data;
return $c->respond_to(
json => {article => $data},
html => {article => $data}
);
}
# "Deletes" a resource from table <%= $a->{t} %>.
sub delete {
return shift->render(message => '"delete" is not implemented...');
}
1;
<% %>__END__
<% %>=encoding utf8
<% %>=head1 NAME
<%= $a->{class} %> - a controller for resource <%= $a->{t} %>.
<% %>=head1 SYNOPSIS
<% %>=cut
@@ list_template
% $a = shift;
%% my $columns = $table_class->COLUMNS;
<table>
<thead>
<tr>
%% foreach my $column( @$columns ){
<th><%%= $column %></th>
%% }
</tr>
</thead>
<tbody>
%% foreach my $row (@{$list->{json}{data}}) {
<tr>
%% foreach my $column( @$columns ){
<td><%%= $row->{$column} %></td>
%% }
</tr>
%% }
</tbody>
%%#== $c->dumper($list);
</table>
@@ create_template
% $a = shift;
<article>
Create your form for creating a resource here.
</article>
@@ read_template
% $a = shift;
<article id="<%%= $article->{id} %>">
<h1><%%= $article->{title} %></h1>
<section><%%= $article->{body} %></section>
</article>
@@ update_template
% $a = shift;
<article>
Create your form for updating a resource here.
</article>
( run in 2.141 seconds using v1.01-cache-2.11-cpan-f56aa216473 )