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,6 +3,14 @@ module ActiveFedora
3
3
  module Default
4
4
  extend ActiveSupport::Concern
5
5
 
6
+ included do
7
+ class_attribute :default_scopes, instance_writer: false, instance_predicate: false
8
+ class_attribute :default_scope_override, instance_writer: false, instance_predicate: false
9
+
10
+ self.default_scopes = []
11
+ self.default_scope_override = nil
12
+ end
13
+
6
14
  module ClassMethods
7
15
  # Returns a scope for the model without the +default_scope+.
8
16
  #
@@ -32,6 +40,115 @@ module ActiveFedora
32
40
  def unscoped
33
41
  block_given? ? relation.scoping { yield } : relation
34
42
  end
43
+
44
+ # Are there attributes associated with this scope?
45
+ def scope_attributes? # :nodoc:
46
+ super || default_scopes.any? || respond_to?(:default_scope)
47
+ end
48
+
49
+ def before_remove_const #:nodoc:
50
+ self.current_scope = nil
51
+ end
52
+
53
+ protected
54
+
55
+ # Use this macro in your model to set a default scope for all operations on
56
+ # the model.
57
+ #
58
+ # class Article < ActiveRecord::Base
59
+ # default_scope { where(published: true) }
60
+ # end
61
+ #
62
+ # Article.all # => SELECT * FROM articles WHERE published = true
63
+ #
64
+ # The #default_scope is also applied while creating/building a record.
65
+ # It is not applied while updating a record.
66
+ #
67
+ # Article.new.published # => true
68
+ # Article.create.published # => true
69
+ #
70
+ # (You can also pass any object which responds to +call+ to the
71
+ # +default_scope+ macro, and it will be called when building the
72
+ # default scope.)
73
+ #
74
+ # If you use multiple #default_scope declarations in your model then
75
+ # they will be merged together:
76
+ #
77
+ # class Article < ActiveRecord::Base
78
+ # default_scope { where(published: true) }
79
+ # default_scope { where(rating: 'G') }
80
+ # end
81
+ #
82
+ # Article.all # => SELECT * FROM articles WHERE published = true AND rating = 'G'
83
+ #
84
+ # This is also the case with inheritance and module includes where the
85
+ # parent or module defines a #default_scope and the child or including
86
+ # class defines a second one.
87
+ #
88
+ # If you need to do more complex things with a default scope, you can
89
+ # alternatively define it as a class method:
90
+ #
91
+ # class Article < ActiveRecord::Base
92
+ # def self.default_scope
93
+ # # Should return a scope, you can call 'super' here etc.
94
+ # end
95
+ # end
96
+ def default_scope(scope = nil)
97
+ scope = Proc.new if block_given?
98
+
99
+ if scope.is_a?(Relation) || !scope.respond_to?(:call)
100
+ raise ArgumentError,
101
+ "Support for calling #default_scope without a block is removed. For example instead " \
102
+ "of `default_scope where(color: 'red')`, please use " \
103
+ "`default_scope { where(color: 'red') }`. (Alternatively you can just redefine " \
104
+ "self.default_scope.)"
105
+ end
106
+
107
+ self.default_scopes += [scope]
108
+ end
109
+
110
+ def build_default_scope(base_rel = nil) # :nodoc:
111
+ return if abstract_class?
112
+
113
+ if default_scope_override.nil?
114
+ self.default_scope_override = !Base.is_a?(method(:default_scope).owner)
115
+ end
116
+
117
+ if default_scope_override
118
+ # The user has defined their own default scope method, so call that
119
+ evaluate_default_scope { default_scope }
120
+ elsif default_scopes.any?
121
+ base_rel ||= relation
122
+ evaluate_default_scope do
123
+ default_scopes.inject(base_rel) do |default_scope, scope|
124
+ scope = scope.respond_to?(:to_proc) ? scope : scope.method(:call)
125
+ default_scope.merge(base_rel.instance_exec(&scope))
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ def ignore_default_scope? # :nodoc:
132
+ ScopeRegistry.value_for(:ignore_default_scope, base_class)
133
+ end
134
+
135
+ def ignore_default_scope=(ignore) # :nodoc:
136
+ ScopeRegistry.set_value_for(:ignore_default_scope, base_class, ignore)
137
+ end
138
+
139
+ # The ignore_default_scope flag is used to prevent an infinite recursion
140
+ # situation where a default scope references a scope which has a default
141
+ # scope which references a scope...
142
+ def evaluate_default_scope # :nodoc:
143
+ return if ignore_default_scope?
144
+
145
+ begin
146
+ self.ignore_default_scope = true
147
+ yield
148
+ ensure
149
+ self.ignore_default_scope = false
150
+ end
151
+ end
35
152
  end
