active-fedora 6.8.0 → 7.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +15 -5
  4. data/CONTRIBUTING.md +2 -0
  5. data/Gemfile +0 -2
  6. data/History.txt +2 -32
  7. data/README.md +143 -0
  8. data/Rakefile +5 -7
  9. data/active-fedora.gemspec +9 -9
  10. data/gemfiles/rails3.gemfile +11 -0
  11. data/gemfiles/rails4.gemfile +10 -0
  12. data/lib/active_fedora.rb +31 -4
  13. data/lib/active_fedora/association_relation.rb +18 -0
  14. data/lib/active_fedora/associations.rb +38 -171
  15. data/lib/active_fedora/associations/association.rb +163 -0
  16. data/lib/active_fedora/associations/association_scope.rb +39 -0
  17. data/lib/active_fedora/associations/belongs_to_association.rb +47 -25
  18. data/lib/active_fedora/associations/builder/association.rb +55 -0
  19. data/lib/active_fedora/associations/builder/belongs_to.rb +100 -0
  20. data/lib/active_fedora/associations/builder/collection_association.rb +56 -0
  21. data/lib/active_fedora/associations/builder/has_and_belongs_to_many.rb +30 -0
  22. data/lib/active_fedora/associations/builder/has_many.rb +63 -0
  23. data/lib/active_fedora/associations/builder/singular_association.rb +32 -0
  24. data/lib/active_fedora/associations/{association_collection.rb → collection_association.rb} +203 -53
  25. data/lib/active_fedora/associations/collection_proxy.rb +862 -0
  26. data/lib/active_fedora/associations/has_and_belongs_to_many_association.rb +35 -25
  27. data/lib/active_fedora/associations/has_many_association.rb +36 -11
  28. data/lib/active_fedora/associations/singular_association.rb +62 -0
  29. data/lib/active_fedora/attributes.rb +43 -139
  30. data/lib/active_fedora/autosave_association.rb +317 -0
  31. data/lib/active_fedora/base.rb +10 -327
  32. data/lib/active_fedora/callbacks.rb +1 -3
  33. data/lib/active_fedora/content_model.rb +16 -0
  34. data/lib/active_fedora/core.rb +151 -0
  35. data/lib/active_fedora/datastream_attribute.rb +76 -0
  36. data/lib/active_fedora/datastream_hash.rb +8 -13
  37. data/lib/active_fedora/datastreams.rb +39 -26
  38. data/lib/active_fedora/digital_object.rb +2 -2
  39. data/lib/active_fedora/fedora_attributes.rb +45 -0
  40. data/lib/active_fedora/fixture_loader.rb +1 -1
  41. data/lib/active_fedora/indexing.rb +6 -1
  42. data/lib/active_fedora/model.rb +0 -17
  43. data/lib/active_fedora/nested_attributes.rb +2 -2
  44. data/lib/active_fedora/null_relation.rb +7 -0
  45. data/lib/active_fedora/om_datastream.rb +3 -4
  46. data/lib/active_fedora/persistence.rb +41 -29
  47. data/lib/active_fedora/querying.rb +2 -163
  48. data/lib/active_fedora/rdf.rb +1 -0
  49. data/lib/active_fedora/rdf/indexing.rb +67 -0
  50. data/lib/active_fedora/rdf_datastream.rb +2 -50
  51. data/lib/active_fedora/rdf_node.rb +12 -7
  52. data/lib/active_fedora/rdf_node/term_proxy.rb +30 -21
  53. data/lib/active_fedora/rdfxml_rdf_datastream.rb +1 -1
  54. data/lib/active_fedora/reflection.rb +163 -20
  55. data/lib/active_fedora/relation.rb +33 -130
  56. data/lib/active_fedora/relation/calculations.rb +19 -0
  57. data/lib/active_fedora/relation/delegation.rb +22 -0
  58. data/lib/active_fedora/relation/finder_methods.rb +247 -0
  59. data/lib/active_fedora/relation/merger.rb +22 -0
  60. data/lib/active_fedora/relation/query_methods.rb +58 -0
  61. data/lib/active_fedora/relation/spawn_methods.rb +46 -0
  62. data/lib/active_fedora/relationship_graph.rb +0 -2
  63. data/lib/active_fedora/rels_ext_datastream.rb +1 -4
  64. data/lib/active_fedora/rubydora_connection.rb +1 -1
  65. data/lib/active_fedora/scoping.rb +20 -0
  66. data/lib/active_fedora/scoping/default.rb +38 -0
  67. data/lib/active_fedora/scoping/named.rb +32 -0
  68. data/lib/active_fedora/semantic_node.rb +54 -106
  69. data/lib/active_fedora/serialization.rb +19 -0
  70. data/lib/active_fedora/sharding.rb +58 -0
  71. data/lib/active_fedora/solr_digital_object.rb +15 -5
  72. data/lib/active_fedora/solr_instance_loader.rb +1 -1
  73. data/lib/active_fedora/solr_service.rb +1 -1
  74. data/lib/active_fedora/unsaved_digital_object.rb +6 -4
  75. data/lib/active_fedora/version.rb +1 -1
  76. data/lib/tasks/active_fedora.rake +3 -0
  77. data/lib/tasks/active_fedora_dev.rake +6 -5
  78. data/spec/config_helper.rb +14 -14
  79. data/spec/integration/associations_spec.rb +168 -455
  80. data/spec/integration/attributes_spec.rb +12 -11
  81. data/spec/integration/auditable_spec.rb +11 -11
  82. data/spec/integration/autosave_association_spec.rb +25 -0
  83. data/spec/integration/base_spec.rb +163 -163
  84. data/spec/integration/belongs_to_association_spec.rb +166 -0
  85. data/spec/integration/bug_spec.rb +7 -7
  86. data/spec/integration/collection_association_spec.rb +58 -0
  87. data/spec/integration/complex_rdf_datastream_spec.rb +88 -88
  88. data/spec/integration/datastream_collections_spec.rb +69 -69
  89. data/spec/integration/datastream_spec.rb +43 -43
  90. data/spec/integration/datastreams_spec.rb +63 -63
  91. data/spec/integration/delete_all_spec.rb +46 -39
  92. data/spec/integration/fedora_solr_sync_spec.rb +5 -5
  93. data/spec/integration/field_to_solr_name_spec.rb +34 -0
  94. data/spec/integration/full_featured_model_spec.rb +100 -101
  95. data/spec/integration/has_and_belongs_to_many_associations_spec.rb +341 -0
  96. data/spec/integration/has_many_associations_spec.rb +172 -24
  97. data/spec/integration/json_serialization_spec.rb +31 -0
  98. data/spec/integration/load_from_solr_spec.rb +48 -0
  99. data/spec/integration/model_spec.rb +35 -40
  100. data/spec/integration/nested_attribute_spec.rb +42 -43
  101. data/spec/integration/ntriples_datastream_spec.rb +131 -113
  102. data/spec/integration/om_datastream_spec.rb +67 -67
  103. data/spec/integration/persistence_spec.rb +7 -7
  104. data/spec/integration/rdf_nested_attributes_spec.rb +56 -56
  105. data/spec/integration/relation_delegation_spec.rb +26 -25
  106. data/spec/integration/relation_spec.rb +42 -0
  107. data/spec/integration/rels_ext_datastream_spec.rb +20 -20
  108. data/spec/integration/scoped_query_spec.rb +61 -51
  109. data/spec/integration/solr_instance_loader_spec.rb +5 -5
  110. data/spec/integration/solr_service_spec.rb +46 -46
  111. data/spec/samples/hydra-mods_article_datastream.rb +334 -334
  112. data/spec/samples/hydra-rights_metadata_datastream.rb +57 -57
  113. data/spec/samples/marpa-dc_datastream.rb +17 -17
  114. data/spec/samples/models/audio_record.rb +16 -16
  115. data/spec/samples/models/image.rb +2 -2
  116. data/spec/samples/models/mods_article.rb +5 -5
  117. data/spec/samples/models/oral_history.rb +18 -18
  118. data/spec/samples/models/seminar.rb +24 -24
  119. data/spec/samples/models/seminar_audio_file.rb +17 -17
  120. data/spec/samples/oral_history_sample_model.rb +21 -21
  121. data/spec/samples/special_thing.rb +14 -14
  122. data/spec/spec_helper.rb +11 -7
  123. data/spec/support/an_active_model.rb +2 -8
  124. data/spec/support/freeze_mocks.rb +12 -0
  125. data/spec/support/mock_fedora.rb +17 -16
  126. data/spec/unit/active_fedora_spec.rb +58 -60
  127. data/spec/unit/attributes_spec.rb +314 -0
  128. data/spec/unit/base_active_model_spec.rb +28 -27
  129. data/spec/unit/base_cma_spec.rb +5 -5
  130. data/spec/unit/base_datastream_management_spec.rb +27 -27
  131. data/spec/unit/base_extra_spec.rb +76 -48
  132. data/spec/unit/base_spec.rb +277 -348
  133. data/spec/unit/callback_spec.rb +18 -19
  134. data/spec/unit/code_configurator_spec.rb +17 -17
  135. data/spec/unit/config_spec.rb +8 -16
  136. data/spec/unit/content_model_spec.rb +79 -60
  137. data/spec/unit/datastream_collections_spec.rb +229 -229
  138. data/spec/unit/datastream_spec.rb +51 -63
  139. data/spec/unit/datastreams_spec.rb +87 -87
  140. data/spec/unit/file_configurator_spec.rb +217 -217
  141. data/spec/unit/has_and_belongs_to_many_collection_spec.rb +44 -25
  142. data/spec/unit/has_many_collection_spec.rb +26 -8
  143. data/spec/unit/inheritance_spec.rb +13 -12
  144. data/spec/unit/model_spec.rb +39 -45
  145. data/spec/unit/nom_datastream_spec.rb +15 -15
  146. data/spec/unit/ntriples_datastream_spec.rb +123 -118
  147. data/spec/unit/om_datastream_spec.rb +227 -233
  148. data/spec/unit/persistence_spec.rb +34 -15
  149. data/spec/unit/predicates_spec.rb +73 -73
  150. data/spec/unit/property_spec.rb +17 -9
  151. data/spec/unit/qualified_dublin_core_datastream_spec.rb +33 -33
  152. data/spec/unit/query_spec.rb +222 -198
  153. data/spec/unit/rdf_datastream_spec.rb +21 -28
  154. data/spec/unit/rdf_list_nested_attributes_spec.rb +34 -34
  155. data/spec/unit/rdf_list_spec.rb +65 -64
  156. data/spec/unit/rdf_node_spec.rb +7 -7
  157. data/spec/unit/rdf_xml_writer_spec.rb +10 -10
  158. data/spec/unit/rdfxml_rdf_datastream_spec.rb +27 -27
  159. data/spec/unit/relationship_graph_spec.rb +51 -51
  160. data/spec/unit/rels_ext_datastream_spec.rb +68 -74
  161. data/spec/unit/rspec_matchers/belong_to_associated_active_fedora_object_matcher_spec.rb +15 -15
  162. data/spec/unit/rspec_matchers/have_many_associated_active_fedora_objects_matcher_spec.rb +15 -15
  163. data/spec/unit/rspec_matchers/have_predicate_matcher_spec.rb +15 -15
  164. data/spec/unit/rspec_matchers/match_fedora_datastream_matcher_spec.rb +12 -12
  165. data/spec/unit/rubydora_connection_spec.rb +5 -5
  166. data/spec/unit/semantic_node_spec.rb +48 -107
  167. data/spec/unit/serializers_spec.rb +4 -4
  168. data/spec/unit/service_definitions_spec.rb +26 -26
  169. data/spec/unit/simple_datastream_spec.rb +17 -17
  170. data/spec/unit/solr_config_options_spec.rb +29 -28
  171. data/spec/unit/solr_digital_object_spec.rb +17 -25
  172. data/spec/unit/solr_service_spec.rb +95 -82
  173. data/spec/unit/unsaved_digital_object_spec.rb +24 -23
  174. data/spec/unit/validations_spec.rb +21 -21
  175. metadata +110 -159
  176. data/.rspec +0 -1
  177. data/.rubocop.yml +0 -1
  178. data/.rubocop_todo.yml +0 -938
  179. data/CONSOLE_GETTING_STARTED.textile +0 -1
  180. data/NOKOGIRI_DATASTREAMS.textile +0 -1
  181. data/README.textile +0 -116
  182. data/lib/active_fedora/associations/association_proxy.rb +0 -178
  183. data/lib/active_fedora/delegating.rb +0 -72
  184. data/lib/active_fedora/nokogiri_datastream.rb +0 -11
  185. data/spec/integration/delegating_spec.rb +0 -59
  186. data/spec/rails3_test_app/.gitignore +0 -4
  187. data/spec/rails3_test_app/.rspec +0 -1
  188. data/spec/rails3_test_app/Gemfile +0 -40
  189. data/spec/rails3_test_app/Rakefile +0 -7
  190. data/spec/rails3_test_app/app/controllers/application_controller.rb +0 -3
  191. data/spec/rails3_test_app/app/helpers/application_helper.rb +0 -2
  192. data/spec/rails3_test_app/app/views/layouts/application.html.erb +0 -14
  193. data/spec/rails3_test_app/config.ru +0 -4
  194. data/spec/rails3_test_app/config/application.rb +0 -42
  195. data/spec/rails3_test_app/config/boot.rb +0 -6
  196. data/spec/rails3_test_app/config/database.yml +0 -22
  197. data/spec/rails3_test_app/config/environment.rb +0 -5
  198. data/spec/rails3_test_app/config/environments/development.rb +0 -25
  199. data/spec/rails3_test_app/config/environments/production.rb +0 -49
  200. data/spec/rails3_test_app/config/environments/test.rb +0 -35
  201. data/spec/rails3_test_app/config/initializers/backtrace_silencers.rb +0 -7
  202. data/spec/rails3_test_app/config/initializers/inflections.rb +0 -10
  203. data/spec/rails3_test_app/config/initializers/mime_types.rb +0 -5
  204. data/spec/rails3_test_app/config/initializers/secret_token.rb +0 -7
  205. data/spec/rails3_test_app/config/initializers/session_store.rb +0 -8
  206. data/spec/rails3_test_app/config/locales/en.yml +0 -5
  207. data/spec/rails3_test_app/config/routes.rb +0 -58
  208. data/spec/rails3_test_app/db/seeds.rb +0 -7
  209. data/spec/rails3_test_app/run_tests +0 -3
  210. data/spec/rails3_test_app/script/rails +0 -6
  211. data/spec/rails3_test_app/spec/spec_helper.rb +0 -27
  212. data/spec/rails3_test_app/spec/unit/rails_3_init.rb +0 -15
  213. data/spec/unit/association_proxy_spec.rb +0 -12
  214. data/spec/unit/base_delegate_spec.rb +0 -197
  215. data/spec/unit/base_delegate_to_spec.rb +0 -73
