Change order in generate_rs so that stash->{class} always overrides ->class Add tests
Alex Howarth [Tue, 18 Jun 2013 19:20:08 +0000 (15:20 -0400)]
Signed-off-by: Alex Howarth <alex.howarth@gmail.com>

lib/Catalyst/Controller/DBIC/API.pm
t/lib/RestTest/Controller/API/REST/StashedClass.pm
t/rest/stashedclass.t

index b595fb4..d3c2d0a 100644 (file)
@@ -202,7 +202,7 @@ sub generate_rs
 {
     my ($self, $c) = @_;
 
-    return $c->model($self->class || $c->stash->{class});
+    return $c->model($c->stash->{class} || $self->class);
 }
 
 =method_protected inflate_request
index 804e7ae..dde1033 100644 (file)
@@ -6,7 +6,9 @@ use namespace::autoclean;
 
 sub setup :Chained('/api/rest/rest_base') :CaptureArgs(1) :PathPart('stashedclass') {
     my ($self, $c, $class) = @_;
-    $c->stash->{class} = $class;
+
+    $c->stash->{class} = $class if $class ne 'noclass';
+
     $self->next::method($c);
 }
 
index 84abd26..9a7dc2d 100644 (file)
@@ -53,4 +53,32 @@ my $base_url = "$base/api/rest/stashedclass";
     like($response->{messages}[0], qr/current_result_set.*does not pass the type constraint/, 'invalid class');
 }
 
+
+{
+    no warnings;
+    # stash->{class} should always win over $self->class
+    *Catalyst::Controller::DBIC::API::class = sub { 'RestTestDB::CD' };
+
+    my $class = 'RestTestDB::Artist';
+    my $req = GET( "$base_url/$class", {}, 'Accept' => 'text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', '200', "status OK" );
+    my $response = $json->decode( $mech->content );
+    is($response->{success}, 'true', 'success');
+    is(scalar( @{$response->{list}} ), 3, 'three results - artist');
+}
+
+{
+    no warnings;
+    # stash->{class} not present, ->class should be returned
+    *Catalyst::Controller::DBIC::API::class = sub { 'RestTestDB::CD' };
+
+    my $req = GET( "$base_url/noclass", {}, 'Accept' => 'text/x-json');
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', '200', "status OK" );
+    my $response = $json->decode( $mech->content );
+    is($response->{success}, 'true', 'success');
+    is(scalar( @{$response->{list}} ), 6, 'six results - cd');
+}
+
 done_testing();