36
153
  end
37
154
  end
@@ -16,16 +16,164 @@ module ActiveFedora
16
16
  # fruits = fruits.limit(10) if limited?
17
17
  #
18
18
  # You can define a scope that applies to all finders using
19
- # <tt>ActiveRecord::Base.default_scope</tt>.
19
+ # <tt>ActiveFedora::Base.default_scope</tt>.
20
20
  def all
21
21
  if current_scope
22
22
  current_scope.clone
23
23
  else
24
- scope = relation
25
- scope.default_scoped = true
26
- scope
24
+ default_scoped
27
25
  end
28
26
  end
27
+
28
+ def default_scoped
29
+ scope = build_default_scope
30
+
31
+ if scope
32
+ relation.spawn.merge!(scope)
33
+ else
34
+ relation
35
+ end
36
+ end
37
+
38
+ # Adds a class method for retrieving and querying objects.
39
+ # The method is intended to return an ActiveFedora::Relation
40
+ # object, which is composable with other scopes.
41
+ # If it returns nil or false, an
42
+ # {all}[rdoc-ref:Scoping::Named::ClassMethods#all] scope is returned instead.
43
+ #
44
+ # A \scope represents a narrowing of a database query, such as
45
+ # <tt>where(color: :red).select('shirts.*').includes(:washing_instructions)</tt>.
46
+ #
47
+ # class Shirt < ActiveFedora::Base
48
+ # scope :red, -> { where(color: 'red') }
49
+ # scope :dry_clean_only, -> { joins(:washing_instructions).where('washing_instructions.dry_clean_only = ?', true) }
50
+ # end
51
+ #
52
+ # The above calls to #scope define class methods <tt>Shirt.red</tt> and
53
+ # <tt>Shirt.dry_clean_only</tt>. <tt>Shirt.red</tt>, in effect,
54
+ # represents the query <tt>Shirt.where(color: 'red')</tt>.
55
+ #
56
+ # You should always pass a callable object to the scopes defined
57
+ # with #scope. This ensures that the scope is re-evaluated each
58
+ # time it is called.
59
+ #
60
+ # Note that this is simply 'syntactic sugar' for defining an actual
61
+ # class method:
62
+ #
63
+ # class Shirt < ActiveFedora::Base
64
+ # def self.red
65
+ # where(color: 'red')
66
+ # end
67
+ # end
68
+ #
69
+ # Unlike <tt>Shirt.find(...)</tt>, however, the object returned by
70
+ # <tt>Shirt.red</tt> is not an Array but an ActiveFedora::Relation,
71
+ # which is composable with other scopes; it resembles the association object
72
+ # constructed by a {has_many}[rdoc-ref:Associations::ClassMethods#has_many]
73
+ # declaration. For instance, you can invoke <tt>Shirt.red.first</tt>, <tt>Shirt.red.count</tt>,
74
+ # <tt>Shirt.red.where(size: 'small')</tt>. Also, just as with the
75
+ # association objects, named \scopes act like an Array, implementing
76
+ # Enumerable; <tt>Shirt.red.each(&block)</tt>, <tt>Shirt.red.first</tt>,
77
+ # and <tt>Shirt.red.inject(memo, &block)</tt> all behave as if
78
+ # <tt>Shirt.red</tt> really was an array.
79
+ #
80
+ # These named \scopes are composable. For instance,
81
+ # <tt>Shirt.red.dry_clean_only</tt> will produce all shirts that are
82
+ # both red and dry clean only. Nested finds and calculations also work
83
+ # with these compositions: <tt>Shirt.red.dry_clean_only.count</tt>
84
+ # returns the number of garments for which these criteria obtain.
85
+ # Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
86
+ #
87
+ # All scopes are available as class methods on the ActiveFedora::Base
88
+ # descendant upon which the \scopes were defined. But they are also
89
+ # available to {has_many}[rdoc-ref:Associations::ClassMethods#has_many]
90
+ # associations. If,
91
+ #
92
+ # class Person < ActiveFedora::Base
93
+ # has_many :shirts
94
+ # end
95
+ #
96
+ # then <tt>elton.shirts.red.dry_clean_only</tt> will return all of
97
+ # Elton's red, dry clean only shirts.
98
+ #
99
+ # \Named scopes can also have extensions, just as with
100
+ # {has_many}[rdoc-ref:Associations::ClassMethods#has_many] declarations:
101
+ #
102
+ # class Shirt < ActiveFedora::Base
103
+ # scope :red, -> { where(color: 'red') } do
104
+ # def dom_id
105
+ # 'red_shirts'
106
+ # end
107
+ # end
108
+ # end
109
+ #
110
+ # Scopes can also be used while creating/building a record.
111
+ #
112
+ # class Article < ActiveFedora::Base
113
+ # scope :published, -> { where(published: true) }
114
+ # end
115
+ #
116
+ # Article.published.new.published # => true
117
+ # Article.published.create.published # => true
118
+ #
119
+ # \Class methods on your model are automatically available
120
+ # on scopes. Assuming the following setup:
121
+ #
122
+ # class Article < ActiveFedora::Base
123
+ # scope :published, -> { where(published: true) }
124
+ # scope :featured, -> { where(featured: true) }
125
+ #
126
+ # def self.latest_article
127
+ # order('published_at desc').first
128
+ # end
129
+ #
130
+ # def self.titles
131
+ # pluck(:title)
132
+ # end
133
+ # end
134
+ #
135
+ # We are able to call the methods like this:
136
+ #
137
+ # Article.published.featured.latest_article
138
+ # Article.featured.titles
139
+ def scope(name, body, &block)
140
+ unless body.respond_to?(:call)
141
+ raise ArgumentError, 'The scope body needs to be callable.'
142
+ end
143
+
144
+ if dangerous_class_method?(name)
145
+ raise ArgumentError, "You tried to define a scope named \"#{name}\" " \
146
+ "on the model \"#{self.name}\", but Active Record already defined " \
147
+ "a class method with the same name."
148
+ end
149
+
150
+ valid_scope_name?(name)
151
+ extension = Module.new(&block) if block
152
+
153
+ if body.respond_to?(:to_proc)
154
+ singleton_class.send(:define_method, name) do |*args|
155
+ scope = all.scoping { instance_exec(*args, &body) }
156
+ scope = scope.extending(extension) if extension
157
+
158
+ scope || all
159
+ end
160
+ else
161
+ singleton_class.send(:define_method, name) do |*args|
162
+ scope = all.scoping { body.call(*args) }
163
+ scope = scope.extending(extension) if extension
164
+
165
+ scope || all
166
+ end
167
+ end
168
+ end
169
+
170
+ protected
171
+
172
+ def valid_scope_name?(name)
173
+ return unless respond_to?(name, true)
174
+ logger.warn "Creating scope :#{name}. " \
175
+ "Overwriting existing method #{self.name}.#{name}."
176
+ end
29
177
  end
