[Jifty-commit] r5205 - in jifty/trunk: lib/Jifty/Plugin lib/Jifty/Plugin/Attributes/Mixin t/TestApp-Plugin-Attributes/t

Jifty commits jifty-commit at lists.jifty.org
Mon Mar 10 17:11:29 EDT 2008


Author: sartak
Date: Mon Mar 10 17:11:25 2008
New Revision: 5205

Added:
   jifty/trunk/t/TestApp-Plugin-Attributes/t/04-delete.t
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Plugin/Attributes.pm
   jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm

Log:
 r52479 at onn:  sartak | 2008-03-10 17:10:02 -0400
 Attributes: When we delete a record, delete any attributes it may have


Modified: jifty/trunk/lib/Jifty/Plugin/Attributes.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Attributes.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Attributes.pm	Mon Mar 10 17:11:25 2008
@@ -4,5 +4,19 @@
 package Jifty::Plugin::Attributes;
 use base 'Jifty::Plugin';
 
+our $VERSION = '0.01';
+
+sub init {
+    Jifty::DBI::Record->add_trigger(
+        name      => "before_delete",
+        callback  => sub {
+            my $record = shift;
+            if ($record->can('delete_all_attributes')) {
+                $record->delete_all_attributes;
+            }
+        },
+    );
+}
+
 1;
 

Modified: jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm	Mon Mar 10 17:11:25 2008
@@ -8,7 +8,7 @@
 use base 'Exporter';
 
 our @EXPORT = qw/attributes first_attribute add_attribute set_attribute
-                 delete_attribute/;
+                 delete_attribute delete_all_attributes/;
 
 =head2 attributes
 
@@ -140,5 +140,24 @@
     return $ok;
 }
 
+=head2 delete_all_attributes
+
+Deletes all the attributes associated with the object.
+
+=cut
+
+sub delete_all_attributes {
+    my $self = shift;
+    my $ok = 1;
+
+    my $attrs = $self->attributes;
+    while (my $attr = $attrs->next) {
+        $attr->delete
+            or $ok = 0;
+    }
+
+    return $ok;
+}
+
 1;
 

Added: jifty/trunk/t/TestApp-Plugin-Attributes/t/04-delete.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/t/04-delete.t	Mon Mar 10 17:11:25 2008
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use lib 't/lib';
+use Jifty::SubTest;
+use Jifty::Test tests => 8;
+
+my $song = TestApp::Plugin::Attributes::Model::Song->new;
+my ($ok, $msg) = $song->create(
+    name   => 'Backdrifts',
+    artist => 'Radiohead',
+    album  => 'Hail to the Thief',
+);
+ok($ok, $msg);
+my $song_id = $song->id;
+
+my $song2 = TestApp::Plugin::Attributes::Model::Song->new;
+($ok, $msg) = $song2->create(
+    name   => '15 Step',
+    artist => 'Radiohead',
+    album  => 'In Rainbows',
+);
+ok($ok, $msg);
+my $song2_id = $song2->id;
+
+for (qw/httt radiohead/) {
+    $song->add_attribute(name => 'tag', content => $_);
+    $song2->add_attribute(name => 'tag', content => $_);
+}
+
+$song->add_attribute(name => 'tag', content => 2003);
+$song2->add_attribute(name => 'tag', content => 2007);
+
+my %got = map { $_->content => 1 }
+          @{ $song->attributes->named("tag")->items_array_ref };
+
+::is_deeply(\%got,
+            {httt => 1, radiohead => 1, 2003 => 1},
+            "attributes set correctly");
+
+($ok, $msg) = $song->delete;
+ok($ok, $msg);
+
+my $attrs = TestApp::Plugin::Attributes::Model::AttributeCollection->new(
+    current_user => Jifty::CurrentUser->superuser,
+);
+$attrs->limit(
+    column => 'object_type',
+    value => 'TestApp::Plugin::Attributes::Model::Song',
+);
+$attrs->limit(
+    column => 'object_id',
+    value => $song_id,
+);
+
+is($attrs->count, 0, "deleted all the attributes of the song");
+
+$attrs = TestApp::Plugin::Attributes::Model::AttributeCollection->new(
+    current_user => Jifty::CurrentUser->superuser,
+);
+$attrs->limit(
+    column => 'object_type',
+    value => 'TestApp::Plugin::Attributes::Model::Song',
+);
+$attrs->limit(
+    column => 'object_id',
+    value => $song2_id,
+);
+
+is($attrs->count, 3, "delete only affects the one deleted object deleted");
+
+$ok = $song2->delete_all_attributes;
+ok($ok, "reported success in deleting all attributes");
+
+$attrs = TestApp::Plugin::Attributes::Model::AttributeCollection->new(
+    current_user => Jifty::CurrentUser->superuser,
+);
+$attrs->limit(
+    column => 'object_type',
+    value => 'TestApp::Plugin::Attributes::Model::Song',
+);
+$attrs->limit(
+    column => 'object_id',
+    value => $song2_id,
+);
+
+is($attrs->count, 0, "delete_all_attributes successful");
+


More information about the Jifty-commit mailing list