active-fedora 9.9.0 → 9.10.0

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.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +20 -6
  3. data/.travis.yml +2 -6
  4. data/History.txt +146 -0
  5. data/active-fedora.gemspec +11 -8
  6. data/lib/active_fedora/associations/association.rb +44 -14
  7. data/lib/active_fedora/associations/association_scope.rb +6 -2
  8. data/lib/active_fedora/associations/basic_contains_association.rb +2 -3
  9. data/lib/active_fedora/associations/belongs_to_association.rb +23 -14
  10. data/lib/active_fedora/associations/builder/association.rb +95 -41
  11. data/lib/active_fedora/associations/builder/belongs_to.rb +30 -2
  12. data/lib/active_fedora/associations/builder/collection_association.rb +36 -4
  13. data/lib/active_fedora/associations/builder/contains.rb +10 -5
  14. data/lib/active_fedora/associations/builder/directly_contains.rb +6 -8
  15. data/lib/active_fedora/associations/builder/directly_contains_one.rb +27 -20
  16. data/lib/active_fedora/associations/builder/has_and_belongs_to_many.rb +7 -3
  17. data/lib/active_fedora/associations/builder/has_many.rb +8 -45
  18. data/lib/active_fedora/associations/builder/indirectly_contains.rb +6 -8
  19. data/lib/active_fedora/associations/builder/property.rb +6 -12
  20. data/lib/active_fedora/associations/builder/singular_association.rb +3 -5
  21. data/lib/active_fedora/associations/builder/singular_property.rb +5 -4
  22. data/lib/active_fedora/associations/collection_association.rb +22 -26
  23. data/lib/active_fedora/associations/collection_proxy.rb +1 -1
  24. data/lib/active_fedora/associations/contained_finder.rb +1 -2
  25. data/lib/active_fedora/associations/directly_contains_one_association.rb +2 -3
  26. data/lib/active_fedora/associations/has_and_belongs_to_many_association.rb +1 -1
  27. data/lib/active_fedora/associations/has_many_association.rb +24 -5
  28. data/lib/active_fedora/associations/rdf.rb +1 -20
  29. data/lib/active_fedora/associations.rb +1 -1
  30. data/lib/active_fedora/attached_files.rb +2 -2
  31. data/lib/active_fedora/attribute_assignment.rb +55 -0
  32. data/lib/active_fedora/attribute_methods/read.rb +29 -44
  33. data/lib/active_fedora/attribute_methods/write.rb +16 -19
  34. data/lib/active_fedora/attribute_methods.rb +149 -29
  35. data/lib/active_fedora/attributes.rb +6 -18
  36. data/lib/active_fedora/autosave_association.rb +8 -12
  37. data/lib/active_fedora/base.rb +2 -2
  38. data/lib/active_fedora/caching_connection.rb +1 -1
  39. data/lib/active_fedora/callbacks.rb +9 -9
  40. data/lib/active_fedora/common.rb +67 -0
  41. data/lib/active_fedora/core.rb +11 -23
  42. data/lib/active_fedora/default_model_mapper.rb +24 -0
  43. data/lib/active_fedora/errors.rb +42 -0
  44. data/lib/active_fedora/fedora.rb +1 -1
  45. data/lib/active_fedora/file/attributes.rb +9 -10
  46. data/lib/active_fedora/file.rb +19 -56
  47. data/lib/active_fedora/file_persistence.rb +27 -0
  48. data/lib/active_fedora/indexing.rb +15 -9
  49. data/lib/active_fedora/indexing_service.rb +4 -4
  50. data/lib/active_fedora/inheritance.rb +34 -0
  51. data/lib/active_fedora/ldp_resource.rb +1 -0
  52. data/lib/active_fedora/model.rb +18 -16
  53. data/lib/active_fedora/model_classifier.rb +77 -0
  54. data/lib/active_fedora/nested_attributes.rb +145 -18
  55. data/lib/active_fedora/persistence.rb +23 -5
  56. data/lib/active_fedora/predicates.rb +3 -0
  57. data/lib/active_fedora/qualified_dublin_core_datastream.rb +1 -1
  58. data/lib/active_fedora/query_result_builder.rb +12 -28
  59. data/lib/active_fedora/querying.rb +1 -1
  60. data/lib/active_fedora/rdf/datastream_indexing.rb +1 -1
  61. data/lib/active_fedora/reflection.rb +212 -80
  62. data/lib/active_fedora/relation/calculations.rb +1 -5
  63. data/lib/active_fedora/relation/finder_methods.rb +39 -26
  64. data/lib/active_fedora/relation.rb +17 -0
  65. data/lib/active_fedora/scoping/default.rb +117 -0
  66. data/lib/active_fedora/scoping/named.rb +152 -4
  67. data/lib/active_fedora/scoping.rb +85 -2
  68. data/lib/active_fedora/simple_datastream.rb +1 -1
  69. data/lib/active_fedora/solr_hit.rb +71 -0
  70. data/lib/active_fedora/solr_instance_loader.rb +12 -36
  71. data/lib/active_fedora/solr_query_builder.rb +20 -25
  72. data/lib/active_fedora/solr_service.rb +24 -13
  73. data/lib/active_fedora/type/boolean.rb +23 -0
  74. data/lib/active_fedora/type/value.rb +118 -0
  75. data/lib/active_fedora/type.rb +8 -0
  76. data/lib/active_fedora/validations.rb +14 -5
  77. data/lib/active_fedora/version.rb +1 -1
  78. data/lib/active_fedora/versionable.rb +8 -7
  79. data/lib/active_fedora/with_metadata/metadata_node.rb +2 -1
  80. data/lib/active_fedora.rb +35 -15
  81. data/lib/generators/active_fedora/config/fedora/fedora_generator.rb +4 -0
  82. data/lib/generators/active_fedora/config/fedora/templates/.fcrepo_wrapper +3 -0
  83. data/lib/generators/active_fedora/config/solr/solr_generator.rb +4 -0
  84. data/lib/generators/active_fedora/config/solr/templates/.solr_wrapper +5 -0
  85. data/lib/generators/active_fedora/config/solr/templates/solr/config/solrconfig.xml +15 -0
  86. data/solr/config/solrconfig.xml +15 -0
  87. data/spec/config_helper.rb +0 -5
  88. data/spec/integration/associations_spec.rb +5 -5
  89. data/spec/integration/attached_files_spec.rb +4 -4
  90. data/spec/integration/base_spec.rb +4 -4
  91. data/spec/integration/bug_spec.rb +0 -1
  92. data/spec/integration/collection_association_spec.rb +34 -3
  93. data/spec/integration/file_spec.rb +2 -2
  94. data/spec/integration/full_featured_model_spec.rb +4 -4
  95. data/spec/integration/has_and_belongs_to_many_associations_spec.rb +1 -1
  96. data/spec/integration/has_many_associations_spec.rb +30 -1
  97. data/spec/integration/indirect_container_spec.rb +1 -1
  98. data/spec/integration/nested_attribute_spec.rb +6 -0
  99. data/spec/integration/ntriples_datastream_spec.rb +4 -4
  100. data/spec/integration/om_datastream_spec.rb +7 -7
  101. data/spec/integration/persistence_spec.rb +1 -1
  102. data/spec/integration/relation_delegation_spec.rb +1 -1
  103. data/spec/integration/scoped_query_spec.rb +13 -13
  104. data/spec/integration/solr_hit_spec.rb +52 -0
  105. data/spec/samples/hydra-mods_article_datastream.rb +2 -2
  106. data/spec/spec_helper.rb +5 -9
  107. data/spec/unit/active_fedora_spec.rb +0 -26
  108. data/spec/unit/attributes_spec.rb +13 -7
  109. data/spec/unit/base_spec.rb +20 -0
  110. data/spec/unit/builder/has_and_belongs_to_many_spec.rb +2 -3
  111. data/spec/unit/callback_spec.rb +3 -8
  112. data/spec/unit/collection_proxy_spec.rb +1 -1
  113. data/spec/unit/default_model_mapper_spec.rb +39 -0
  114. data/spec/unit/file_spec.rb +21 -2
  115. data/spec/unit/finder_methods_spec.rb +30 -6
  116. data/spec/unit/has_and_belongs_to_many_association_spec.rb +4 -4
  117. data/spec/unit/has_many_association_spec.rb +25 -3
  118. data/spec/unit/indexing_spec.rb +17 -3
  119. data/spec/unit/model_classifier_spec.rb +49 -0
  120. data/spec/unit/model_spec.rb +0 -9
  121. data/spec/unit/ntriples_datastream_spec.rb +16 -16
  122. data/spec/unit/om_datastream_spec.rb +7 -7
  123. data/spec/unit/qualified_dublin_core_datastream_spec.rb +1 -1
  124. data/spec/unit/query_result_builder_spec.rb +4 -10
  125. data/spec/unit/query_spec.rb +28 -28
  126. data/spec/unit/rdf/indexing_service_spec.rb +16 -16
  127. data/spec/unit/reflection_spec.rb +2 -2
  128. data/spec/unit/scoping_spec.rb +67 -0
  129. data/spec/unit/simple_datastream_spec.rb +2 -2
  130. data/spec/unit/solr_config_options_spec.rb +29 -32
  131. data/spec/unit/solr_hit_spec.rb +58 -0
  132. data/spec/unit/solr_query_builder_spec.rb +9 -1
  133. data/spec/unit/solr_service_spec.rb +19 -3
  134. data/spec/unit/validations_spec.rb +1 -1
  135. metadata +82 -20
  136. data/spec/support/freeze_mocks.rb +0 -12