30
178
  end
31
179
  end
@@ -1,6 +1,9 @@
1
+ require 'active_support/per_thread_registry'
2
+
1
3
  module ActiveFedora
2
4
  module Scoping
3
5
  extend ActiveSupport::Concern
6
+
4
7
  included do
5
8
  include Default
6
9
  include Named
@@ -8,12 +11,92 @@ module ActiveFedora
8
11
 
9
12
  module ClassMethods
10
13
  def current_scope #:nodoc:
11
- Thread.current["#{self}_current_scope"]
14
+ ScopeRegistry.value_for(:current_scope, self)
12
15
  end
13
16
 
14
17
  def current_scope=(scope) #:nodoc:
15
- Thread.current["#{self}_current_scope"] = scope
18
+ ScopeRegistry.set_value_for(:current_scope, self, scope)
19
+ end
20
+
21
+ # Collects attributes from scopes that should be applied when creating
22
+ # an AF instance for the particular class this is called on.
23
+ def scope_attributes # :nodoc:
24
+ all.scope_for_create
25
+ end
26
+
27
+ # Are there attributes associated with this scope?
28
+ def scope_attributes? # :nodoc:
29
+ current_scope
16
30
  end
17
31
  end
32
+
33
+ def populate_with_current_scope_attributes # :nodoc:
34
+ return unless self.class.scope_attributes?
35
+
36
+ self.class.scope_attributes.each do |att, value|
37
+ send("#{att}=", value) if respond_to?("#{att}=")
38
+ end
39
+ end
40
+
41
+ def initialize_internals_callback # :nodoc:
42
+ super
43
+ populate_with_current_scope_attributes
44
+ end
45
+
46
+ # This class stores the +:current_scope+ and +:ignore_default_scope+ values
47
+ # for different classes. The registry is stored as a thread local, which is
48
+ # accessed through +ScopeRegistry.current+.
49
+ #
50
+ # This class allows you to store and get the scope values on different
51
+ # classes and different types of scopes. For example, if you are attempting
52
+ # to get the current_scope for the +Board+ model, then you would use the
53
+ # following code:
54
+ #
55
+ # registry = ActiveFedora::Scoping::ScopeRegistry
56
+ # registry.set_value_for(:current_scope, Board, some_new_scope)
57
+ #
58
+ # Now when you run:
59
+ #
60
+ # registry.value_for(:current_scope, Board)
61
+ #
62
+ # You will obtain whatever was defined in +some_new_scope+. The #value_for
63
+ # and #set_value_for methods are delegated to the current ScopeRegistry
64
+ # object, so the above example code can also be called as:
65
+ #
66
+ # ActiveFedora::Scoping::ScopeRegistry.set_value_for(:current_scope,
67
+ # Board, some_new_scope)
68
+ class ScopeRegistry # :nodoc:
69
+ extend ActiveSupport::PerThreadRegistry
70
+
71
+ VALID_SCOPE_TYPES = [:current_scope, :ignore_default_scope].freeze
72
+
73
+ def initialize
74
+ @registry = Hash.new { |hash, key| hash[key] = {} }
75
+ end
76
+
77
+ # Obtains the value for a given +scope_type+ and +model+.
78
+ def value_for(scope_type, model)
79
+ raise_invalid_scope_type!(scope_type)
80
+ klass = model
81
+ base = model.base_class
82
+ while klass <= base
83
+ value = @registry[scope_type][klass.name]
84
+ return value if value
85
+ klass = klass.superclass
86
+ end
87
+ end
88
+
89
+ # Sets the +value+ for a given +scope_type+ and +model+.
90
+ def set_value_for(scope_type, model, value)
91
+ raise_invalid_scope_type!(scope_type)
92
+ @registry[scope_type][model.name] = value
93
+ end
94
+
95
+ private
96
+
97
+ def raise_invalid_scope_type!(scope_type)
98
+ raise ArgumentError, "Invalid scope type '#{scope_type}' sent to the registry. Scope types must be included in VALID_SCOPE_TYPES" unless VALID_SCOPE_TYPES.include?(scope_type)
99
+ end
100
+ end
18
101
  end
