On Thu, May 13, 2010 at 2:54 PM, Kieren Diment <dim...@gmail.com> wrote:

>  sub ACCEPT_CONTEXT {
>     my ($self, $c ) = @_;
>     my $new = $self->meta->clone_object($self, arg =>
> $c->stash->{something});
>     return $new;
>  }
>

Hum, I'm not clear how that works with Catalyst::Model::DBIC::Schema,
because I want to be able to call "current_user" on any row object.
 $track->current_user.


Again, I have some aversion to saving a current user in my model.  That was
more my question.  Getting something to work is easy enough, just not sure
it's the best approach.

For example:

In my Music::Schema base class I add an accessor:

 has 'current_user' => ( is => 'rw'  );

Then in my Result base class add a convenience method:

 sub current_user { return shift->result_source->schema->current_user }

Now, I might want to have a method $track->can_user_access so in my Track
class:

sub can_user_access {
    my ( $track, $current_user ) = @_;

    # Use saved user if one not passed.
    $current_user ||= $track->current_user || die 'no current_user';
    ...
    return $has_access;


Finally, in Catalyst my Model class would then look like:

package MyApp::Model::Music;
use Moose;
extends 'Catalyst::Model::DBIC::Schema';
use namespace::autoclean;

before 'ACCEPT_CONTEXT' => sub {
    my ( $self, $c  ) = @_;
    $self->schema->current_user( $c->current_user );

};


 __PACKAGE__->meta->make_immutable;
1;


That implementation is simple enough, but not clear is it's the best
approach.


Backing up, the issue that came up is I have a base class used for setting
up very common actions for an API.  The controllers for, say,
/music/track/$id are set up with configuration only:

package MyApp::Controller::Music::Track;
BEGIN { extends 'MyApp::Controller::API' }
__PACKAGE__->config(
    return_colums => [qw/ id track_name position can_user_access /],
);
1;


The controller base class then builds a response that includes those columns
and methods.  But, the base class doesn't know when to pass in the current
user to a method (e.g. for can_user_access).

So, I either extend the config to tell the base class to pass the current
user to some methods:

pass_user_to_meethod => [ 'can_user_access' ]


Or I use something like ACCEPT_CONTEXT to set the current user in the
schema.

Better ideas?


This is going to be a killer Music app.  I just hope CDs don't go out of
style.



-- 
Bill Moseley
mose...@hank.org
_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Reply via email to