@@ -3,29 +3,35 @@ module ActiveFedora
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- class_attribute :reflections
7
- self.reflections = {}
6
+ class_attribute :_reflections
7
+ self._reflections = {}
8
8
  end
9
9
 
10
- module ClassMethods
11
- def create_reflection(macro, name, options, active_fedora)
10
+ def reflections
11
+ self.class.reflections
12
+ end
13
+
14
+ class << self
15
+ def create(macro, name, scope, options, active_fedora)
12
16
  klass = case macro
13
17
  when :has_many, :belongs_to, :has_and_belongs_to_many, :contains, :directly_contains, :directly_contains_one, :indirectly_contains
14
18
  AssociationReflection
15
19
  when :rdf, :singular_rdf
16
20
  RDFPropertyReflection
17
21
  end
18
- reflection = klass.new(macro, name, options, active_fedora)
19
- add_reflection name, reflection
20
-
22
+ reflection = klass.new(macro, name, scope, options, active_fedora)
23
+ add_reflection(active_fedora, name, reflection)
21
24
  reflection
22
25
  end
23
26
 
24
- def add_reflection(name, reflection)
27
+ def add_reflection(active_fedora, name, reflection)
28
+ active_fedora.clear_reflections_cache
25
29
  # FIXME: this is where the problem with association_spec is caused (key is string now)