19
102
  end
@@ -79,7 +79,7 @@ module ActiveFedora
79
79
  next if field_key == :location ## FIXME HYDRA-825
80
80
  things = send(field_key)
81
81
  next unless things
82
- field_symbol = ActiveFedora::SolrQueryBuilder.solr_name(field_key, type: field_info[:type])
82
+ field_symbol = ActiveFedora.index_field_mapper.solr_name(field_key, type: field_info[:type])
83
83
  things.val.each do |val|
84
84
  ::Solrizer::Extractor.insert_solr_field_value(solr_doc, field_symbol, val.to_s)
85
85
  end
@@ -0,0 +1,71 @@
1
+ module ActiveFedora
2
+ class SolrHit < Delegator
3
+ def self.for(hit)
4
+ return hit if hit.is_a? ActiveFedora::SolrHit
5
+
6
+ SolrHit.new(hit)
7
+ end
8
+
9
+ delegate :models, to: :classifier
10
+
11
+ def __getobj__
12
+ @document # return object we are delegating to, required
13
+ end
14
+
15
+ alias static_config __getobj__
16
+
17
+ def __setobj__(obj)
18
+ @document = obj
19
+ end
20
+
21
+ attr_reader :document
22
+
23
+ def initialize(document)
24
+ super
25
+ @document = document
26
+ end
27
+
28
+ def id
29
+ document[ActiveFedora.id_field]
30
+ end
31
+
32
+ def rdf_uri
33
+ ::RDF::URI.new(ActiveFedora::Base.id_to_uri(id))
34
+ end
35
+
36
+ def model(opts = {})
37
+ best_model_match = classifier.best_model(opts)
38
+ ActiveFedora::Base.logger.warn "Could not find a model for #{id}, defaulting to ActiveFedora::Base" if ActiveFedora::Base.logger && best_model_match == ActiveFedora::Base
39
+ best_model_match
40
+ end
41
+
42
+ def model?(model_to_check)
43
+ models.any? do |model|
44
+ model_to_check >= model
45
+ end
46
+ end
47
+
48
+ def instantiate_with_json
49
+ model.allocate.init_with_json(profile_json) do |allocated_object|
50
+ create_key = allocated_object.indexing_service.class.create_time_solr_name
51
+ modified_key = allocated_object.indexing_service.class.modified_time_solr_name
52
+ allocated_object.resource.set_value(:create_date, DateTime.parse(document[create_key])) if document[create_key]
53
+ allocated_object.resource.set_value(:modified_date, DateTime.parse(document[modified_key])) if document[modified_key]
54
+ end
55
+ end
56
+
57
+ def reify(opts = {})
58
+ model(opts).find(id, cast: true)
59
+ end
60
+
61
+ private
62
+
63
+ def classifier
64
+ ActiveFedora.model_mapper.classifier(document)
65
+ end
66
+
67
+ def profile_json
68
+ Array(document[ActiveFedora::IndexingService.profile_solr_name]).first
69
+ end
70
+ end
71
+ end
@@ -12,13 +12,12 @@ module ActiveFedora
12
12
  def initialize(context, id, solr_doc = nil)
