From f86b9b3d9d80daff34ed967799ff2925a2d6dceb Mon Sep 17 00:00:00 2001 From: Mauricio Giacomini Girardello <mauriciogiacomini4@gmail.com> Date: Wed, 18 Nov 2015 16:28:12 -0200 Subject: [PATCH] adding OrientDbSerializable module in collection model --- app/models/collection.rb | 11 +++++++++- app/models/concerns/orient_db_serializable.rb | 4 +++- .../orient_db/collection_repository.rb | 10 ++++----- test/models/collection_test.rb | 21 +++++++++++++++++++ test/models/learning_object_test.rb | 2 +- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/app/models/collection.rb b/app/models/collection.rb index 015116ae..f9f412e2 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -1,5 +1,6 @@ class Collection include ActiveModel::Model + include OrientDbSerializable include RepositoriesProxy attr_accessor :id, :created_at, :last_modified, :name, :description, :privacy, :owner, :learning_objects @@ -42,7 +43,15 @@ class Collection !@name.blank? end - private + #def to_orientdb_hash + # + #end + + protected + + def excluded_orientdb_properties + ['owner'] + end def defaults { diff --git a/app/models/concerns/orient_db_serializable.rb b/app/models/concerns/orient_db_serializable.rb index ae7ff682..e8e222f5 100644 --- a/app/models/concerns/orient_db_serializable.rb +++ b/app/models/concerns/orient_db_serializable.rb @@ -20,6 +20,8 @@ module OrientDbSerializable hash end - def excluded_orientdb_properties; end + def excluded_orientdb_properties + [] + end end \ No newline at end of file diff --git a/app/repositories/orient_db/collection_repository.rb b/app/repositories/orient_db/collection_repository.rb index 67836427..064b5c01 100644 --- a/app/repositories/orient_db/collection_repository.rb +++ b/app/repositories/orient_db/collection_repository.rb @@ -4,10 +4,6 @@ module OrientDb include OrientDb::Methods::FinderMethods include RepositoriesProxy - def build_object(args={}) - CollectionBuilder.build_from_orientdb args - end - def save_learning_objects(collection = Collection.new) if collection.id.to_s.empty? raise NotPersistedRecordError, 'Before save learning objects to collection, you must persist using CollectionRepository::create method' @@ -116,7 +112,11 @@ module OrientDb update_property(collection, 'privacy', privacy) if privacy_accepted?(privacy) end - private + protected + + def build_object(args={}) + CollectionBuilder.build_from_orientdb args + end def accepted_properties ['privacy'] diff --git a/test/models/collection_test.rb b/test/models/collection_test.rb index d46e27a1..eac7a38b 100644 --- a/test/models/collection_test.rb +++ b/test/models/collection_test.rb @@ -31,4 +31,25 @@ class CollectionTest < ActiveSupport::TestCase assert_count 1, collection.learning_objects end + test 'serialize collection for orientdb' do + collection = Collection.new(name: 'Minha coleção de teste') + assert_equal(build_collection_hash(collection), collection.to_orientdb_hash) + #collection.add LearningObject.new(id: '#43:34') + #collection.add LearningObject.new(id: '#43:53') + #assert_equal({"@class" => "Collection", "created_at" => "2015-11-18 16:10:52", "last_modified" => "2015-11-18 16:10:52", "learning_objects" => ['#43:34', '#43:53'], "privacy" => "private", "name" => "Minha coleção de teste"}, collection.to_orientdb_hash) + end + + private + + def build_collection_hash(collection) + { + "@class" => "Collection", + "created_at" => collection.created_at, + "last_modified" => collection.last_modified, + "learning_objects" => [], + "privacy" => collection.privacy, + "name" => collection.name + } + end + end \ No newline at end of file diff --git a/test/models/learning_object_test.rb b/test/models/learning_object_test.rb index 5d524385..3a7c972e 100644 --- a/test/models/learning_object_test.rb +++ b/test/models/learning_object_test.rb @@ -13,7 +13,7 @@ class LearningObjectTest < ActiveSupport::TestCase thumbnail: '/asdasdasd.jpg', id_dspace: '123213', type: 'image', - file: LearningObject::Attachment.new('[{"id":2203,"name":"Pontociencia_Efe_chamas_na_horizontal.flv","type":"bitstream","link":"/rest/bitstreams/2203","bundleName":"ORIGINAL","description":null,"format":"Unknown","mimeType":"application/octet-stream","sizeBytes":4108209,"parentObject":null,"retrieveLink":"/bitstreams/2203/retrieve","checkSum":{"value":"43bd43bea944dcb8708d6ec708fb510d","checkSumAlgorithm":"MD5"},"sequenceId":-1,"policies":null,"expand":["parent","policies","all"]}]'), + attachment: LearningObject::Attachment.new('[{"id":2203,"name":"Pontociencia_Efe_chamas_na_horizontal.flv","type":"bitstream","link":"/rest/bitstreams/2203","bundleName":"ORIGINAL","description":null,"format":"Unknown","mimeType":"application/octet-stream","sizeBytes":4108209,"parentObject":null,"retrieveLink":"/bitstreams/2203/retrieve","checkSum":{"value":"43bd43bea944dcb8708d6ec708fb510d","checkSumAlgorithm":"MD5"},"sequenceId":-1,"policies":null,"expand":["parent","policies","all"]}]'), last_modified: '2015-03-10 15:33:45', metadata: [] ) -- GitLab