26
- self.reflections = reflections.merge(name => reflection)
30
+ active_fedora._reflections = active_fedora._reflections.merge(name => reflection)
27
31
  end
32
+ end
28
33
 
34
+ module ClassMethods
29
35
  # Returns a hash containing all AssociationReflection objects for the current class.
30
36
  # Example:
31
37
  #
@@ -33,7 +39,47 @@ module ActiveFedora
33
39
  # Account.reflections
34
40
  #
35
41
  def reflections
36
- read_inheritable_attribute(:reflections) || write_inheritable_attribute(:reflections, {})
42
+ @__reflections ||= begin
43
+ ref = {}
44
+
45
+ _reflections.each do |name, reflection|
46
+ parent_reflection = reflection.parent_reflection
47
+
48
+ if parent_reflection
49
+ parent_name = parent_reflection.name
50
+ ref[parent_name.to_s] = parent_reflection
51
+ else
52
+ ref[name] = reflection
53
+ end
54
+ end
55
+
56
+ ref
57
+ end
58
+ end
59
+
60
+ # Returns an array of AssociationReflection objects for all the
61
+ # associations in the class. If you only want to reflect on a certain
62
+ # association type, pass in the symbol (<tt>:has_many</tt>, <tt>:has_one</tt>,
63
+ # <tt>:belongs_to</tt>) as the first parameter.
64
+ #
65
+ # Example:
66
+ #
67
+ # Account.reflect_on_all_associations # returns an array of all associations
68
+ # Account.reflect_on_all_associations(:has_many) # returns an array of all has_many associations
69
+ #
70
+ def reflect_on_all_associations(macro = nil)
71
+ association_reflections = reflections.dup
72
+ association_reflections.select! { |_k, reflection| reflection.macro == macro } if macro
73
+ association_reflections
74
+ end
75
+
76
+ # Returns the AssociationReflection object for the +association+ (use the symbol).
77
+ #
78
+ # Account.reflect_on_association(:owner) # returns the owner AssociationReflection
79
+ # Invoice.reflect_on_association(:line_items).macro # returns :has_many
80
+ #
81
+ def reflect_on_association(association)
82
+ __reflect_on_association(association)
37
83
  end
38
84
 
39
85
  def outgoing_reflections
