[Catalyst-commits] r12161 - trunk/examples/CatalystAdvent/root/2009

alexn_org at dev.catalyst.perl.org alexn_org at dev.catalyst.perl.org
Thu Dec 3 12:41:35 GMT 2009


Author: alexn_org
Date: 2009-12-03 12:41:34 +0000 (Thu, 03 Dec 2009)
New Revision: 12161

Modified:
   trunk/examples/CatalystAdvent/root/2009/3.pod
Log:
fixes

Modified: trunk/examples/CatalystAdvent/root/2009/3.pod
===================================================================
--- trunk/examples/CatalystAdvent/root/2009/3.pod	2009-12-03 12:25:46 UTC (rev 12160)
+++ trunk/examples/CatalystAdvent/root/2009/3.pod	2009-12-03 12:41:34 UTC (rev 12161)
@@ -141,7 +141,7 @@
           is_nullable => 0,
       },
   );
-  __PACKAGE__->add_unique_constraint([ qw/article_fk tag_fk/ ]);
+  __PACKAGE__->set_primary_key(qw/article_fk tag_fk/);
   __PACKAGE__->belongs_to(tag => 'Blog::Schema::Result::Tag', 'tag_fk');
   __PACKAGE__->belongs_to(article => 'Blog::Schema::Result::Article', 'article_fk');
 
@@ -151,7 +151,7 @@
     # perl -I./lib -MBlog::Model::DB \
          -e " Blog::Model::DB->new->schema->deploy "
 
-Also add these utilities to lib/Blog.pm ...
+Also add this utility to lib/Blog.pm ...
 
   sub redirect_to_action {
       my ($c, $controller, $action, @params) =@_;
@@ -177,18 +177,17 @@
       $c->stash->{articles} = $c->model('DB::Article');
   }
 
-  sub list : Chained('articles') Args(0) {}
+  sub list : Chained('articles') Args(0) {
+      my ($self, $c) = @_;
+      $c->stash->{template} = 'article/list.tt';      
+  }
 
   sub item : Chained('articles') PathPart('') CaptureArgs(1) {
       my ($self, $c, $id) = @_;
 
       $c->error("ID isn't valid!") unless $id =~ /^\d+$/;
-      $c->stash->{item} = $c->stash->{articles}->find($id);
-      unless ($c->stash->{item}) {
-          # $c->flash->{error_msg} = "Request article is not available!";
-          $c->response->status(404);
-          $c->forward('list');
-      }
+      $c->stash->{item} = $c->stash->{articles}->find($id)
+          || $c->detach('not_found');
   }
 
   sub edit : Chained('item') {
@@ -220,6 +219,13 @@
       $c->redirect_to_action('Article', 'edit', [$item->article_id]);
   }
 
+  sub not_found : Local {
+      my ($self, $c) = @_;
+      $c->response->status(404);
+      $c->stash->{error_msg} = "Article not found!";
+      $c->detach('list');
+  }
+
 This CRUD is pretty standard, and it would be possible to abstract
 it away in a role (see  L<Catalyst::Manual::CatalystAndMoose/"Controller_Roles">)
 
@@ -288,19 +294,24 @@
 the necessary links between the article and those tags. We'll just
 append the following to Blog::Form::Article:
 
-  before 'update_model' => sub {
+  around 'update_model' => sub {
+      my $orig = shift;
       my $self = shift;
       my $item = $self->item;
-
-      my @tags = split /\s*,\s*/, $self->field('tags_str')->value;
-      $item->article_tags->delete;
-
-      my $tags_rs = $item->result_source->schema->resultset('Tag');
-      foreach my $tag (@tags) {
-          my $tag_obj = $tags_rs->search({ name => $tag })->first
-            || $tags_rs->create({ name => $tag });
-          $item->article_tags->create({ tag => $tag_obj });
-      }
+      
+      my $schema = $item->result_source->schema;
+  
+      my $changes = sub {	
+          $orig->($self, @_);
+  
+          my @tags = split /\s*,\s*/, $self->field('tags_str')->value;
+  
+          $item->article_tags->delete;
+          $item->article_tags->create({ tag => { name => $_ } })
+              foreach (@tags);
+      };
+  
+      $schema->txn_do($changes);
   };
 
 We have this flexibility, since HTML::FormHandler is based on
@@ -329,10 +340,10 @@
   extends 'HTML::FormHandler::Field::Text';
 
   apply( [
-       { # remove a dollar sign
+       { # remove suffix chars
          transform => sub {
            my $value = shift;
-           $value =~ s/^\$//;
+           $value =~ s/\w+$//;
            return $value;
        }},
        {
@@ -357,18 +368,34 @@
 doing this, and one stupid way of doing it would be to just delete the
 HTML tags and slice it to 200 chars or something.
 
-So to autogenerate the summary tag, add the following to
-C<Blog::Form::Article> ...
+So to autogenerate the summary tag, we'll change the 'around "update_model"' in
+C<Blog::Form::Article> as ...
 
-  after 'update_model' => sub {
-      my $self = shift;
-      my $item = $self->item;
+  around 'update_model' => sub {
+
+      # .... <init here> ...
   
-      my $summary = generate_symmary($item->content);
-      $item->update({ summary => $summary })
-        if $summary;
+      my $changes = sub {    
+          $orig->($self, @_);
+
+          # .... < tags saving here > ....  
+  
+          # generates the summary and updates the $item
+          my $summary = generate_symmary($item->content);
+          $item->update({ summary => $summary }) if $summary;
+      };
+  
+      $schema->txn_do($changes);
   };
 
+You may have noticed schema->txn_do. That's a transaction. If any of
+the method calls in our $changes code-block throws an error, a
+"ROLLBACK" is issued.
+
+B<NOTE:> It would've been better for the summary generation to be
+defined in C<Blog::Schema::Result::Article>, by overriding
+"update". So what I did here is not necessarily a good practice.
+
 =head2 Testing
 
 And we are done. Start your development server with:




More information about the Catalyst-commits mailing list