From: Jess Robinson Date: Mon, 21 May 2012 10:49:01 +0000 (+0000) Subject: Add user_traditionlist X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fefeeedaa6092a3c13a221130fa75561124c7a1a;p=scpubgit%2Fstemmatology.git Add user_traditionlist Test user_traditionlist and add_tradition on user --- diff --git a/lib/Text/Tradition/Directory.pm b/lib/Text/Tradition/Directory.pm index b697f7c..55b7986 100644 --- a/lib/Text/Tradition/Directory.pm +++ b/lib/Text/Tradition/Directory.pm @@ -274,11 +274,37 @@ sub tradition { return $obj; } +sub user_traditionlist { + my ($self, $user) = @_; + + my @tlist; + if($user && $user ne 'public') { + ## We have a user object already, so just fetch its traditions and use tose + foreach my $t (@{ $self->lookup($user)->traditions }) { + push( @tlist, { 'id' => $self->object_to_id( $t ), + 'name' => $t->name } ); + } + return @tlist; + } + + ## Search for all traditions which allow public viewing + ## When they exist! + # $self->search({ public => 1 }); + + ## For now, just fetch all + ## (could use all_objects or grep down there?) + return $self->traditionlist(); +} + sub traditionlist { my $self = shift; + my ($user) = @_; + + return $self->user_traditionlist($user) if($user); + + my @tlist; # If we are using DBI, we can do it the easy way; if not, the hard way. # Easy way still involves making a separate DBI connection. Ew. - my @tlist; if( $self->dsn =~ /^dbi:(\w+):/ ) { my $dbtype = $1; my @connection = @{$self->directory->backend->connect_info}; diff --git a/lib/Text/Tradition/User.pm b/lib/Text/Tradition/User.pm index c78b8e2..e57b8b8 100644 --- a/lib/Text/Tradition/User.pm +++ b/lib/Text/Tradition/User.pm @@ -85,3 +85,4 @@ Returns an ArrayRef of L objects belonging to this user. Inherited from KiokuX::User, verifies a given password string against the stored encrypted version. + diff --git a/t/text_tradition_user.t b/t/text_tradition_user.t index 7dfa930..62520de 100644 --- a/t/text_tradition_user.t +++ b/t/text_tradition_user.t @@ -78,3 +78,25 @@ ok($changed->check_password('passbloggs'), 'Modified & retrieved with correct ne ok($gone && !$d_user, 'Deleted user completely from store'); } +{ +## add_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 => 'testadd', + password => 'testingtraditions' }); + $user->add_tradition($t); + $user_store->update($user); +# $userstore->update($t); + + is( scalar @{$user->traditions}, 1, 'Added one tradition'); + + my @tlist = $user_store->traditionlist($user->kiokudb_object_id); + is($tlist[0]->name, $t->name, 'Traditionlist returns stored user->tradition'); +} +