@@ -41,19 +87,19 @@ module ActiveFedora
41
87
  end
42
88
 
43
89
  def child_resource_reflections
44
- reflections.select { |_, reflection| reflection.is_a?(AssociationReflection) && reflection.macro == :contains && reflection.klass <= ActiveFedora::File }
90
+ reflect_on_all_associations(:contains).select { |_, reflection| reflection.klass <= ActiveFedora::File }
45
91
  end
46
92
 
47
93
  def contained_rdf_source_reflections
48
- reflections.select { |_, reflection| reflection.is_a?(AssociationReflection) && reflection.macro == :contains && !(reflection.klass <= ActiveFedora::File) }
94
+ reflect_on_all_associations(:contains).select { |_, reflection| !(reflection.klass <= ActiveFedora::File) }
49
95
  end
50
96
 
51
97
  # Returns the AssociationReflection object for the +association+ (use the symbol).
52
98
  #
53
- # Account.reflect_on_association(:owner) # returns the owner AssociationReflection
54
- # Invoice.reflect_on_association(:line_items).macro # returns :has_many
99
+ # Account._reflect_on_association(:owner) # returns the owner AssociationReflection
100
+ # Invoice._reflect_on_association(:line_items).macro # returns :has_many
55
101
  #
56
- def reflect_on_association(association)
102
+ def _reflect_on_association(association)
57
103
  val = reflections[association].is_a?(AssociationReflection) ? reflections[association] : nil
58
104
  unless val
59
105
  # When a has_many is paired with a has_and_belongs_to_many the assocation will have a plural name
@@ -66,14 +112,79 @@ module ActiveFedora
66
112
  def reflect_on_all_autosave_associations
67
113
  reflections.values.select { |reflection| reflection.options[:autosave] }
68
114
  end
115
+
116
+ def clear_reflections_cache # :nodoc:
117
+ @__reflections = nil
118
+ end
119
+ end
120
+
121
+ # Holds all the methods that are shared between MacroReflection and ThroughReflection.
122
+ #
123
+ # AbstractReflection
124
+ # MacroReflection
125
+ # AggregateReflection
126
+ # AssociationReflection
127
+ # HasManyReflection
128
+ # HasOneReflection
129
+ # BelongsToReflection
130
+ # HasAndBelongsToManyReflection
131
+ # ThroughReflection
132
+ # PolymorphicReflection
133
+ # RuntimeReflection
134
+ class AbstractReflection # :nodoc:
135
+ def through_reflection?
136
+ false
137
+ end
138
+
139
+ # Returns a new, unsaved instance of the associated class. +attributes+ will
140
+ # be passed to the class's constructor.
141
+ def build_association(attributes, &block)
142
+ klass.new(attributes, &block)
143
+ end
144
+
145
+ # Returns the class name for the macro.
146
+ #
147
+ # <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>'Money'</tt>
148
+ # <tt>has_many :clients</tt> returns <tt>'Client'</tt>
149
+ def class_name
150
+ @class_name ||= (options[:class_name] || derive_class_name).to_s
151
+ end
152
+
153
+ def constraints
154
+ scope_chain.flatten
155
+ end
156
+
157
+ def inverse_of
158
+ return unless inverse_name
159
+
160
+ @inverse_of ||= klass._reflect_on_association inverse_name
161
+ end
162
+
163
+ def check_validity_of_inverse!
164
+ unless polymorphic?
165
+ if has_inverse? && inverse_of.nil?
166
+ raise InverseOfAssociationNotFoundError, self
167
+ end
168
+ end
169
+ end
170
+
171
+ def alias_candidate(name)
172
+ "#{plural_name}_#{name}"
173
+ end
174
+
175
+ def chain
176
+ collect_join_chain
177
+ end
69
178
  end
70
179
 
71
- class MacroReflection
180
+ class MacroReflection < AbstractReflection
72
181
  # Returns the name of the macro.
73
182
  #
74
183
  # <tt>has_many :clients</tt> returns <tt>:clients</tt>
75
184
  attr_reader :name
76
185
 
186
+ attr_reader :scope
187
+
77
188
  # Returns the macro type.
78
189
  #
79
190
  # <tt>has_many :clients</tt> returns <tt>:has_many</tt>
@@ -86,61 +197,42 @@ module ActiveFedora
86
197
 
87
198
  attr_reader :active_fedora
88
199
 
