<br><br><div class="gmail_quote">On Thu, May 13, 2010 at 2:54 PM, Kieren Diment <span dir="ltr">&lt;<a href="mailto:diment@gmail.com">diment@gmail.com</a>&gt;</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-&gt;meta-&gt;clone_object($self, arg =&gt; $c-&gt;stash-&gt;{something});<br>
     return $new;<br>
<div><div></div><div class="h5"> }<br></div></div></blockquote><div><br></div><div>Hum, I&#39;m not clear how that works with Catalyst::Model::DBIC::Schema, because I want to be able to call &quot;current_user&quot; on any row object.  $track-&gt;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&#39;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 &#39;current_user&#39; =&gt; ( is =&gt; &#39;rw&#39;  );</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-&gt;result_source-&gt;schema-&gt;current_user }</div></div><div><br></div><div>Now, I might want to have a method $track-&gt;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-&gt;current_user || die &#39;no current_user&#39;;</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 &#39;Catalyst::Model::DBIC::Schema&#39;;</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 &#39;ACCEPT_CONTEXT&#39; =&gt; 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-&gt;schema-&gt;current_user( $c-&gt;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__-&gt;meta-&gt;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&#39;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 &#39;MyApp::Controller::API&#39; }</div></div><div class="gmail_quote"><div>__PACKAGE__-&gt;config(</div>

</div><div class="gmail_quote"><div>    return_colums =&gt; [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&#39;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 =&gt; [ &#39;can_user_access&#39; ]</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&#39;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>