Switch subinclude generation to being called on instances.
[catagits/Catalyst-View-Component-SubInclude.git] / lib / Catalyst / View / Component / SubInclude / SubRequest.pm
index 5c8cf7a..18260dc 100644 (file)
@@ -1,6 +1,8 @@
 package Catalyst::View::Component::SubInclude::SubRequest;
-use warnings;
-use strict;
+use Moose;
+use Carp qw/croak/;
+use MooseX::Types::Moose qw/ Bool /;
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
@@ -8,11 +10,11 @@ Catalyst::View::Component::SubInclude::SubRequest - Sub-requests plugin for C::V
 
 =head1 VERSION
 
-Version 0.01
+Version 0.07
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.07';
 
 =head1 SYNOPSIS
 
@@ -48,28 +50,46 @@ to render the subinclude contents.
 
 It requires L<Catalyst::Plugin::SubRequest>.
 
-=head1 STASH FUNCTION
+=head1 CLASS METHODS
 
-=head2 C<subinclude( $path, @args )>
+=head2 C<generate_subinclude( $c, $path, @args )>
 
-This will translate to the following sub-request call:
+This will make a sub-request call to the action specified by C<$path>. Note that
+C<$path> should be the private action path - translation to the public path is
+handled internally.
 
-  $c->sub_request( $path, {}, @args );
+So, after path translation, the call will be (roughly) equivalent to:
+
+  $c->sub_request( $translated_path, {}, @args );
 
 Notice that the stash will always be empty. This behavior could be configurable
 in the future through an additional switch - for now, this behavior guarantees a
-common interface for plugins.
+common interface for all plugins.
 
 =cut
 
+has keep_stash => (
+    isa => Bool,
+    is => 'ro',
+    default => 0,
+);
+
 sub generate_subinclude {
-    my ($class, $c, $path, @params) = @_;
-    my $stash = {};
+    my ($self, $c, $path, @params) = @_;
+    my $stash = $self->keep_stash ? { %{ $c->stash } } : {};
 
     croak "subincludes through subrequests require Catalyst::Plugin::SubRequest"
         unless $c->can('sub_request');
 
-    $c->sub_request( $path, $stash, @params );
+    my $query = ref $params[-1] eq 'HASH' ? pop @params : {};
+    
+    my $action = blessed($path)
+          ? $path
+          : $c->dispatcher->get_action_by_path($path);
+
+    my $uri = $c->uri_for( $action, @params );
+
+    $c->sub_request( $uri->path, $stash, $query );
 }
 
 =head1 SEE ALSO
@@ -96,4 +116,5 @@ under the same terms as Perl itself.
 
 =cut
 
+__PACKAGE__->meta->make_immutable;
 1;