89
- # Returns the target association's class.
90
- #
91
- # class Author < ActiveRecord::Base
92
- # has_many :books
93
- # end
94
- #
95
- # Author.reflect_on_association(:books).klass
96
- # # => Book
97
- #
98
- # <b>Note:</b> Do not call +klass.new+ or +klass.create+ to instantiate
99
- # a new association object. Use +build_association+ or +create_association+
100
- # instead. This allows plugins to hook into association object creation.
101
- def klass
102
- @klass ||= class_name.constantize
103
- end
104
-
105
- def initialize(macro, name, options, active_fedora)
200
+ def initialize(macro, name, scope, options, active_fedora)
106
201
  @macro = macro
107
202
  @name = name
203
+ @scope = scope
108
204
  @options = options
109
205
  @active_fedora = active_fedora
206
+ @klass = options[:anonymous_class]
110
207
  @automatic_inverse_of = nil
111
208
  end
112
209
 
113
- # Returns a new, unsaved instance of the associated class. +options+ will
114
- # be passed to the class's constructor.
115
- def build_association(*options, &block)
116
- klass.new(*options, &block)
210
+ def autosave=(autosave)
211
+ @options[:autosave] = autosave
212
+ parent_reflection = self.parent_reflection
213
+ parent_reflection.autosave = autosave if parent_reflection
117
214
  end
118
215
 
119
- # Returns the class name for the macro.
216
+ # Returns the class for the macro.
120
217
  #
121
- # <tt>has_many :clients</tt> returns <tt>'Client'</tt>
122
- def class_name
123
- @class_name ||= options[:class_name] || derive_class_name
124
- end
125
-
126
- # Returns whether or not this association reflection is for a collection
127
- # association. Returns +true+ if the +macro+ is either +has_many+ or
128
- # +has_and_belongs_to_many+, +false+ otherwise.
129
- def collection?
130
- @collection
218
+ # <tt>composed_of :balance, class_name: 'Money'</tt> returns the Money class
219
+ # <tt>has_many :clients</tt> returns the Client class
220
+ def klass
221
+ @klass ||= compute_class(class_name)
131
222
  end
132
223
 
133
- # Returns +true+ if +self+ is a +belongs_to+ reflection.
134
- def belongs_to?
135
- macro == :belongs_to
224
+ def compute_class(name)
225
+ name.constantize
136
226
  end
137
227
 
138
- def has_many?
139
- macro == :has_many
140
- end
141
-
142
- def has_and_belongs_to_many?
143
- macro == :has_and_belongs_to_many
228
+ # Returns +true+ if +self+ and +other_aggregation+ have the same +name+ attribute, +active_record+ attribute,
229
+ # and +other_aggregation+ has an options hash assigned to it.
230
+ def ==(other)
231
+ super ||
232
+ other.is_a?(self.class) &&
233
+ name == other.name &&
234
+ !other.options.nil? &&
235
+ active_record == other.active_record
144
236
  end
145
237
 
146
238
  private
@@ -150,30 +242,20 @@ module ActiveFedora
150
242
  class_name = class_name.singularize if collection?
151
243
  class_name
152
244
  end
153
-
154
- def derive_foreign_key
155
- if belongs_to?
156
- "#{name}_id"
157
- elsif has_and_belongs_to_many?
158
- "#{name.to_s.singularize}_ids"
159
- elsif options[:as]
160
- "#{options[:as]}_id"
161
- elsif inverse_of && inverse_of.collection?
162
- # for a has_many that is the inverse of a has_and_belongs_to_many
163
- "#{options[:inverse_of].to_s.singularize}_ids"
164
- else
165
- # for a has_many that is the inverse of a belongs_to
166
- active_fedora.name.foreign_key
167
- end
168
- end
169
245
  end
170
246
 
171
247
  # Holds all the meta-data about an association as it was specified in the
172
248
  # Active Record class.
173
249
  class AssociationReflection < MacroReflection #:nodoc:
174
- def initialize(macro, name, options, active_fedora)
250
+ attr_accessor :parent_reflection # Reflection
251
+
252
+ def initialize(macro, name, scope, options, active_fedora)
175
253
  super
176
- @collection = [:has_many, :has_and_belongs_to_many, :directly_contains, :indirectly_contains].include?(macro)
254
+ @constructable = calculate_constructable(macro, options)
255
+ end
256
+
257
+ def constructable? # :nodoc:
258
+ @constructable
177
259
  end
178
260
 
179
261
  # Creates a new instance of the associated class, and immediately saves it
