Update admin_users to be able to remove traditions from users
Jess Robinson [Mon, 21 May 2012 15:44:56 +0000 (15:44 +0000)]
Write tests for remove_tradition
Add clearer

lib/Text/Tradition.pm
lib/Text/Tradition/User.pm
script/admin_users.pl
t/text_tradition_user.t

index 97971a0..e454722 100644 (file)
@@ -60,6 +60,7 @@ has 'user' => (
     isa => 'Text::Tradition::User',
     required => 0,
     predicate => 'has_user',
+    clearer => 'clear_user',
     );
   
 # Create the witness before trying to add it
index e57b8b8..9ba694b 100644 (file)
@@ -27,6 +27,17 @@ after add_tradition => sub {
         unless $tradition->has_user && $tradition->user->id eq $self->id;
 };
 
+sub remove_tradition {
+    my ($self, $tradition) = @_;
+
+    ## FIXME: Is "name" a good unique field to compare traditions on?
+    my @traditions = @{$self->traditions};
+    @traditions = grep { $tradition->name ne $_->name } @traditions;
+
+    $tradition->clear_user;
+    $self->traditions(\@traditions);
+}
+
 1;
 
 =head1 NAME
index e327395..9f9b72e 100644 (file)
@@ -77,7 +77,14 @@ given ($command) {
             if(!$tradition || !$user) {
                 print "Can't find one of '$username' or '$tradition_id' in the database!\n";
             } else {
-                $user->add_tradition($tradition);
+                if(grep { $userstore->object_to_id($_) 
+                          eq 
+                          $userstore->object_to_id($tradition)} 
+                   @{$user->traditions}) {
+                    $user->remove_tradition($tradition);
+                } else {
+                    $user->add_tradition($tradition);
+                }
                 $userstore->update($tradition);
                 $userstore->update($user);
                 print "OK.\n";
index f93e5e7..bce5cb1 100644 (file)
@@ -102,3 +102,29 @@ ok($changed->check_password('passbloggs'), 'Modified & retrieved with correct ne
     is($tlist[0]->{id}, $uuid, 'Traditionlist returns actual tradition with same uuid we put in earlier');
 }
 
+{
+## remove_tradition
+    use Text::Tradition;
+    my $t = Text::Tradition->new( 
+        'name'  => 'inline', 
+        'input' => 'Tabular',
+        'file'  => 't/data/simple.txt',
+       );
+
+    my $uuid = $user_store->save($t);
+    my $user = $user_store->add_user({ username => 'testremove',
+                                       password => 'testingtraditions' });
+    $user->add_tradition($t);
+    $user_store->update($user);
+
+    $user->remove_tradition($t);
+    $user_store->update($user);
+    my $changed_t = $user_store->tradition($uuid);
+
+    is( scalar @{$user->traditions}, 0, 'Added and removed one tradition');
+    ok(!$changed_t->has_user, 'Removed user from tradition');
+
+    my @tlist = $user_store->traditionlist($user);
+    is(scalar @tlist, 0, 'Traditionlist now empty');
+}
+