@@ -35,7 +35,7 @@ module ActiveFedora
35
35
  end
36
36
 
37
37
  def self.index(pid)
38
- ActiveFedora::Base.find(pid, cast: true).update_index
38
+ ActiveFedora::Base.find(pid).update_index
39
39
  end
40
40
 
41
41
  def self.import_to_fedora(filename, pid='0')
@@ -2,6 +2,11 @@ module ActiveFedora
2
2
  module Indexing
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ included do
6
+ class_attribute :profile_solr_name
7
+ self.profile_solr_name = ActiveFedora::SolrService.solr_name("object_profile", :displayable)
8
+ end
9
+
5
10
  # Return a Hash representation of this object where keys in the hash are appropriate Solr field names.
6
11
  # @param [Hash] solr_doc (optional) Hash to insert the fields into
7
12
  # @param [Hash] opts (optional)
@@ -93,7 +98,7 @@ module ActiveFedora
93
98
  connections.each do |conn|
94
99
  conn.search(query) do |object|
95
100
  next if object.pid.start_with?('fedora-system:')
96
- ActiveFedora::Base.find(object.pid, :cast=>true).update_index
101
+ ActiveFedora::Base.find(object.pid).update_index
97
102
  end
98
103
  end
99
104
  end
@@ -34,23 +34,6 @@ module ActiveFedora
34
34
  result