@@ -198,7 +280,7 @@ module ActiveFedora
198
280
 
199
281
  def solr_key
200
282
  @solr_key ||= begin
201
- ActiveFedora::SolrQueryBuilder.solr_name(predicate_for_solr, :symbol)
283
+ ActiveFedora.index_field_mapper.solr_name(predicate_for_solr, :symbol)
202
284
  end
203
285
  end
204
286
 
@@ -213,11 +295,17 @@ module ActiveFedora
213
295
 
214
296
  # A chain of reflections from this one back to the owner. For more see the explanation in
215
297
  # ThroughReflection.
216
- def chain
298
+ def collect_join_chain
217
299
  [self]
218
300
  end
301
+ alias chain collect_join_chain # todo
219
302
 
220
- alias source_macro macro
303
+ # Returns whether or not this association reflection is for a collection
304
+ # association. Returns +true+ if the +macro+ is either +has_many+ or
305
+ # +has_and_belongs_to_many+, +false+ otherwise.
306
+ def collection?
307
+ [:has_many, :has_and_belongs_to_many, :directly_contains, :indirectly_contains].include?(macro)
308
+ end
221
309
 
222
310
  def has_inverse?
223
311
  inverse_name
@@ -226,7 +314,7 @@ module ActiveFedora
226
314
  def inverse_of
227
315
  return unless inverse_name
228
316
 
229
- @inverse_of ||= klass.reflect_on_association inverse_name
317
+ @inverse_of ||= klass._reflect_on_association inverse_name
230
318
  end
231
319
 
232
320
  # Returns whether or not the association should be validated as part of
@@ -268,8 +356,25 @@ module ActiveFedora
268
356
  VALID_AUTOMATIC_INVERSE_MACROS = [:has_many, :has_and_belongs_to_many, :belongs_to].freeze
269
357
  INVALID_AUTOMATIC_INVERSE_OPTIONS = [:conditions, :through, :polymorphic, :foreign_key].freeze
270
358
 
359
+ # Returns +true+ if +self+ is a +belongs_to+ reflection.
360
+ def belongs_to?
361
+ macro == :belongs_to
362
+ end
363
+
364
+ def has_many?
365
+ macro == :has_many
366
+ end
367
+
368
+ def has_and_belongs_to_many?
369
+ macro == :has_and_belongs_to_many
370
+ end
371
+
271
372
  private
272
373
 
374
+ def calculate_constructable(_macro, _options)
375
+ true
376
+ end
377
+
273
378
  def inverse_name
274
379
  options.fetch(:inverse_of) do
275
380
  if @automatic_inverse_of == false
@@ -299,7 +404,7 @@ module ActiveFedora
299
404
  inverse_name = ActiveSupport::Inflector.underscore(options[:as] || active_fedora.name.demodulize).to_sym
300
405
 
301
406
  begin
302
- reflection = klass.reflect_on_association(inverse_name)
407
+ reflection = klass._reflect_on_association(inverse_name)
303
408
  rescue NameError
304
409
  # Give up: we couldn't compute the klass type so we won't be able
305
410
  # to find any associations either.
@@ -328,9 +433,30 @@ module ActiveFedora
328
433
  !INVALID_AUTOMATIC_INVERSE_OPTIONS.any? { |opt| reflection.options[opt] }
329
434
  # && !reflection.scope
330
435
  end
436
+
437
+ def derive_foreign_key
438
+ if belongs_to?
439
+ "#{name}_id"
440
+ elsif has_and_belongs_to_many?
441
+ "#{name.to_s.singularize}_ids"
442
+ elsif options[:as]
443
+ "#{options[:as]}_id"
444
+ elsif inverse_of && inverse_of.collection?
445
+ # for a has_many that is the inverse of a has_and_belongs_to_many
446
+ "#{options[:inverse_of].to_s.singularize}_ids"
447
+ else
448
+ # for a has_many that is the inverse of a belongs_to
449
+ active_fedora.name.foreign_key
450
+ end
451
+ end
331
452
  end
332
453
 
333
454
  class RDFPropertyReflection < AssociationReflection
455
+ def initialize(*args)
456
+ super
457
+ active_fedora.index_config[name] = build_index_config
458
+ end
459
+
334
460
  def derive_foreign_key
335
461
  name
336
462
  end
@@ -340,6 +466,12 @@ module ActiveFedora
340
466
  class_name = class_name.singularize if collection?