13
13
  @context = context
14
14
  @id = id
15
- @solr_doc = solr_doc
16
- validate_solr_doc_and_id!(@solr_doc)
15
+ self.solr_doc = solr_doc
17
16
  end
18
17
 
19
18
  def object
20
19
  return @object if @object
21
- @object = allocate_object
20
+ @object = solr_doc.instantiate_with_json
22
21
  @object.readonly!
23
22
  @object.freeze
24
23
  @object
@@ -26,46 +25,23 @@ module ActiveFedora
26
25
 
27
26
  private
28
27
 
29
- def allocate_object
30
- active_fedora_class.allocate.init_with_json(profile_json) do |allocated_object|
31
- create_key = allocated_object.indexing_service.class.create_time_solr_name
32
- modified_key = allocated_object.indexing_service.class.modified_time_solr_name
33
- allocated_object.resource.set_value(:create_date, DateTime.parse(solr_doc[create_key])) if solr_doc[create_key]
34
- allocated_object.resource.set_value(:modified_date, DateTime.parse(solr_doc[modified_key])) if solr_doc[modified_key]
35
- end
36
- end
37
-
38
28
  def solr_doc
39
29
  @solr_doc ||= begin
40
- result = context.find_with_conditions(id: id)
41
- if result.empty?
42
- raise ActiveFedora::ObjectNotFoundError, "Object #{id} not found in solr"
43
- end
44
- @solr_doc = result.first
45
- validate_solr_doc_and_id!(@solr_doc)
46
- @solr_doc
30
+ self.solr_doc = context.search_by_id(id)
47
31
  end