35
35
  end
36
36
 
37
- # Returns a suitable uri object for :has_model
38
- # Should reverse Model#from_class_uri
39
- def to_class_uri(attrs = {})
40
- if self.respond_to? :pid_suffix
41
- pid_suffix = self.pid_suffix
42
- else
43
- pid_suffix = attrs.fetch(:pid_suffix, ContentModel::CMODEL_PID_SUFFIX)
44
- end
45
- if self.respond_to? :pid_namespace
46
- namespace = self.pid_namespace
47
- else
48
- namespace = attrs.fetch(:namespace, ContentModel::CMODEL_NAMESPACE)
49
- end
50
- "info:fedora/#{namespace}:#{ContentModel.sanitized_class_name(self)}#{pid_suffix}"
51
- end
52
-
53
-
54
37
  private
55
38
 
56
39
  def self.class_exists?(class_name)
@@ -118,7 +118,7 @@ module ActiveFedora
118
118
  if attributes_collection.is_a? Hash
119
119
  keys = attributes_collection.keys
120
120
  attributes_collection = if keys.include?('id') || keys.include?(:id)
121
- Array.wrap(attributes_collection)
121
+ Array(attributes_collection)
122
122
  else
123
123
  attributes_collection.sort_by { |i, _| i.to_i }.map { |_, attributes| attributes }