341
467
  class_name
342
468
  end
469
+
470
+ private
471
+
472
+ def build_index_config
473
+ ActiveFedora::Indexing::Map::IndexObject.new(predicate_for_solr) { |index| index.as :symbol }
474
+ end
343
475
  end
344
476
  end
345
477
  end
@@ -8,11 +8,7 @@ module ActiveFedora
8
8
  opts[:rows] = limit_value if limit_value
9
9
  opts[:sort] = order_values if order_values
10
10
 
11
- calculate :count, where_values, opts
12
- end
13
-
14
- def calculate(_calculation, conditions, _opts = {})
15
- SolrService.query(create_query(conditions), raw: true, rows: 0).fetch('response'.freeze)['numFound'.freeze]
11
+ SolrService.count(create_query(where_values))
16
12
  end
17
13
  end
18
14
  end
@@ -87,7 +87,7 @@ module ActiveFedora
87
87
  return false unless conditions
88
88
  case conditions
89
89
  when Hash
90
- find_with_conditions(conditions, rows: 1).present?
90
+ search_with_conditions(conditions, rows: 1).present?
91
91
  when String
92
92
  find(conditions).present?
93
93
  else
@@ -102,15 +102,35 @@ module ActiveFedora
102
102
  # hash representing the query part of an solr statement. If a hash is
103
103
  # provided, this method will generate conditions based simple equality
104
104
  # combined using the boolean AND operator.
105
- # @param[Hash] options
105
+ # @param [Hash] options
106
106
  # @option opts [Array] :sort a list of fields to sort by
107
107
  # @option opts [Array] :rows number of rows to return
108
- def find_with_conditions(conditions, opts = {})
108
+ def search_with_conditions(conditions, opts = {})
109
109
  # set default sort to created date ascending
110
110
  opts[:sort] = @klass.default_sort_params unless opts.include?(:sort)
111
111
  SolrService.query(create_query(conditions), opts)
112
112
  end
113
113
 
114
+ # Returns a single solr hit matching the given id
115
+ # @param [String] id document id
116
+ # @param [Hash] opts
117
+ def search_by_id(id, opts = {})
118
+ opts[:rows] = 1
119
+ result = search_with_conditions({ id: id }, opts)
120
+
121
+ if result.empty?
122
+ raise ActiveFedora::ObjectNotFoundError, "Object #{id} not found in solr"
123
+ end
124
+
125
+ result.first
126
+ end
127
+
128
+ # @deprecated
129
+ def find_with_conditions(*args)
130
+ Deprecation.warn(ActiveFedora::Base, '.find_with_conditions is deprecated and will be removed in active-fedora 10.0; use .search_with_conditions instead')
131
+ search_with_conditions(*args)
132
+ end
133
+
114
134
  # Yields the found ActiveFedora::Base object to the passed block
115
135
  #
116
136
  # @param [Hash] conditions the conditions for the solr search to match
@@ -118,12 +138,12 @@ module ActiveFedora
118
138
  # @option opts [Boolean] :cast (true) when true, examine the model and cast it to the first known cModel
119
139
  def find_each(conditions = {}, opts = {})
120
140
  cast = opts.delete(:cast)
121
- find_in_batches(conditions, opts.merge(fl: SOLR_DOCUMENT_ID)) do |group|
141
+ search_in_batches(conditions, opts.merge(fl: ActiveFedora.id_field)) do |group|
122
142
  group.each do |hit|
123
143
  begin
124
- yield(load_from_fedora(hit[SOLR_DOCUMENT_ID], cast))
144
+ yield(load_from_fedora(hit[ActiveFedora.id_field], cast))
125
145
  rescue Ldp::Gone
126
- ActiveFedora::Base.logger.error "Although #{hit[SOLR_DOCUMENT_ID]} was found in Solr, it doesn't seem to exist in Fedora. The index is out of synch." if ActiveFedora::Base.logger
146
+ ActiveFedora::Base.logger.error "Although #{hit[ActiveFedora.id_field]} was found in Solr, it doesn't seem to exist in Fedora. The index is out of synch." if ActiveFedora::Base.logger
127
147
  end
128
148
  end
129
149
  end
@@ -140,11 +160,10 @@ module ActiveFedora
140
160
  # @option opts [Array] :rows number of rows to return
141
161
  #
142
162
  # @example
