From 37bd55f6981b1389a21b9f689d6bed3c67a8599b Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Tue, 8 Nov 2016 13:29:07 +0200 Subject: [PATCH] Bug 17499: Simplify searching by message_name This patch adds optional search & find parameter message_name for Koha::Patron::Message::Preferences->search and ->find. This simplifies object usage by allowing us to skip joins or finding the attribute id ourselves. --- Koha/Patron/Message/Preference.pm | 22 ++++++++++ Koha/Patron/Message/Preferences.pm | 55 ++++++++++++++++++++++++ t/db_dependent/Koha/Patron/Message/Preferences.t | 12 +++++- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/Koha/Patron/Message/Preference.pm b/Koha/Patron/Message/Preference.pm index 4ca3fe5..e5e5f0f 100644 --- a/Koha/Patron/Message/Preference.pm +++ b/Koha/Patron/Message/Preference.pm @@ -154,6 +154,28 @@ sub new_from_default { return $self; } +=head3 message_name + +$preference->message_name + +Gets message_name for this messaging preference. + +Setter not implemented. + +=cut + +sub message_name { + my ($self) = @_; + + if ($self->{'_message_name'}) { + return $self->{'_message_name'}; + } + $self->{'_message_name'} = Koha::Patron::Message::Attributes->find({ + message_attribute_id => $self->message_attribute_id, + })->message_name; + return $self->{'_message_name'}; +} + =head3 message_transport_types $preference->message_transport_types diff --git a/Koha/Patron/Message/Preferences.pm b/Koha/Patron/Message/Preferences.pm index bf3d14c..34af5e0 100644 --- a/Koha/Patron/Message/Preferences.pm +++ b/Koha/Patron/Message/Preferences.pm @@ -20,6 +20,7 @@ package Koha::Patron::Message::Preferences; use Modern::Perl; use Koha::Database; +use Koha::Patron::Message::Attributes; use Koha::Patron::Message::Preference; use Koha::Patron::Message::Transports; @@ -35,6 +36,32 @@ Koha::Patron::Message::Preferences - Koha Patron Message Preferences object clas =cut +=head3 find_with_message_name + +Koha::Patron::Message::Preferences->find_with_message_name({ + borrowernumber => 123, + message_name => 'Hold_Filled', +}); + +Converts C into C and continues find. + +=cut + +sub find_with_message_name { + my ($self, $id) = @_; + + if (ref($id) eq "HASH" && $id->{'message_name'}) { + my $attr = Koha::Patron::Message::Attributes->find({ + message_name => $id->{'message_name'}, + }); + $id->{'message_attribute_id'} = ($attr) ? + $attr->message_attribute_id : undef; + delete $id->{'message_name'}; + } + + return $self->SUPER::find($id); +} + =head3 get_options my $messaging_options = Koha::Patron::Message::Preferences->get_options @@ -69,6 +96,34 @@ sub get_options { return \@return; } +=head3 search + +Koha::Patron::Message::Preferences->search_with_message_name({ + borrowernumber => 123, + message_name => 'Hold_Filled', +}); + +Converts C into C and continues search. Use +Koha::Patron::Message::Preferences->search with a proper join for more complicated +searches. + +=cut + +sub search_with_message_name { + my ($self, $params, $attributes) = @_; + + if (ref($params) eq "HASH" && $params->{'message_name'}) { + my $attr = Koha::Patron::Message::Attributes->find({ + message_name => $params->{'message_name'}, + }); + $params->{'message_attribute_id'} = ($attr) ? + $attr->message_attribute_id : undef; + delete $params->{'message_name'}; + } + + return $self->SUPER::search($params, $attributes); +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t index 4998d80..78b18b5 100644 --- a/t/db_dependent/Koha/Patron/Message/Preferences.t +++ b/t/db_dependent/Koha/Patron/Message/Preferences.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 17; +use Test::More tests => 18; use t::lib::Mocks; use t::lib::TestBuilder; @@ -351,6 +351,16 @@ subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub }; }; +subtest 'Test Koha::Patron::Message::Preference->message_name' => sub { + plan tests => 1; + + my $message_name_pref = Koha::Patron::Message::Preferences->search_with_message_name({ + borrowernumber => $patron->{'borrowernumber'}, + message_name => $attribute->message_name, + })->next; + is($message_name_pref->message_name, $attribute->message_name, "Found preference with message_name"); +}; + $schema->storage->txn_rollback; 1; -- 2.7.4