124
124
  end
@@ -130,7 +130,7 @@ module ActiveFedora
130
130
  association.to_a
131
131
  else
132
132
  attribute_ids = attributes_collection.map {|a| a['id'] || a[:id] }.compact
133
- attribute_ids.present? ? association.select{ |x| attribute_ids.include?(x.pid)} : []
133
+ attribute_ids.present? ? association.to_a.select{ |x| attribute_ids.include?(x.pid)} : []
134
134
  end
135
135
 
136
136
  attributes_collection.each do |attributes|
@@ -0,0 +1,7 @@
1
+ module ActiveFedora
2
+ module NullRelation # :nodoc:
3
+ def exists?(_id = false)
4
+ false
5
+ end
6
+ end
7
+ end
@@ -24,7 +24,7 @@ module ActiveFedora
24
24
 
25
25
  # Create an instance of this class based on xml content
26
26
  # @param [String, File, Nokogiri::XML::Node] xml the xml content to build from
27
- # @param [ActiveFedora::NokogiriDatastream] tmpl the Datastream object that you are building @default a new instance of this class
27
+ # @param [ActiveFedora::OmDatastream] tmpl the Datastream object that you are building @default a new instance of this class
28
28
  # Careful! If you call this from a constructor, be sure to provide something 'ie. self' as the @tmpl. Otherwise, you will get an infinite loop!
