[Catalyst-commits] r7620 - in CatalystX-CRUD/CatalystX-CRUD/trunk:
. lib/CatalystX lib/CatalystX/CRUD lib/CatalystX/CRUD/Model
lib/CatalystX/CRUD/ModelAdapter lib/CatalystX/CRUD/Object
karpet at dev.catalyst.perl.org
karpet at dev.catalyst.perl.org
Mon Apr 21 17:00:58 BST 2008
Author: karpet
Date: 2008-04-21 17:00:58 +0100 (Mon, 21 Apr 2008)
New Revision: 7620
Modified:
CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/File.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/Utils.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object/File.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/REST.pm
Log:
switch to Class::C3
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/Changes 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/Changes 2008-04-21 16:00:58 UTC (rev 7620)
@@ -113,6 +113,7 @@
* renamed 'buffer' to 'content' in ::Object::File.
* ::Model::File->fetch() will now always return an object even if it does not yet
exist on the filesystem.
-
-
+ * change default create() method in Controller to call methods directly instead of forward()ing.
+ * add create() method to REST that just redirects to create_form().
+ * change from 'use NEXT' to 'use Class::C3'
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm 2008-04-21 16:00:58 UTC (rev 7620)
@@ -7,6 +7,7 @@
);
use Carp;
use Catalyst::Utils;
+use Class::C3;
__PACKAGE__->mk_accessors(qw( model_adapter ));
@@ -125,15 +126,15 @@
Attribute: Local
-Namespace for creating a new object. Forwards to fetch() and edit()
+Namespace for creating a new object. Calls to fetch() and edit()
with a B<primary_key> value of C<0> (zero).
=cut
sub create : Local {
my ( $self, $c ) = @_;
- $c->forward( 'fetch', [0] );
- $c->detach('edit');
+ $self->fetch( $c, 0 );
+ $self->edit($c);
}
=head2 edit
@@ -347,7 +348,7 @@
sub new {
my ( $class, $app_class, $args ) = @_;
- my $self = $class->NEXT::new( $app_class, $args );
+ my $self = $class->next::method( $app_class, $args );
# if model_adapter class is defined, load and instantiate it.
if ( $self->config->{model_adapter} ) {
@@ -418,7 +419,7 @@
elsif ( $self->{_form}->can('reset') ) {
$self->{_form}->reset;
}
- $self->NEXT::form($c);
+ $self->next::method($c) if $self->next::can;
return $self->{_form};
}
@@ -572,7 +573,7 @@
return;
}
- # turn flag on if explicitly turned off
+ # turn flag on unless explicitly turned off
$c->stash->{view_on_single_result} = 1
unless exists $c->stash->{view_on_single_result};
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/File.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/File.pm 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/File.pm 2008-04-21 16:00:58 UTC (rev 7620)
@@ -6,6 +6,7 @@
use Carp;
use Data::Dump qw( dump );
use Path::Class::File;
+use Class::C3;
our $VERSION = '0.26';
@@ -42,7 +43,7 @@
sub Xsetup {
my ( $self, $c ) = @_;
$self->config->{inc_path} ||= [ $c->config->{root} ];
- $self->NEXT::Xsetup($c);
+ $self->next::method($c);
}
=head2 new_object( file => I<path/to/file> )
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/Utils.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/Utils.pm 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model/Utils.pm 2008-04-21 16:00:58 UTC (rev 7620)
@@ -92,6 +92,11 @@
Ignore _page_size, _page and _offset and do not return a limit
or offset value.
+=item _op
+
+If set to C<OR> then the query columns will be marked as OR'd together,
+rather than AND'd together (the default).
+
=back
=cut
@@ -158,7 +163,8 @@
next unless grep {m/\S/} @v;
push( @s, "$p = " . join( ' or ', @v ) );
}
- return join( ' AND ', @s );
+ my $op = $self->context->req->params->{_op} || 'AND';
+ return join( " $op ", @s );
}
=head2 params_to_sql_query( I<field_names> )
@@ -192,20 +198,27 @@
my $like = $self->use_ilike ? 'ilike' : 'like';
my $treat_like_int
= $self->can('treat_like_int') ? $self->treat_like_int : {};
+ my $ORify
+ = ( exists $c->req->params->{_op} && $c->req->params->{_op} eq 'OR' )
+ ? 1
+ : 0;
+ my $fuzzy = $c->req->params->{_fuzzy} || 0;
for my $p (@$field_names) {
next unless exists $c->req->params->{$p};
- my @v = $c->req->param($p);
- my @safe = @v;
- next unless grep { defined && m/./ } @safe;
-
+ my @v = $c->req->param($p);
+ next unless grep { defined && m/./ } @v;
+ my @copy = @v;
$query{$p} = \@v;
+ if ($fuzzy) {
+ grep { $_ .= '%' unless m/[\%\*]/ } @copy;
+ }
# normalize wildcards and set sql
- if ( grep {/[\%\*]|^!/} @v ) {
- grep {s/\*/\%/g} @safe;
- my @wild = grep {m/\%/} @safe;
+ if ( grep {/[\%\*]|^!/} @copy ) {
+ grep {s/\*/\%/g} @copy;
+ my @wild = grep {m/\%/} @copy;
if (@wild) {
if ( exists $treat_like_int->{$p} ) {
push( @sql,
@@ -217,20 +230,23 @@
}
# allow for negation of query
- my @not = grep {m/^!/} @safe;
+ my @not = grep {m/^!/} @copy;
if (@not) {
push( @sql, ( $p => { $ne => [ grep {s/^!//} @not ] } ) );
}
}
else {
- push( @sql, $p => [@safe] );
+ push( @sql, $p => [@copy] );
}
}
- return { sql => \@sql, query => \%query };
+ return {
+ sql => ( scalar(@sql) > 2 && $ORify ) ? [ 'or' => \@sql ] : \@sql,
+ query => \%query
+ };
}
-=head2 make_pager( I<total>, I<results> )
+=head2 make_pager( I<total> )
Returns a Data::Pageset object using I<total>,
either the C<_page_size> param or the value of page_size(),
@@ -243,7 +259,7 @@
=cut
sub make_pager {
- my ( $self, $count, $results ) = @_;
+ my ( $self, $count ) = @_;
my $c = $self->context;
return if $c->req->param('_no_page');
return Data::Pageset->new(
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model.pm 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Model.pm 2008-04-21 16:00:58 UTC (rev 7620)
@@ -7,6 +7,7 @@
Catalyst::Model
);
use Carp;
+use Class::C3;
our $VERSION = '0.26';
@@ -82,7 +83,7 @@
sub new {
my ( $class, $c, @arg ) = @_;
- my $self = $class->NEXT::new( $c, @arg );
+ my $self = $class->next::method( $c, @arg );
$self->Xsetup( $c, @arg );
return $self;
}
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm 2008-04-21 16:00:58 UTC (rev 7620)
@@ -6,6 +6,7 @@
CatalystX::CRUD::Model::File
CatalystX::CRUD::Model::Utils
);
+use Class::C3;
our $VERSION = '0.26';
@@ -47,7 +48,7 @@
sub new_object {
my ( $self, $c, @arg ) = @_;
- $self->SUPER::new_object(@arg);
+ $self->next::method(@arg);
}
=head2 fetch( I<context>, I<args> )
@@ -58,7 +59,7 @@
sub fetch {
my ( $self, $c, @arg ) = @_;
- $self->SUPER::new_object(@arg);
+ $self->next::method(@arg);
}
=head2 search( I<context>, I<args> )
@@ -69,7 +70,7 @@
sub search {
my ( $self, $c, @arg ) = @_;
- $self->SUPER::new_object(@arg);
+ $self->next::method(@arg);
}
=head2 iterator( I<context>, I<args> )
@@ -80,7 +81,7 @@
sub iterator {
my ( $self, $c, @arg ) = @_;
- $self->SUPER::new_object(@arg);
+ $self->next::method(@arg);
}
=head2 count( I<context>, I<args> )
@@ -91,7 +92,7 @@
sub count {
my ( $self, $c, @arg ) = @_;
- $self->SUPER::new_object(@arg);
+ $self->next::method(@arg);
}
=head2 make_query( I<context>, I<args> )
@@ -102,7 +103,7 @@
sub make_query {
my ( $self, $c, @arg ) = @_;
- $self->SUPER::new_object(@arg);
+ $self->next::method(@arg);
}
1;
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object/File.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object/File.pm 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object/File.pm 2008-04-21 16:00:58 UTC (rev 7620)
@@ -4,7 +4,7 @@
use base qw( CatalystX::CRUD::Object );
use Path::Class::File;
use Carp;
-use NEXT;
+use Class::C3;
use overload(
q[""] => sub { shift->delegate },
fallback => 1,
@@ -43,7 +43,7 @@
sub new {
my $class = shift;
- my $self = $class->NEXT::new(@_);
+ my $self = $class->next::method(@_);
my $file = $self->{file} or $self->throw_error("file param required");
$self->{delegate} = Path::Class::File->new($file);
return $self;
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object.pm 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Object.pm 2008-04-21 16:00:58 UTC (rev 7620)
@@ -3,6 +3,8 @@
use warnings;
use base qw( Class::Accessor::Fast CatalystX::CRUD );
use Carp;
+use Class::C3;
+Class::C3::initialize();
__PACKAGE__->mk_ro_accessors(qw( delegate ));
@@ -52,7 +54,7 @@
sub new {
my $class = shift;
my $arg = ref( $_[0] ) eq 'HASH' ? $_[0] : {@_};
- return $class->SUPER::new($arg);
+ return $class->next::method($arg);
}
=head2 delegate
@@ -105,17 +107,18 @@
=cut
sub AUTOLOAD {
- my $obj = shift;
- my $obj_class = ref($obj) || $obj;
- my $method = our $AUTOLOAD;
+ my $obj = shift;
+ my $obj_class = ref($obj) || $obj;
+ my $delegate_class = ref( $obj->delegate ) || $obj->delegate;
+ my $method = our $AUTOLOAD;
$method =~ s/.*://;
return if $method eq 'DESTROY';
if ( $obj->delegate->can($method) ) {
return $obj->delegate->$method(@_);
}
- $obj->throw_error(
- "method '$method' not implemented in class '$obj_class'");
+ $obj->throw_error( "method '$method' not implemented in class "
+ . "'$obj_class' or '$delegate_class'" );
}
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/REST.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/REST.pm 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/REST.pm 2008-04-21 16:00:58 UTC (rev 7620)
@@ -2,9 +2,8 @@
use strict;
use warnings;
use base qw( CatalystX::CRUD::Controller );
-
use Carp;
-use Data::Dump qw( dump );
+use Class::C3;
our $VERSION = '0.26';
@@ -73,7 +72,8 @@
sub create_form : Local {
my ( $self, $c ) = @_;
- return $self->create($c);
+ $self->fetch( $c, 0 );
+ $self->edit($c);
}
sub edit_form : PathPart Chained('fetch') Args(0) {
@@ -81,6 +81,17 @@
return $self->edit($c);
}
+=head2 create
+
+Redirects to create_form().
+
+=cut
+
+sub create : Local {
+ my ( $self, $c ) = @_;
+ $c->res->redirect( $c->uri_for('create_form') );
+}
+
=head2 default
Attribute: Private
@@ -162,8 +173,7 @@
sub edit {
my ( $self, $c ) = @_;
- Data::Dump::dump $c->stash;
- return $self->NEXT::edit($c);
+ return $self->next::method($c);
}
=head2 view( I<context> )
@@ -174,7 +184,7 @@
sub view {
my ( $self, $c ) = @_;
- return $self->NEXT::view($c);
+ return $self->next::method($c);
}
=head2 save( I<context> )
@@ -185,7 +195,7 @@
sub save {
my ( $self, $c ) = @_;
- return $self->NEXT::save($c);
+ return $self->next::method($c);
}
=head2 rm( I<context> )
@@ -196,9 +206,29 @@
sub rm {
my ( $self, $c ) = @_;
- return $self->NEXT::rm($c);
+ return $self->next::method($c);
}
+=head2 postcommit( I<context>, I<object> )
+
+Overrides base method to redirect to REST-style URL.
+
+=cut
+
+sub postcommit {
+ my ( $self, $c, $o ) = @_;
+ my $pk = $self->primary_key;
+
+ if ( $c->action->name eq 'rm' ) {
+ $c->response->redirect( $c->uri_for('') );
+ }
+ else {
+ $c->response->redirect( $c->uri_for( '', $o->$pk ) );
+ }
+
+ 1;
+}
+
1;
__END__
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD.pm 2008-04-17 13:02:47 UTC (rev 7619)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD.pm 2008-04-21 16:00:58 UTC (rev 7620)
@@ -55,10 +55,7 @@
sub throw_error {
my $self = shift;
my $msg = shift || 'unknown error';
- if ( $ENV{CXCRUD_TEST} ) {
- Carp::cluck();
- }
- Catalyst::Exception->throw($msg);
+ Carp::confess($msg);
}
=head1 AUTHOR
More information about the Catalyst-commits
mailing list