From: Jess Robinson Date: Mon, 21 May 2012 15:44:56 +0000 (+0000) Subject: Update admin_users to be able to remove traditions from users X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ec7ea4e66a9c35cbf50ea39d092ad7187435b76e;p=scpubgit%2Fstemmatology.git Update admin_users to be able to remove traditions from users Write tests for remove_tradition Add clearer --- diff --git a/lib/Text/Tradition.pm b/lib/Text/Tradition.pm index 97971a0..e454722 100644 --- a/lib/Text/Tradition.pm +++ b/lib/Text/Tradition.pm @@ -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 diff --git a/lib/Text/Tradition/User.pm b/lib/Text/Tradition/User.pm index e57b8b8..9ba694b 100644 --- a/lib/Text/Tradition/User.pm +++ b/lib/Text/Tradition/User.pm @@ -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 diff --git a/script/admin_users.pl b/script/admin_users.pl index e327395..9f9b72e 100644 --- a/script/admin_users.pl +++ b/script/admin_users.pl @@ -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"; diff --git a/t/text_tradition_user.t b/t/text_tradition_user.t index f93e5e7..bce5cb1 100644 --- a/t/text_tradition_user.t +++ b/t/text_tradition_user.t @@ -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'); +} +