29
29
  def self.from_xml(xml, tmpl=nil)
30
30
  tmpl = self.new if tmpl.nil? ## This path is used only for unit testing (e.g. MarpaDCDatastream.from_xml(fixture("data.xml")) )
@@ -70,7 +70,7 @@ module ActiveFedora
70
70
  when String
71
71
  self.content=new_xml
72
72
  else
73
- raise TypeError, "You passed a #{new_xml.class} into the ng_xml of the #{self.dsid} datastream. NokogiriDatastream.ng_xml= only accepts Nokogiri::XML::Document, Nokogiri::XML::Element, Nokogiri::XML::Node, or raw XML (String) as inputs."
73
+ raise TypeError, "You passed a #{new_xml.class} into the ng_xml of the #{self.dsid} datastream. OmDatastream.ng_xml= only accepts Nokogiri::XML::Document, Nokogiri::XML::Element, Nokogiri::XML::Node, or raw XML (String) as inputs."
74
74
  end
75
75
  end
76
76
 
@@ -126,7 +126,6 @@ module ActiveFedora
126
126
 
127
127
  def content_changed?
128
128
  return false if !xml_loaded
129
- return true if changed_attributes.any?
130
129
  super
131
130
  end
132
131
 
@@ -357,7 +356,7 @@ module ActiveFedora
357
356
  # </mods>
358
357
  def update_indexed_attributes(params={}, opts={})
359
358
  if self.class.terminology.nil?
360
- raise "No terminology is set for this NokogiriDatastream class. Cannot perform update_indexed_attributes"
359
+ raise "No terminology is set for this OmDatastream class. Cannot perform update_indexed_attributes"
361
360
  end
362
361
  # remove any fields from params that this datastream doesn't recognize
363
362
  # make sure to make a copy of params so not to modify hash that might be passed to other methods
@@ -2,19 +2,41 @@ module ActiveFedora
2
2
  # = Active Fedora Persistence
3
3
  module Persistence
4
4
  extend ActiveSupport::Concern
5
+ extend Deprecation
6
+ self.deprecation_horizon = 'active-fedora version 8.0.0'
7
+
8
+
9
+ def new?
10
+ new_record?
11
+ end
12
+ deprecation_deprecate :new?
13
+
14
+ # Has this object been saved?
15
+ def new_object?
16
+ new_record?
17
+ end
18
+ deprecation_deprecate :new_object?
19
+
20
+
21
+ ## Required by associations
22
+ def new_record?
23
+ inner_object.new?
24
+ end
25
+
26
+ def persisted?
27
+ !(new_record? || destroyed?)
28
+ end
29
+
30
+ # Returns true if this object has been destroyed, otherwise returns false.
31
+ def destroyed?
32
+ @destroyed
33
+ end
5
34
 
6
35
  #Saves a Base object, and any dirty datastreams, then updates
7
36
  #the Solr index for this object.
8
37
  def save(*)
9
38
  # If it's a new object, set the conformsTo relationship for Fedora CMA
