active-fedora 2.3.4 → 2.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +5 -4
- data/History.txt +16 -0
- data/README.textile +1 -1
- data/active-fedora.gemspec +1 -1
- data/lib/active_fedora.rb +4 -4
- data/lib/active_fedora/relationships_helper.rb +889 -0
- data/lib/active_fedora/semantic_node.rb +53 -383
- data/lib/active_fedora/version.rb +1 -1
- data/spec/integration/base_spec.rb +87 -170
- data/spec/integration/datastream_spec.rb +3 -3
- data/spec/integration/datastreams_crud_spec.rb +1 -1
- data/spec/integration/rels_ext_datastream_spec.rb +4 -4
- data/spec/integration/semantic_node_spec.rb +369 -10
- data/spec/unit/active_fedora_spec.rb +42 -19
- data/spec/unit/base_extra_spec.rb +1 -0
- data/spec/unit/base_spec.rb +16 -20
- data/spec/unit/relationships_helper_spec.rb +842 -0
- data/spec/unit/semantic_node_spec.rb +49 -323
- metadata +88 -85
@@ -1,13 +1,17 @@
|
|
1
|
+
require 'active_fedora/relationships_helper'
|
2
|
+
|
1
3
|
module ActiveFedora
|
2
4
|
module SemanticNode
|
3
5
|
include MediaShelfClassLevelInheritableAttributes
|
4
|
-
ms_inheritable_attributes :class_relationships, :internal_uri, :class_named_relationships_desc
|
5
6
|
|
6
|
-
|
7
|
+
ms_inheritable_attributes :class_relationships, :internal_uri
|
8
|
+
|
9
|
+
attr_accessor :internal_uri, :relationships_are_dirty, :load_from_solr
|
7
10
|
|
8
11
|
|
9
12
|
def self.included(klass)
|
10
13
|
klass.extend(ClassMethods)
|
14
|
+
klass.send(:include, ActiveFedora::RelationshipsHelper)
|
11
15
|
end
|
12
16
|
|
13
17
|
def assert_kind_of(n, o,t)
|
@@ -39,23 +43,6 @@ module ActiveFedora
|
|
39
43
|
relationships[subject][predicate] = []
|
40
44
|
end
|
41
45
|
end
|
42
|
-
|
43
|
-
# ** EXPERIMENTAL **
|
44
|
-
#
|
45
|
-
# Internal method that ensures a relationship subject such as :self and :inbound
|
46
|
-
# exist within the named_relationships_desc hash tracking named relationships metadata.
|
47
|
-
# This method just calls the class method counterpart of this method.
|
48
|
-
def register_named_subject(subject)
|
49
|
-
self.class.register_named_subject(subject)
|
50
|
-
end
|
51
|
-
|
52
|
-
# ** EXPERIMENTAL **
|
53
|
-
#
|
54
|
-
# Internal method that adds relationship name and predicate pair to either an outbound (:self)
|
55
|
-
# or inbound (:inbound) relationship types. This method just calls the class method counterpart of this method.
|
56
|
-
def register_named_relationship(subject, name, predicate, opts)
|
57
|
-
self.class.register_named_relationship(subject, name, predicate, opts)
|
58
|
-
end
|
59
46
|
|
60
47
|
# ** EXPERIMENTAL **
|
61
48
|
#
|
@@ -89,7 +76,7 @@ module ActiveFedora
|
|
89
76
|
|
90
77
|
def inbound_relationships(response_format=:uri)
|
91
78
|
rel_values = {}
|
92
|
-
|
79
|
+
inbound_relationship_predicates.each_pair do |name,predicate|
|
93
80
|
objects = self.send("#{name}",{:response_format=>response_format})
|
94
81
|
items = []
|
95
82
|
objects.each do |object|
|
@@ -134,266 +121,6 @@ module ActiveFedora
|
|
134
121
|
return rels
|
135
122
|
end
|
136
123
|
|
137
|
-
# ** EXPERIMENTAL **
|
138
|
-
#
|
139
|
-
# Return array of objects for a given relationship name
|
140
|
-
def named_relationship(name)
|
141
|
-
rels = nil
|
142
|
-
if inbound_relationship_names.include?(name)
|
143
|
-
rels = named_relationships(false)[:inbound][name]
|
144
|
-
elsif outbound_relationship_names.include?(name)
|
145
|
-
rels = named_relationships[:self][name]
|
146
|
-
end
|
147
|
-
rels = [] if rels.nil?
|
148
|
-
return rels
|
149
|
-
end
|
150
|
-
|
151
|
-
# ** EXPERIMENTAL **
|
152
|
-
#
|
153
|
-
# Gets the named relationships hash of subject=>name=>object_array
|
154
|
-
# It has an optional parameter of outbound_only that defaults true.
|
155
|
-
# If false it will include inbound relationships in the results.
|
156
|
-
# Also, it will only reload outbound relationships if the relationships hash has changed
|
157
|
-
# since the last time this method was called.
|
158
|
-
def named_relationships(outbound_only=true)
|
159
|
-
#make sure to update if relationships have been updated
|
160
|
-
if @relationships_are_dirty == true
|
161
|
-
@named_relationships = named_relationships_from_class()
|
162
|
-
@relationships_are_dirty = false
|
163
|
-
end
|
164
|
-
|
165
|
-
#this will get called normally on first fetch if relationships are not dirty
|
166
|
-
@named_relationships ||= named_relationships_from_class()
|
167
|
-
outbound_only ? @named_relationships : @named_relationships.merge(:inbound=>named_inbound_relationships)
|
168
|
-
end
|
169
|
-
|
170
|
-
# ** EXPERIMENTAL **
|
171
|
-
#
|
172
|
-
# Gets named relationships from the class using the current relationships hash
|
173
|
-
# and relationship name,predicate pairs.
|
174
|
-
def named_relationships_from_class()
|
175
|
-
rels = {}
|
176
|
-
named_relationship_predicates.each_pair do |subj, names|
|
177
|
-
if relationships.has_key?(subj)
|
178
|
-
rels[subj] = {}
|
179
|
-
names.each_pair do |name, predicate|
|
180
|
-
rels[subj][name] = (relationships[subj].has_key?(predicate) ? relationships[subj][predicate] : [])
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
return rels
|
185
|
-
end
|
186
|
-
|
187
|
-
# ** EXPERIMENTAL **
|
188
|
-
#
|
189
|
-
# Return hash of named_relationships defined within other objects' RELS-EXT
|
190
|
-
# It returns a hash of relationship name to arrays of objects. It requeries
|
191
|
-
# solr each time this method is called.
|
192
|
-
def named_inbound_relationships
|
193
|
-
rels = {}
|
194
|
-
if named_relationships_desc.has_key?(:inbound)&&!named_relationships_desc[:inbound].empty?()
|
195
|
-
inbound_rels = inbound_relationships
|
196
|
-
|
197
|
-
if named_relationship_predicates.has_key?(:inbound)
|
198
|
-
named_relationship_predicates[:inbound].each do |name, predicate|
|
199
|
-
rels[name] = inbound_rels.has_key?(predicate) ? inbound_rels[predicate] : []
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
return rels
|
204
|
-
end
|
205
|
-
|
206
|
-
# ** EXPERIMENTAL **
|
207
|
-
#
|
208
|
-
# Return hash of outbound relationship names and predicate pairs
|
209
|
-
def outbound_named_relationship_predicates
|
210
|
-
named_relationship_predicates.has_key?(:self) ? named_relationship_predicates[:self] : {}
|
211
|
-
end
|
212
|
-
|
213
|
-
# ** EXPERIMENTAL **
|
214
|
-
#
|
215
|
-
# Return hash of inbound relationship names and predicate pairs
|
216
|
-
def inbound_named_relationship_predicates
|
217
|
-
named_relationship_predicates.has_key?(:inbound) ? named_relationship_predicates[:inbound] : {}
|
218
|
-
end
|
219
|
-
|
220
|
-
# ** EXPERIMENTAL **
|
221
|
-
#
|
222
|
-
# Return hash of relationship names and predicate pairs
|
223
|
-
def named_relationship_predicates
|
224
|
-
@named_relationship_predicates ||= named_relationship_predicates_from_class
|
225
|
-
end
|
226
|
-
|
227
|
-
# ** EXPERIMENTAL **
|
228
|
-
#
|
229
|
-
# Return hash of relationship names and predicate pairs from class
|
230
|
-
def named_relationship_predicates_from_class
|
231
|
-
rels = {}
|
232
|
-
named_relationships_desc.each_pair do |subj, names|
|
233
|
-
rels[subj] = {}
|
234
|
-
names.each_pair do |name, args|
|
235
|
-
rels[subj][name] = args[:predicate]
|
236
|
-
end
|
237
|
-
end
|
238
|
-
return rels
|
239
|
-
end
|
240
|
-
|
241
|
-
# ** EXPERIMENTAL **
|
242
|
-
#
|
243
|
-
# Return array all relationship names
|
244
|
-
def relationship_names
|
245
|
-
names = []
|
246
|
-
named_relationships_desc.each_key do |subject|
|
247
|
-
names = names.concat(named_relationships_desc[subject].keys)
|
248
|
-
end
|
249
|
-
names
|
250
|
-
end
|
251
|
-
|
252
|
-
# ** EXPERIMENTAL **
|
253
|
-
#
|
254
|
-
# Return array of relationship names for all named inbound relationships (coming from other objects' RELS-EXT and Solr)
|
255
|
-
def inbound_relationship_names
|
256
|
-
named_relationships_desc.has_key?(:inbound) ? named_relationships_desc[:inbound].keys : []
|
257
|
-
end
|
258
|
-
|
259
|
-
# ** EXPERIMENTAL **
|
260
|
-
#
|
261
|
-
# Return array of relationship names for all named outbound relationships (coming from this object's RELS-EXT)
|
262
|
-
def outbound_relationship_names
|
263
|
-
named_relationships_desc.has_key?(:self) ? named_relationships_desc[:self].keys : []
|
264
|
-
end
|
265
|
-
|
266
|
-
# ** EXPERIMENTAL **
|
267
|
-
#
|
268
|
-
# Return hash of named_relationships defined within this object's RELS-EXT
|
269
|
-
# It returns a hash of relationship name to arrays of objects
|
270
|
-
def named_outbound_relationships
|
271
|
-
named_relationships_desc.has_key?(:self) ? named_relationships[:self] : {}
|
272
|
-
end
|
273
|
-
|
274
|
-
# ** EXPERIMENTAL **
|
275
|
-
#
|
276
|
-
# Returns true if the given relationship name is a named relationship
|
277
|
-
# ====Parameters
|
278
|
-
# name: Name of relationship
|
279
|
-
# outbound_only: If false checks inbound relationships as well (defaults to true)
|
280
|
-
def is_named_relationship?(name, outbound_only=true)
|
281
|
-
if outbound_only
|
282
|
-
outbound_relationship_names.include?(name)
|
283
|
-
else
|
284
|
-
(outbound_relationship_names.include?(name)||inbound_relationship_names.include?(name))
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
# ** EXPERIMENTAL **
|
289
|
-
#
|
290
|
-
# Return hash that stores named relationship metadata defined by has_relationship calls
|
291
|
-
# ====Example
|
292
|
-
# For the following relationship
|
293
|
-
#
|
294
|
-
# has_relationship "audio_records", :has_part, :type=>AudioRecord
|
295
|
-
# Results in the following returned by named_relationships_desc
|
296
|
-
# {:self=>{"audio_records"=>{:type=>AudioRecord, :singular=>nil, :predicate=>:has_part, :inbound=>false}}}
|
297
|
-
def named_relationships_desc
|
298
|
-
@named_relationships_desc ||= named_relationships_desc_from_class
|
299
|
-
end
|
300
|
-
|
301
|
-
# ** EXPERIMENTAL **
|
302
|
-
#
|
303
|
-
# Get class instance variable named_relationships_desc that holds has_relationship metadata
|
304
|
-
def named_relationships_desc_from_class
|
305
|
-
self.class.named_relationships_desc
|
306
|
-
end
|
307
|
-
|
308
|
-
# ** EXPERIMENTAL **
|
309
|
-
#
|
310
|
-
# Return the value of :type for the relationship for name passed in.
|
311
|
-
# It defaults to ActiveFedora::Base.
|
312
|
-
def named_relationship_type(name)
|
313
|
-
if is_named_relationship?(name,true)
|
314
|
-
subject = outbound_relationship_names.include?(name)? :self : :inbound
|
315
|
-
if named_relationships_desc[subject][name].has_key?(:type)
|
316
|
-
return class_from_name(named_relationships_desc[subject][name][:type])
|
317
|
-
end
|
318
|
-
end
|
319
|
-
return nil
|
320
|
-
end
|
321
|
-
|
322
|
-
# ** EXPERIMENTAL **
|
323
|
-
#
|
324
|
-
# Add an outbound relationship for given named relationship
|
325
|
-
# See ActiveFedora::SemanticNode::ClassMethods.has_relationship
|
326
|
-
def add_named_relationship(name, object)
|
327
|
-
if is_named_relationship?(name,true)
|
328
|
-
if named_relationships_desc[:self][name].has_key?(:type)
|
329
|
-
klass = class_from_name(named_relationships_desc[:self][name][:type])
|
330
|
-
unless klass.nil?
|
331
|
-
(assert_kind_of_model 'object', object, klass)
|
332
|
-
end
|
333
|
-
end
|
334
|
-
#r = ActiveFedora::Relationship.new({:subject=>:self,:predicate=>outbound_named_relationship_predicates[name],:object=>object})
|
335
|
-
#add_relationship(r)
|
336
|
-
add_relationship(outbound_named_relationship_predicates[name],object)
|
337
|
-
else
|
338
|
-
false
|
339
|
-
end
|
340
|
-
end
|
341
|
-
|
342
|
-
# ** EXPERIMENTAL **
|
343
|
-
#
|
344
|
-
# Remove an object from the named relationship
|
345
|
-
def remove_named_relationship(name, object)
|
346
|
-
if is_named_relationship?(name,true)
|
347
|
-
remove_relationship(outbound_named_relationship_predicates[name],object)
|
348
|
-
else
|
349
|
-
return false
|
350
|
-
end
|
351
|
-
end
|
352
|
-
|
353
|
-
# ** EXPERIMENTAL **
|
354
|
-
#
|
355
|
-
# Throws an assertion error if kind_of_model? returns false for object and model_class
|
356
|
-
# ====Parameters
|
357
|
-
# name: Name of object (just label for output)
|
358
|
-
# object: The object to test
|
359
|
-
# model_class: The model class used to in kind_of_model? check on object
|
360
|
-
def assert_kind_of_model(name, object, model_class)
|
361
|
-
raise "Assertion failure: #{name}: #{object.pid} does not have model #{model_class}, it has model #{relationships[:self][:has_model]}" unless object.kind_of_model?(model_class)
|
362
|
-
end
|
363
|
-
|
364
|
-
# ** EXPERIMENTAL **
|
365
|
-
#
|
366
|
-
# Checks that this object is matches the model class passed in.
|
367
|
-
# It requires two steps to pass to return true
|
368
|
-
# 1. It has a hasModel relationship of the same model
|
369
|
-
# 2. kind_of? returns true for the model passed in
|
370
|
-
# This method can most often be used to detect if an object from Fedora that was created
|
371
|
-
# with a different model was then used to populate this object.
|
372
|
-
def kind_of_model?(model_class)
|
373
|
-
if self.kind_of?(model_class)
|
374
|
-
#check has model and class match
|
375
|
-
if relationships[:self].has_key?(:has_model)
|
376
|
-
r = ActiveFedora::Relationship.new(:subject=>:self, :predicate=>:has_model, :object=>ActiveFedora::ContentModel.pid_from_ruby_class(self.class))
|
377
|
-
if relationships[:self][:has_model].first.to_s.eql?(r.object.to_s)
|
378
|
-
return true
|
379
|
-
else
|
380
|
-
raise "has_model relationship check failed for model #{model_class} raising exception, expected: '#{r.object.to_s}' actual: '#{relationships[:self][:has_model].to_s}'"
|
381
|
-
end
|
382
|
-
else
|
383
|
-
raise "has_model relationship does not exist for model #{model_class} check raising exception"
|
384
|
-
end
|
385
|
-
else
|
386
|
-
raise "kind_of? check failed for model #{model_class}, actual #{self.class} raising exception"
|
387
|
-
end
|
388
|
-
return false
|
389
|
-
end
|
390
|
-
|
391
|
-
def class_from_name(name)
|
392
|
-
klass = name.to_s.split('::').inject(Kernel) {|scope, const_name|
|
393
|
-
scope.const_get(const_name)}
|
394
|
-
(!klass.nil? && klass.is_a?(::Class)) ? klass : nil
|
395
|
-
end
|
396
|
-
|
397
124
|
# Creates a RELS-EXT datastream for insertion into a Fedora Object
|
398
125
|
# @param [String] pid
|
399
126
|
# @param [Hash] relationships (optional) @default self.relationships
|
@@ -448,17 +175,21 @@ module ActiveFedora
|
|
448
175
|
end
|
449
176
|
xml.to_s
|
450
177
|
end
|
451
|
-
|
178
|
+
|
452
179
|
module ClassMethods
|
453
|
-
|
180
|
+
#include ActiveFedora::RelationshipsHelper::ClassMethods
|
181
|
+
|
454
182
|
# Allows for a relationship to be treated like any other attribute of a model class. You define
|
455
|
-
#
|
183
|
+
# relationships in your model class using this method. You then have access to several
|
456
184
|
# helper methods to list, append, and remove objects from the list of relationships.
|
457
185
|
# ====Examples to define two relationships
|
458
186
|
# class AudioRecord < ActiveFedora::Base
|
459
187
|
#
|
460
188
|
# has_relationship "oral_history", :has_part, :inbound=>true, :type=>OralHistory
|
189
|
+
# # returns all similar audio
|
461
190
|
# has_relationship "similar_audio", :has_part, :type=>AudioRecord
|
191
|
+
# #returns only similar audio with format wav
|
192
|
+
# has_relationship "similar_audio_wav", :has_part, :solr_fq=>"format_t:wav"
|
462
193
|
#
|
463
194
|
# The first two parameters are required:
|
464
195
|
# name: relationship name
|
@@ -467,6 +198,7 @@ module ActiveFedora
|
|
467
198
|
# possible parameters
|
468
199
|
# :inbound => if true loads an external relationship via Solr (defaults to false)
|
469
200
|
# :type => The type of model to use when instantiated an object from the pid in this relationship (defaults to ActiveFedora::Base)
|
201
|
+
# :solr_fq => Define a solr query here if you want to filter out some objects in your relationship (must be a properly formatted solr query)
|
470
202
|
#
|
471
203
|
# If inbound is true it expects the relationship to be defined by another object's RELS-EXT
|
472
204
|
# and to load that relationship from Solr. Otherwise, if inbound is true the relationship is stored in
|
@@ -478,25 +210,27 @@ module ActiveFedora
|
|
478
210
|
# For the oral_history relationship in the example above the following helper methods are created:
|
479
211
|
# oral_history: returns array of OralHistory objects that have this AudioRecord with predicate :has_part
|
480
212
|
# oral_history_ids: Return array of pids for OralHistory objects that have this AudioRecord with predicate :has_part
|
213
|
+
# oral_history_query: Return solr query that can be used to retrieve related objects as solr documents
|
481
214
|
#
|
482
215
|
# For the outbound relationship "similar_audio" there are two additional methods to append and remove objects from that relationship
|
483
216
|
# since it is managed internally:
|
484
217
|
# similar_audio: Return array of AudioRecord objects that have been added to similar_audio relationship
|
485
218
|
# similar_audio_ids: Return array of AudioRecord object pids that have been added to similar_audio relationship
|
219
|
+
# similar_audio_query: Return solr query that can be used to retrieve related objects as solr documents
|
486
220
|
# similar_audio_append: Add an AudioRecord object to the similar_audio relationship
|
487
221
|
# similar_audio_remove: Remove an AudioRecord from the similar_audio relationship
|
488
222
|
def has_relationship(name, predicate, opts = {})
|
489
223
|
opts = {:singular => nil, :inbound => false}.merge(opts)
|
490
224
|
if opts[:inbound] == true
|
491
|
-
raise "Duplicate use of predicate for
|
492
|
-
|
225
|
+
#raise "Duplicate use of predicate for inbound relationship name not allowed" if predicate_exists_with_different_relationship_name?(:inbound,name,predicate)
|
226
|
+
register_relationship_desc(:inbound, name, predicate, opts)
|
493
227
|
register_predicate(:inbound, predicate)
|
494
228
|
create_inbound_relationship_finders(name, predicate, opts)
|
495
229
|
else
|
496
|
-
raise "Duplicate use of predicate for
|
497
|
-
|
230
|
+
#raise "Duplicate use of predicate for outbound relationship name not allowed" if predicate_exists_with_different_relationship_name?(:self,name,predicate)
|
231
|
+
register_relationship_desc(:self, name, predicate, opts)
|
498
232
|
register_predicate(:self, predicate)
|
499
|
-
|
233
|
+
create_relationship_name_methods(name)
|
500
234
|
create_outbound_relationship_finders(name, predicate, opts)
|
501
235
|
end
|
502
236
|
end
|
@@ -519,96 +253,14 @@ module ActiveFedora
|
|
519
253
|
def has_bidirectional_relationship(name, outbound_predicate, inbound_predicate, opts={})
|
520
254
|
create_bidirectional_relationship_finders(name, outbound_predicate, inbound_predicate, opts)
|
521
255
|
end
|
522
|
-
|
523
|
-
# ** EXPERIMENTAL **
|
524
|
-
#
|
525
|
-
# Check to make sure a subject,name, and predicate triple does not already exist
|
526
|
-
# with the same subject but different name.
|
527
|
-
# This method is used to ensure conflicting has_relationship calls are not made because
|
528
|
-
# predicates cannot be reused across relationship names. Otherwise, the mapping of relationship name
|
529
|
-
# to predicate in RELS-EXT would be broken.
|
530
|
-
def named_predicate_exists_with_different_name?(subject,name,predicate)
|
531
|
-
if named_relationships_desc.has_key?(subject)
|
532
|
-
named_relationships_desc[subject].each_pair do |existing_name, args|
|
533
|
-
return true if !args[:predicate].nil? && args[:predicate] == predicate && existing_name != name
|
534
|
-
end
|
535
|
-
end
|
536
|
-
return false
|
537
|
-
end
|
538
|
-
|
539
|
-
# ** EXPERIMENTAL **
|
540
|
-
#
|
541
|
-
# Return hash that stores named relationship metadata defined by has_relationship calls
|
542
|
-
# ====Example
|
543
|
-
# For the following relationship
|
544
|
-
#
|
545
|
-
# has_relationship "audio_records", :has_part, :type=>AudioRecord
|
546
|
-
# Results in the following returned by named_relationships_desc
|
547
|
-
# {:self=>{"audio_records"=>{:type=>AudioRecord, :singular=>nil, :predicate=>:has_part, :inbound=>false}}}
|
548
|
-
def named_relationships_desc
|
549
|
-
@class_named_relationships_desc ||= Hash[:self => {}]
|
550
|
-
end
|
551
|
-
|
552
|
-
# ** EXPERIMENTAL **
|
553
|
-
#
|
554
|
-
# Internal method that ensures a relationship subject such as :self and :inbound
|
555
|
-
# exist within the named_relationships_desc hash tracking named relationships metadata.
|
556
|
-
def register_named_subject(subject)
|
557
|
-
unless named_relationships_desc.has_key?(subject)
|
558
|
-
named_relationships_desc[subject] = {}
|
559
|
-
end
|
560
|
-
end
|
561
|
-
|
562
|
-
# ** EXPERIMENTAL **
|
563
|
-
#
|
564
|
-
# Internal method that adds relationship name and predicate pair to either an outbound (:self)
|
565
|
-
# or inbound (:inbound) relationship types.
|
566
|
-
def register_named_relationship(subject, name, predicate, opts)
|
567
|
-
register_named_subject(subject)
|
568
|
-
opts.merge!({:predicate=>predicate})
|
569
|
-
named_relationships_desc[subject][name] = opts
|
570
|
-
end
|
571
|
-
|
572
|
-
# ** EXPERIMENTAL **
|
573
|
-
#
|
574
|
-
# Used in has_relationship call to create dynamic helper methods to
|
575
|
-
# append and remove objects to and from a named relationship
|
576
|
-
# ====Example
|
577
|
-
# For the following relationship
|
578
|
-
#
|
579
|
-
# has_relationship "audio_records", :has_part, :type=>AudioRecord
|
580
|
-
#
|
581
|
-
# Methods audio_records_append and audio_records_remove are created.
|
582
|
-
# Boths methods take an object that is kind_of? ActiveFedora::Base as a parameter
|
583
|
-
def create_named_relationship_methods(name)
|
584
|
-
append_method_name = "#{name.to_s.downcase}_append"
|
585
|
-
remove_method_name = "#{name.to_s.downcase}_remove"
|
586
|
-
self.send(:define_method,:"#{append_method_name}") {|object| add_named_relationship(name,object)}
|
587
|
-
self.send(:define_method,:"#{remove_method_name}") {|object| remove_named_relationship(name,object)}
|
588
|
-
end
|
589
|
-
|
590
|
-
# ** EXPERIMENTAL **
|
591
|
-
# Similar to +create_named_relationship_methods+ except we are merely creating an alias for outbound portion of bidirectional
|
592
|
-
#
|
593
|
-
# ====Example
|
594
|
-
# has_bidirectional_relationship "members", :has_collection_member, :is_member_of_collection
|
595
|
-
#
|
596
|
-
# Method members_outbound_append and members_outbound_remove added
|
597
|
-
# This method will create members_append which does same thing as members_outbound_append
|
598
|
-
# and will create members_remove which does same thing as members_outbound_remove
|
599
|
-
def create_bidirectional_named_relationship_methods(name,outbound_name)
|
600
|
-
append_method_name = "#{name.to_s.downcase}_append"
|
601
|
-
remove_method_name = "#{name.to_s.downcase}_remove"
|
602
|
-
self.send(:define_method,:"#{append_method_name}") {|object| add_named_relationship(outbound_name,object)}
|
603
|
-
self.send(:define_method,:"#{remove_method_name}") {|object| remove_named_relationship(outbound_name,object)}
|
604
|
-
end
|
605
256
|
|
606
257
|
def create_inbound_relationship_finders(name, predicate, opts = {})
|
607
258
|
class_eval <<-END
|
608
259
|
def #{name}(opts={})
|
609
260
|
opts = {:rows=>25}.merge(opts)
|
610
|
-
|
611
|
-
|
261
|
+
query = self.class.inbound_relationship_query(self.pid,"#{name}")
|
262
|
+
return [] if query.empty?
|
263
|
+
solr_result = SolrService.instance.conn.query(query, :rows=>opts[:rows])
|
612
264
|
if opts[:response_format] == :solr
|
613
265
|
return solr_result
|
614
266
|
else
|
@@ -631,6 +283,9 @@ module ActiveFedora
|
|
631
283
|
def #{name}_from_solr
|
632
284
|
#{name}(:response_format => :load_from_solr)
|
633
285
|
end
|
286
|
+
def #{name}_query
|
287
|
+
relationship_query("#{name}")
|
288
|
+
end
|
634
289
|
END
|
635
290
|
end
|
636
291
|
|
@@ -643,13 +298,19 @@ module ActiveFedora
|
|
643
298
|
id_array << rel.gsub("info:fedora/", "")
|
644
299
|
end
|
645
300
|
end
|
646
|
-
if opts[:response_format] == :id_array
|
301
|
+
if opts[:response_format] == :id_array && !self.class.relationship_has_solr_filter_query?(:self,"#{name}")
|
647
302
|
return id_array
|
648
303
|
else
|
649
|
-
query =
|
304
|
+
query = self.class.outbound_relationship_query("#{name}",id_array)
|
650
305
|
solr_result = SolrService.instance.conn.query(query)
|
651
306
|
if opts[:response_format] == :solr
|
652
307
|
return solr_result
|
308
|
+
elsif opts[:response_format] == :id_array
|
309
|
+
id_array = []
|
310
|
+
solr_result.hits.each do |hit|
|
311
|
+
id_array << hit[SOLR_DOCUMENT_ID]
|
312
|
+
end
|
313
|
+
return id_array
|
653
314
|
elsif opts[:response_format] == :load_from_solr || self.load_from_solr
|
654
315
|
return ActiveFedora::SolrService.reify_solr_results(solr_result,{:load_from_solr=>true})
|
655
316
|
else
|
@@ -663,6 +324,9 @@ module ActiveFedora
|
|
663
324
|
def #{name}_from_solr
|
664
325
|
#{name}(:response_format => :load_from_solr)
|
665
326
|
end
|
327
|
+
def #{name}_query
|
328
|
+
relationship_query("#{name}")
|
329
|
+
end
|
666
330
|
END
|
667
331
|
end
|
668
332
|
|
@@ -680,18 +344,21 @@ module ActiveFedora
|
|
680
344
|
has_relationship(inbound_method_name, inbound_predicate, opts.merge!(:inbound=>true))
|
681
345
|
|
682
346
|
#create methods that mirror the outbound append and remove with our bidirectional name, assume just add and remove locally
|
683
|
-
|
347
|
+
create_bidirectional_relationship_name_methods(name,outbound_method_name)
|
684
348
|
|
685
349
|
class_eval <<-END
|
686
350
|
def #{name}(opts={})
|
687
351
|
opts = {:rows=>25}.merge(opts)
|
688
352
|
if opts[:response_format] == :solr || opts[:response_format] == :load_from_solr
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
353
|
+
outbound_id_array = []
|
354
|
+
predicate = outbound_relationship_predicates["#{name}_outbound"]
|
355
|
+
if !outbound_relationships[predicate].nil?
|
356
|
+
outbound_relationships[predicate].each do |rel|
|
357
|
+
outbound_id_array << rel.gsub("info:fedora/", "")
|
358
|
+
end
|
359
|
+
end
|
360
|
+
#outbound_id_array = #{outbound_method_name}(:response_format=>:id_array)
|
361
|
+
query = self.class.bidirectional_relationship_query(self.pid,"#{name}",outbound_id_array)
|
695
362
|
solr_result = SolrService.instance.conn.query(query, :rows=>opts[:rows])
|
696
363
|
|
697
364
|
if opts[:response_format] == :solr
|
@@ -712,6 +379,9 @@ module ActiveFedora
|
|
712
379
|
def #{name}_from_solr
|
713
380
|
#{name}(:response_format => :load_from_solr)
|
714
381
|
end
|
382
|
+
def #{name}_query
|
383
|
+
relationship_query("#{name}")
|
384
|
+
end
|
715
385
|
END
|
716
386
|
end
|
717
387
|
|
@@ -735,7 +405,7 @@ module ActiveFedora
|
|
735
405
|
relationships[subject][predicate] = []
|
736
406
|
end
|
737
407
|
end
|
738
|
-
|
408
|
+
|
739
409
|
#alias_method :register_target, :register_object
|
740
410
|
|
741
411
|
# Creates a RELS-EXT datastream for insertion into a Fedora Object
|