isa => 'Text::Tradition::User',
required => 0,
predicate => 'has_user',
+ clearer => 'clear_user',
);
# Create the witness before trying to add it
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
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";
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');
+}
+