10
- if new_record?
11
- result = create
12
- update_index if create_needs_index?
13
- else
14
- result = update_record
15
- update_index if update_needs_index?
16
- end
17
- return result
39
+ new_record? ? create : update_record
18
40
  end
19
41
 
20
42
  def save!(*)
@@ -41,29 +63,16 @@ module ActiveFedora
41
63
  # inner_object.load_attributes_from_fedora
42
64
  end
43
65
 
44
- def dependent_objects
45
- # Loop over all the inbound associations (has_many reflections)
46
- results = {}
47
- reflections.each_pair do |name, reflection|
48
- if reflection.macro == :has_many
49
- results[reflection.options[:property]] = send(name)
50
- end
51
- end
52
- results.merge (inbound_relationships(:object) )
53
- end
54
-
55
66
  #Deletes a Base object, also deletes the info indexed in Solr, and
56
67
  #the underlying inner_object. If this object is held in any relationships (ie inbound relationships
57
68
  #outside of this object it will remove it from those items rels-ext as well
58
69
  def delete
59
- dependent_objects.each_pair do |predicate, objects|
60
- objects.each do |obj|
61
- if obj.respond_to?(:remove_relationship)
62
- obj.remove_relationship(predicate,self)
63
- obj.save
64
- end
70
+ reflections.each_pair do |name, reflection|
71
+ if reflection.macro == :has_many
72
+ association(name).delete_all
65
73
  end
66
74
  end
75
+
67
76
 
68
77
  #Fedora::Repository.instance.delete(@inner_object)
69
78
  pid = self.pid ## cache so it's still available after delete
@@ -77,6 +86,8 @@ module ActiveFedora
77
86
  solr.delete_by_id(pid)
78
87
  solr.commit
79
88
  end
89
+ @destroyed = true
90
+ freeze
80
91
  end
81
92
 
82
93
  def destroy
@@ -138,11 +149,11 @@ module ActiveFedora
138
149
  def create
139
150
  assign_pid
140
151
  assert_content_model
141
- persist
152
+ persist(create_needs_index?)
142
153
  end
143
154
 
144
155
  def update_record
145
- persist
156
+ persist(update_needs_index?)
146
157
  end
147
158
 
148
159
  # replace the unsaved digital object with a saved digital object
@@ -150,15 +161,16 @@ module ActiveFedora
150
161
  @inner_object = @inner_object.save
151
162
  end
152
163
 
153
- def persist
164
+ def persist(should_update_index)
165
+ serialize_datastreams
154
166
  result = @inner_object.save
155
-
156
167
  ### Rubydora re-inits the datastreams after a save, so ensure our copy stays in synch
157
168
  @inner_object.datastreams.each do |dsid, ds|
158
169
  datastreams[dsid] = ds
159
170
  ds.model = self if ds.kind_of? RelsExtDatastream
160
171
  end
161
172
  refresh
173
+ update_index if should_update_index
162
174
  return !!result
163
175
  end
164
176
  end
@@ -1,180 +1,19 @@
1
1
  module ActiveFedora
2
2
  module Querying
3
- delegate :find, :first, :where, :limit, :order, :all, :delete_all, :destroy_all, :count, :last, :to=>:relation
3
+ delegate :find, :first, :exists?, :where, :limit, :order, :delete_all,
4
+ :destroy_all, :count, :last, :find_with_conditions, :find_in_batches, :find_each, :to=>:all
4
5
 
5
6
  def self.extended(base)
6
7
  base.class_attribute :solr_query_handler
7
8
  base.solr_query_handler = 'standard'
8
9
  end
9
-
10
-
11
- def relation
12
- Relation.new(self)
13
- end
14
-
15
- # Yields each batch of solr records that was found by the find +options+ as
16
- # an array. The size of each batch is set by the <tt>:batch_size</tt>
17
- # option; the default is 1000.
18
- #
19
- # Returns a solr result matching the supplied conditions
20
- # @param[Hash] conditions solr conditions to match
21
- # @param[Hash] options
22
- # @option opts [Array] :sort a list of fields to sort by
23
- # @option opts [Array] :rows number of rows to return
24
- #
25
- # @example
26
- # Person.find_in_batches('age_t'=>'21', {:batch_size=>50}) do |group|
27
- # group.each { |person| puts person['name_t'] }
28
- # end
29
-
30
- def find_in_batches conditions, opts={}
31
- opts[:q] = create_query(conditions)
32
- opts[:qt] = solr_query_handler
33
- #set default sort to created date ascending
34
- unless opts[:sort].present?
35
- opts[:sort]= default_sort_params
36
- end
37
-
38
- batch_size = opts.delete(:batch_size) || 1000
39
-
40
- counter = 0
41
- begin
42
- counter += 1
43
- response = ActiveFedora::SolrService.instance.conn.paginate counter, batch_size, "select", :params => opts
44
- docs = response["response"]["docs"]
45
- yield docs
46
- end while docs.has_next?
47
- end
48
-
49
- def calculate(calculation, conditions, opts={})
50
- SolrService.query(create_query(conditions), :raw=>true, :rows=>0)['response']['numFound']
51
- end
52
10
 