143
- # Person.find_in_batches('age_t'=>'21', {:batch_size=>50}) do |group|
163
+ # Person.search_in_batches('age_t'=>'21', {:batch_size=>50}) do |group|
144
164
  # group.each { |person| puts person['name_t'] }
145
165
  # end
146
-
147
- def find_in_batches(conditions, opts = {})
166
+ def search_in_batches(conditions, opts = {})
148
167
  opts[:q] = create_query(conditions)
149
168
  opts[:qt] = @klass.solr_query_handler
150
169
  # set default sort to created date ascending
@@ -163,6 +182,12 @@ module ActiveFedora
163
182
  end
164
183
  end
165
184
 
185
+ # @deprecated
186
+ def find_in_batches(*args)
187
+ Deprecation.warn(ActiveFedora::Base, '.find_in_batches is deprecated and will be removed in active-fedora 10.0; use .search_in_batches instead')
188
+ search_in_batches(*args)
189
+ end
190
+
166
191
  # Retrieve the Fedora object with the given id, explore the returned object
167
192
  # Raises a ObjectNotFoundError if the object is not found.
168
193
  # @param [String] id of the object to load
@@ -174,7 +199,7 @@ module ActiveFedora
174
199
  if where_values.empty?
175
200
  load_from_fedora(id, cast)
176
201
  else
177
- conditions = where_values + [ActiveFedora::SolrQueryBuilder.raw_query(SOLR_DOCUMENT_ID, id)]
202
+ conditions = where_values + [ActiveFedora::SolrQueryBuilder.construct_query(ActiveFedora.id_field => id)]
178
203
  query = conditions.join(" AND ".freeze)
179
204
  to_enum(:find_each, query, {}).to_a.first
180
205
  end
@@ -193,8 +218,7 @@ module ActiveFedora
193
218
  if @klass == ActiveFedora::Base && cast == false
194
219
  ActiveFedora::Base
195
220
  else
196
- # The true class may be a subclass of @klass, so always use from_class_uri
197
- resource_class = Model.from_class_uri(has_model_value(resource)) || ActiveFedora::Base
221
+ resource_class = has_model_value(resource)
198
222
  unless equivalent_class?(resource_class)
199
223
  raise ActiveFedora::ActiveFedoraError, "Model mismatch. Expected #{@klass}. Got: #{resource_class}"
200
224
  end
@@ -203,18 +227,7 @@ module ActiveFedora
203
227
  end
204
228
 
205
229
  def has_model_value(resource)
206
- best_model_match = nil
207
-
208
- resource.graph.query([nil, ActiveFedora::RDF::Fcrepo::Model.hasModel, nil]).each do |rg|
209
- model_value = Model.from_class_uri(rg.object.to_s)
210
- next unless model_value
211
- best_model_match ||= model_value
212
-
213
- # If there is an inheritance structure, use the most specific case.
214
- best_model_match = model_value if best_model_match > model_value
215
- end
216
-
217
- best_model_match.to_s
230
+ ActiveFedora.model_mapper.classifier(resource).best_model
218
231
  end
219
232
 
220
233
  def equivalent_class?(other_class)
@@ -276,9 +289,9 @@ module ActiveFedora
276
289
  def field_name_for(key)
277
290
  if @klass.delegated_attributes.key?(key)
278
291
  # TODO: Check to see if `key' is a possible solr field for this class, if it isn't try :searchable instead
279
- ActiveFedora::SolrQueryBuilder.solr_name(key, :stored_searchable, type: :string)
292
+ ActiveFedora.index_field_mapper.solr_name(key, :stored_searchable, type: :string)
280
293
  elsif key == :id
281
- SOLR_DOCUMENT_ID
294
+ ActiveFedora.id_field
282
295
  else
283
296
  key.to_s
284
297
  end
@@ -62,6 +62,23 @@ module ActiveFedora
62
62
  @records
63
63
  end
64
64
 
65
+ # Scope all queries to the current scope.
66
+ #
67
+ # Comment.where(post_id: 1).scoping do
68
+ # Comment.first
69
+ # end
70
+ # # => SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1 ORDER BY "comments"."id" ASC LIMIT 1
71
+ #
72
+ # Please check unscoped if you want to remove all previous scopes (including
73
+ # the default_scope) during the execution of a block.
74
+ def scoping
75
+ previous = klass.current_scope
76
+ klass.current_scope = self
77
+ yield
78
+ ensure
79
+ klass.current_scope = previous
80
+ end
81
+
65
82
  def ==(other)
66
83
  case other
67
84
  when Relation