<br><br><div class="gmail_quote">On Thu, May 13, 2010 at 2:54 PM, Kieren Diment <span dir="ltr"><<a href="mailto:diment@gmail.com">diment@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5"> sub ACCEPT_CONTEXT {</div></div>
my ($self, $c ) = @_;<br>
my $new = $self->meta->clone_object($self, arg => $c->stash->{something});<br>
return $new;<br>
<div><div></div><div class="h5"> }<br></div></div></blockquote><div><br></div><div>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.</div>
<div><br></div><div><br></div><div><div class="gmail_quote"><div>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.</div>
<div><br></div><div>For example:</div><div><br></div><div>In my Music::Schema base class I add an accessor:</div><div><br></div><div> has 'current_user' => ( is => 'rw' );</div><div><br></div><div>
Then in my Result base class add a convenience method:</div>
<div><br></div><div><div> sub current_user { return shift->result_source->schema->current_user }</div></div><div><br></div><div>Now, I might want to have a method $track->can_user_access so in my Track class:</div>
<div><br></div><div>sub can_user_access {</div><div> my ( $track, $current_user ) = @_;</div><div><br></div><div> # Use saved user if one not passed.</div><div> $current_user ||= $track->current_user || die 'no current_user';</div>
<div> ...</div><div> return $has_access;</div><div><br></div><div><br></div><div>Finally, in Catalyst my Model class would then look like:</div><div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
<div class="gmail_quote"><div><div><div>package MyApp::Model::Music;</div></div></div></div><div class="gmail_quote"><div><div><div>use Moose;</div></div></div></div><div class="gmail_quote"><div><div><div>extends 'Catalyst::Model::DBIC::Schema';</div>
</div></div></div><div class="gmail_quote"><div><div><div>use namespace::autoclean;</div></div></div></div><div class="gmail_quote"><div><div><div><br></div></div></div></div><div class="gmail_quote"><div><div><div>before 'ACCEPT_CONTEXT' => sub {</div>
</div></div></div><div class="gmail_quote"><div><div><div> my ( $self, $c ) = @_;</div></div></div></div><div class="gmail_quote"><div><div><div> $self->schema->current_user( $c->current_user );</div></div>
</div></div><div class="gmail_quote"><div><div><div><br></div></div></div></div><div class="gmail_quote"><div><div><div>};</div></div></div></div></blockquote><div class="gmail_quote"><div><div><div><br></div></div></div>
</div><blockquote class="webkit-indent-blockquote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
<div class="gmail_quote"><div><div><div>__PACKAGE__->meta->make_immutable;</div></div></div><div>1;</div></div></blockquote><div class="gmail_quote"><div><br></div><div>That implementation is simple enough, but not clear is it's the best approach.</div>
<div><br></div><div><br></div><div>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:</div>
<div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
<div class="gmail_quote"><div>package MyApp::Controller::Music::Track;</div></div><div class="gmail_quote"><div>BEGIN { extends 'MyApp::Controller::API' }</div></div><div class="gmail_quote"><div>__PACKAGE__->config(</div>
</div><div class="gmail_quote"><div> return_colums => [qw/ id track_name position can_user_access /],</div></div><div class="gmail_quote"><div>);</div></div><div class="gmail_quote"><div>1;</div></div></blockquote>
<div class="gmail_quote">
<div><br></div><div>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).</div>
<div><br></div><div>So, I either extend the config to tell the base class to pass the current user to some methods:</div><div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
<div class="gmail_quote"><div>pass_user_to_meethod => [ 'can_user_access' ]</div></div></blockquote><div><br></div>Or I use something like ACCEPT_CONTEXT to set the current user in the schema.<div><br></div><div>
Better ideas?</div><div><br></div><div><br></div><div>This is going to be a killer Music app. I just hope CDs don't go out of style.<br><div class="gmail_quote"><div><br></div></div></div></div><div> </div></div><br>
-- <br>Bill Moseley<br><a href="mailto:moseley@hank.org">moseley@hank.org</a><br>