53
11
  def default_sort_params
54
12
  [ActiveFedora::SolrService.solr_name(:system_create, :stored_sortable, type: :date)+' asc']
55
13
  end
56
14
 
57
- # Yields the found ActiveFedora::Base object to the passed block
58
- #
59
- # @param [Hash] conditions the conditions for the solr search to match
60
- # @param [Hash] opts
61
- # @option opts [Boolean] :cast when true, examine the model and cast it to the first known cModel
62
- def find_each( conditions={}, opts={})
63
- cast = opts.delete(:cast)
64
- find_in_batches(conditions, opts.merge({:fl=>SOLR_DOCUMENT_ID})) do |group|
65
- group.each do |hit|
66
- begin
67
- yield(find_one(hit[SOLR_DOCUMENT_ID], cast))
68
- rescue ActiveFedora::ObjectNotFoundError
69
- logger.error "When trying to find_each #{hit[SOLR_DOCUMENT_ID]}, encountered an ObjectNotFoundError. Solr may be out of sync with Fedora"
70
- end
71
- end
72
- end
73
- end
74
-
75
-
76
- # Returns true if the pid exists in the repository
77
- # @param[String] pid
78
- # @return[boolean]
79
- def exists?(pid)
80
- return false if pid.nil? || pid.empty?
81
- !!DigitalObject.find(self, pid)
82
- rescue ActiveFedora::ObjectNotFoundError
83
- false
84
- end
85
-
86
- # Returns a solr result matching the supplied conditions
87
- # @param[Hash,String] conditions can either be specified as a string, or
88
- # hash representing the query part of an solr statement. If a hash is
89
- # provided, this method will generate conditions based simple equality
90
- # combined using the boolean AND operator.
91
- # @param[Hash] options
92
- # @option opts [Array] :sort a list of fields to sort by
93
- # @option opts [Array] :rows number of rows to return
94
- def find_with_conditions(conditions, opts={})
95
- #set default sort to created date ascending
96
- unless opts.include?(:sort)
97
- opts[:sort]=default_sort_params
98
- end
99
- SolrService.query(create_query(conditions), opts)
100
- end
101
-
102
15
  def quote_for_solr(value)