48
32
  end
49
33
 
50
- def validate_solr_doc_and_id!(document)
51
- return true if document.nil?
52
- solr_id = document[SOLR_DOCUMENT_ID]
53
- return if id == solr_id
54
- raise ActiveFedora::FedoraSolrMismatchError, id, solr_id
55
- end
56
-
57
- def active_fedora_class
58
- @active_fedora_class ||= ActiveFedora::QueryResultBuilder.class_from_solr_document(solr_doc)
34
+ def solr_doc=(solr_doc)
35
+ unless solr_doc.nil?
36
+ solr_doc = ActiveFedora::SolrHit.for(solr_doc)
37
+ validate_solr_doc_and_id!(solr_doc)
38
+ @solr_doc = solr_doc
39
+ end
59
40
  end
60
41
 
61
- def profile_json
62
- @profile_json ||= begin
63
- profile_json = Array(solr_doc[ActiveFedora::IndexingService.profile_solr_name]).first
64
- unless profile_json.present?
65
- raise ActiveFedora::ObjectNotFoundError, "Object #{id} does not contain a solrized profile"
66
- end
67
- profile_json
68
- end
42
+ def validate_solr_doc_and_id!(document)
43
+ return if id == document.id
44
+ raise ActiveFedora::FedoraSolrMismatchError, id, document.id
69
45
  end
70
46
  end
71
47
  end
@@ -1,6 +1,6 @@
1
1
  module ActiveFedora
2
2
  module SolrQueryBuilder
3
- PARSED_SUFFIX = '_tesim'.freeze
3
+ extend Deprecation
4
4
 
5
5
  class << self
6
6
  # Construct a solr query for a list of ids
@@ -10,18 +10,21 @@ module ActiveFedora
10
10
  def construct_query_for_ids(id_array)
11
11
  ids = id_array.reject(&:blank?)
12
12
  return "id:NEVER_USE_THIS_ID" if ids.empty?
13
- "{!terms f=#{SOLR_DOCUMENT_ID}}#{ids.join(',')}"
13
+ "{!terms f=#{ActiveFedora.id_field}}#{ids.join(',')}"
14
14
  end
15
15
 
16
16
  # Create a raw query clause suitable for sending to solr as an fq element
17
17
  # @param [String] key
18
18
  # @param [String] value
19
19
  def raw_query(key, value)
20
+ Deprecation.warn(ActiveFedora::SolrQueryBuilder, 'ActiveFedora::SolrQueryBuilder.raw_query is deprecated and will be removed in ActiveFedora 10.0. Use .construct_query instead.')
20
21
  "_query_:\"{!raw f=#{key}}#{value.gsub('"', '\"')}\""
21
22
  end
22
23
 
24
+ # @deprecated
23
25
  def solr_name(*args)
24
- Solrizer.default_field_mapper.solr_name(*args)
26
+ Deprecation.warn(ActiveFedora::SolrQueryBuilder, 'ActiveFedora::SolrQueryBuilder.solr_name is deprecated and will be removed in ActiveFedora 10.0. Use ActiveFedora.index_field_mapper.solr_name instead.')
27
+ ActiveFedora.index_field_mapper.solr_name(*args)
25
28
  end
26
29
 
27
30
  # Create a query with a clause for each key, value
@@ -29,9 +32,9 @@ module ActiveFedora
29
32
  # @param [String] join_with ('AND') the value we're joining the clauses with
30
33
  # @example
31
34
  # construct_query_for_rel [[:has_model, "info:fedora/afmodel:ComplexCollection"], [:has_model, "info:fedora/afmodel:ActiveFedora_Base"]], 'OR'
