From e933e607bc0b7887d2e16634fbed28323bea99f2 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 30 Dec 2016 11:12:32 -0300 Subject: [PATCH] Bug 17828: Overload Koha::Patron::Attribute->store to check for unique_id This patch introduces a Koha::Patron::Attribute->store method that does the required check for the corresponding Koha::Patron::Attribute::Type and raises an exception if trying to store more than one unique attribute. This patch introduces an exception definition for that purpose. To test: - Apply the unit tests patches - Run: $ prove t/db_dependent/Koha/Patron/Attributes.t => FAIL: Tests fail - Apply this patch - Run: $ prove t/db_dependent/Koha/Patron/Attributes.t => SUCCESS: Tests pass! - Sign off :-D --- Koha/Exceptions/Patron/Attribute.pm | 16 ++++++++++++++++ Koha/Patron/Attribute.pm | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 Koha/Exceptions/Patron/Attribute.pm diff --git a/Koha/Exceptions/Patron/Attribute.pm b/Koha/Exceptions/Patron/Attribute.pm new file mode 100644 index 0000000000..2df1a0151f --- /dev/null +++ b/Koha/Exceptions/Patron/Attribute.pm @@ -0,0 +1,16 @@ +package Koha::Exceptions::Patron::Attribute; + +use Modern::Perl; + +use Exception::Class ( + + 'Koha::Exceptions::Patron::Attribute' => { + description => 'Something went wrong' + }, + 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint' => { + isa => 'Koha::Exceptions::Patron::Attribute', + description => "unique_id set for attribute type and tried to add a new attribute for the same code" + } +); + +1; diff --git a/Koha/Patron/Attribute.pm b/Koha/Patron/Attribute.pm index 7643536b7c..c133f0026f 100644 --- a/Koha/Patron/Attribute.pm +++ b/Koha/Patron/Attribute.pm @@ -17,6 +17,8 @@ package Koha::Patron::Attribute; use Modern::Perl; +use Koha::Database; +use Koha::Exceptions::Patron::Attribute; use Koha::Patron::Attribute::Types; use base qw(Koha::Object); @@ -31,6 +33,32 @@ Koha::Patron::Attribute - Koha Patron Attribute Object class =cut +=head3 store + + my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... }); + try { $attribute->store } + catch { handle_exception }; + +=cut + +sub store { + + my $self = shift; + + if ( Koha::Patron::Attribute::Types->find( $self->code )->unique_id ) { + my $attr_count + = Koha::Database->new->schema->resultset( $self->_type )->search( + { borrowernumber => $self->borrowernumber, + code => $self->code + } + )->count; + Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw() + if $attr_count > 0; + } + + return $self->SUPER::store(); +} + =head3 opac_display my $attribute = Koha::Patron::Attribute->new({ code => 'a_code', ... }); -- 2.11.0