103
16
  '"' + value.gsub(/(:)/, '\\:').gsub(/(\/)/, '\\/').gsub(/"/, '\\"') + '"'
104
17
  end
105
-
106
-
107
- extend Deprecation
108
- self.deprecation_horizon = 'active-fedora 7.0.0'
109
-
110
- # Retrieve the Fedora object with the given pid, explore the returned object, determine its model
111
- # using #{ActiveFedora::ContentModel.known_models_for} and cast to that class.
112
- # Raises a ObjectNotFoundError if the object is not found.
113
- # @param [String] pid of the object to load
114
- # @param [Boolean] cast when true, cast the found object to the class of the first known model defined in it's RELS-EXT
115
- #
116
- # @example because the object hydra:dataset1 asserts it is a Dataset (hasModel info:fedora/afmodel:Dataset), return a Dataset object (not a Book).
117
- # Book.find_one("hydra:dataset1")
118
- def find_one(pid, cast=nil)
119
- if self == ActiveFedora::Base && cast.nil?
120
- cast = false
121
- Deprecation.warn(Querying, "find_one's cast parameter will default to true in ActiveFedora 7.0.0. If you want to maintain your existing behavior set `false' as the second parameter.", caller)
122
- end
123
- inner = DigitalObject.find(self, pid)
124
- af_base = self.allocate.init_with_object(inner)
125
- cast ? af_base.adapt_to_cmodel : af_base
126
- end
127
-
128
- private
129
-
130
- # Returns a solr query for the supplied conditions
131
- # @param[Hash] conditions solr conditions to match
132
- def create_query(conditions)
133
- conditions.kind_of?(Hash) ? create_query_from_hash(conditions) : create_query_from_string(conditions)
134
- end
135
-
136
- def create_query_from_hash(conditions)
137
- clauses = search_model_clause ? [search_model_clause] : []
138
- conditions.each_pair do |key,value|
139
- clauses << condition_to_clauses(key, value)
140
- end
141
- return "*:*" if clauses.empty?
142
- clauses.compact.join(" AND ")
143
- end
144
-
145
- def condition_to_clauses(key, value)
146
- unless value.nil?
147
- # if the key is a property name, turn it into a solr field
148
- if self.defined_attributes.key?(key)
149
- # TODO Check to see if `key' is a possible solr field for this class, if it isn't try :searchable instead
150
- key = ActiveFedora::SolrService.solr_name(key, :stored_searchable, type: :string)
151
- end
152
- if value.is_a? Array
153
- value.map { |val| "#{key}:#{quote_for_solr(val)}" }
154
- elsif value.is_a? String and value.empty?
155
- "-#{key}:['' TO *]"
156
- else
157
- key = SOLR_DOCUMENT_ID if (key === :id || key === :pid)
158
- escaped_value = quote_for_solr(value)
159
- key.to_s.eql?(SOLR_DOCUMENT_ID) ? "#{key}:#{escaped_value}" : "#{key}:#{escaped_value}"
160
- end
161
- end
162
- end
163
-
164
- def create_query_from_string(conditions)
165
- model_clause = search_model_clause
166
- if model_clause
167
- conditions ? "#{model_clause} AND (#{conditions})" : model_clause
168
- else
169
- conditions
170
- end
171
- end
172
-
173
- # Return the solr clause that queries for this type of class
174
- def search_model_clause
175
- unless self == ActiveFedora::Base
176
- return ActiveFedora::SolrService.construct_query_for_rel(:has_model => self.to_class_uri)
177
- end
178
- end
179
18
  end
180
19
  end
@@ -3,5 +3,6 @@ module ActiveFedora
3
3
  extend ActiveSupport::Autoload
4
4
  autoload :NestedAttributes
5
5
  autoload :NodeConfig
6
+ autoload :Indexing
6
7
  end
7
8
  end
@@ -0,0 +1,67 @@
1
+ module ActiveFedora
2
+ module Rdf
3
+ module Indexing
4
+ extend ActiveSupport::Concern
5
+
6
+ def prefix(name)
7
+ self.class.prefix(dsid, name)
8
+ end
9
+
10
+ def to_solr(solr_doc = Hash.new) # :nodoc:
11
+ fields.each do |field_key, field_info|
12
+ values = get_values(rdf_subject, field_key)
13
+ Array(values).each do |val|
14
+ val = val.to_s if val.kind_of? RDF::URI
15
+ Solrizer.insert_field(solr_doc, prefix(field_key), val, *field_info[:behaviors])
16
+ end
17
+ end
18
+ solr_doc
19
+ end
20
+
21
+
22
+ module ClassMethods
23
+ def prefix(dsid, name)
24
+ "#{dsid.underscore}__#{name}".to_sym
25
+ end
26
+
27
+ # Gives the primary solr name for a column. If there is more than one indexer on the field definition, it gives the first
28
+ def primary_solr_name(dsid, field)
29
+ config = config_for_term_or_uri(field)
30
+ if behaviors = config.behaviors
31
+ ActiveFedora::SolrService.solr_name(prefix(dsid, field), behaviors.first, type: config.type)
32
+ end
33
+ end
34
+
35
+ # Gives the datatype for a column.
36
+ def type(field)
37
+ config_for_term_or_uri(field).type
38
+ end
39
+ end
40
+
41
+ private
42
+ # returns a Hash, e.g.: {field => {:values => [], :type => :something, :behaviors => []}, ...}
43
+ def fields
44
+ field_map = {}.with_indifferent_access
45
+
46
+ rdf_subject = self.rdf_subject
47
+ query = RDF::Query.new do
48
+ pattern [rdf_subject, :predicate, :value]
49
+ end
50
+
51
+ query.execute(graph).each do |solution|
52
+ predicate = solution.predicate
53
+ value = solution.value
54
+
55
+ name, config = self.class.config_for_predicate(predicate)
56
+ next unless config
57
+ type = config.type
58
+ behaviors = config.behaviors
59
+ next unless type and behaviors
60
+ field_map[name] ||= {:values => [], :type => type, :behaviors => behaviors}
61
+ field_map[name][:values] << value.to_s
62
+ end
63
+ field_map
64
+ end
65
+ end
66
+ end
67
+ end