32
- # # => _query_:"{!raw f=has_model_ssim}info:fedora/afmodel:ComplexCollection" OR _query_:"{!raw f=has_model_ssim}info:fedora/afmodel:ActiveFedora_Base"
35
+ # # => _query_:"{!field f=has_model_ssim}info:fedora/afmodel:ComplexCollection" OR _query_:"{!field f=has_model_ssim}info:fedora/afmodel:ActiveFedora_Base"
33
36
  #
34
- # construct_query_for_rel [[Book.reflect_on_association(:library), "foo/bar/baz"]]
37
+ # construct_query_for_rel [[Book._reflect_on_association(:library), "foo/bar/baz"]]
35
38
  def construct_query_for_rel(field_pairs, join_with = ' AND ')
36
39
  field_pairs = field_pairs.to_a if field_pairs.is_a? Hash
37
40
  construct_query(property_values_to_solr(field_pairs), join_with)
@@ -43,7 +46,7 @@ module ActiveFedora
43
46
  # @return [String] a solr query
44
47
  # @example
45
48
  # construct_query([['library_id_ssim', '123'], ['owner_ssim', 'Fred']])
46
- # # => "_query_:\"{!raw f=library_id_ssim}123\" AND _query_:\"{!raw f=owner_ssim}Fred\""
49
+ # # => "_query_:\"{!field f=library_id_ssim}123\" AND _query_:\"{!field f=owner_ssim}Fred\""
47
50
  def construct_query(field_pairs, join_with = ' AND ')
48
51
  pairs_to_clauses(field_pairs).join(join_with)
49
52
  end
@@ -66,12 +69,7 @@ module ActiveFedora
66
69
  values << nil if values.empty?
67
70
  values.map do |value|
68
71
  if value.present?
69
- if parsed?(field)
70
- # If you do a raw query on a parsed field you won't get the matches you expect.
71
- "#{field}:#{solr_escape(value)}"
72
- else
73
- raw_query(field, value)
74
- end
72
+ field_query(field, value)
75
73
  else
76
74
  # Check that the field is not present. In SQL: "WHERE field IS NULL"
77
75
  "-#{field}:[* TO *]"
@@ -79,16 +77,6 @@ module ActiveFedora
79
77
  end
80
78
  end
81
79
 
82
- def parsed?(field)
83
- field.end_with?(PARSED_SUFFIX)
84
- end
85
-
86
- # Adds esaping for spaces which are not handled by RSolr.solr_escape
87
- # See rsolr/rsolr#101
88
- def solr_escape(terms)
89
- RSolr.solr_escape(terms).gsub(/\s+/, "\\ ")
90
- end
91
-
92
80
  # Given a list of pairs (e.g. [field name, values]), convert the field names
93
81
  # to solr names
94
82
  # @param [Array<Array>] pairs a list of pairs of property name and values
@@ -97,8 +85,8 @@ module ActiveFedora
97
85
  # property_values_to_solr([['library_id', '123'], ['owner', 'Fred']])
98
86
  # # => [['library_id_ssim', '123'], ['owner_ssim', 'Fred']]
99
87
  def property_values_to_solr(pairs)
100
- pairs.each_with_object([]) do |(property, value), list|
101
- list << [solr_field(property), value]
88
+ pairs.map do |(property, value)|
89
+ [solr_field(property), value]
102
90
  end
103
91
  end
104
92
 
@@ -109,9 +97,16 @@ module ActiveFedora
109
97
  when ActiveFedora::Reflection::AssociationReflection
110
98
  field.solr_key
111
99
  else
112
- solr_name(field, :symbol)
100
+ ActiveFedora.index_field_mapper.solr_name(field, :symbol)
113
101
  end
114
102
  end
103
+
104
+ # Create a raw query clause suitable for sending to solr as an fq element
105
+ # @param [String] key
106
+ # @param [String] value
107
+ def field_query(key, value)
108
+ "_query_:\"{!field f=#{key}}#{value.gsub('"', '\"')}\""
109
+ end
115
110
  end
116
111
  end
117
112
  end