Bug 24321: Add Koha::Objects->attributes_from_api
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 31 Dec 2019 12:16:17 +0000 (09:16 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 8 Jan 2020 14:41:23 +0000 (14:41 +0000)
This patch makes the 'attributes_from_api' method from the singular
class available from the result set class.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Objects.t
=> SUCCESS: Tests pass!
4. Sign off :-D
5. Yeah, I skipped 3 :-P

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Koha/Objects.pm
t/db_dependent/Koha/Objects.t

index 31387e1..165511a 100644 (file)
@@ -320,6 +320,21 @@ sub to_api {
     return [ map { $_->to_api } $self->as_list ];
 }
 
+=head3 attributes_from_api
+
+    my $attributes = $objects->attributes_from_api( $api_attributes );
+
+Translates attributes from the API to DBIC
+
+=cut
+
+sub attributes_from_api {
+    my ( $self, $attributes ) = @_;
+
+    $self->{_singular_object} ||= $self->object_class->new();
+    return $self->{_singular_object}->attributes_from_api( $attributes );
+}
+
 =head3 Koha::Objects->_wrap
 
 wraps the DBIC object in a corresponding Koha object
index f6a3ecd..513d2ff 100644 (file)
@@ -675,3 +675,25 @@ subtest 'Return same values as DBIx::Class' => sub {
 
     };
 };
+
+subtest "attributes_from_api() tests" => sub {
+
+    plan tests => 1;
+
+    $schema->storage->txn_begin;
+
+    my $cities_rs = Koha::Cities->new;
+    my $city      = Koha::City->new;
+
+    my $api_attributes = {
+        name        => 'Cordoba',
+        postal_code => 5000
+    };
+
+    is_deeply(
+        $cities_rs->attributes_from_api($api_attributes),
+        $city->attributes_from_api($api_attributes)
+    );
+
+    $schema->storage->txn_rollback;
+};