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
@@ -13,26 +13,97 @@ module ActiveFedora
13
13
  end
14
14
  end
15
15
 
16
- class AttributeMethodCache
17
- def initialize
18
- @module = Module.new
19
- @method_cache = ThreadSafe::Cache.new
16
+ BLACKLISTED_CLASS_METHODS = %w(private public protected allocate new name parent superclass).freeze
17
+
18
+ class GeneratedAttributeMethods < Module; end # :nodoc:
19
+
20
+ module ClassMethods
21
+ def inherited(child_class) #:nodoc:
22
+ child_class.initialize_generated_modules
23
+ super
24
+ end
25
+
26
+ def initialize_generated_modules # :nodoc:
27
+ @generated_attribute_methods = GeneratedAttributeMethods.new { extend Mutex_m }
28
+ @attribute_methods_generated = false
29
+ include @generated_attribute_methods
30
+
31
+ super
20
32
  end
21
33
 
22
- def [](name)
23
- @method_cache.compute_if_absent(name) do
24
- safe_name = name.unpack('h*').first
25
- temp_method = "__temp__#{safe_name}"
26
- ActiveFedora::AttributeMethods::AttrNames.set_name_cache safe_name, name
27
- @module.module_eval method_body(temp_method, safe_name), __FILE__, __LINE__
28
- @module.instance_method temp_method
34
+ # Raises an ActiveRecord::DangerousAttributeError exception when an
35
+ # \Active \Record method is defined in the model, otherwise +false+.
36
+ #
37
+ # class Person < ActiveRecord::Base
38
+ # def save
39
+ # 'already defined by Active Record'
40
+ # end
41
+ # end
42
+ #
43
+ # Person.instance_method_already_implemented?(:save)
44
+ # # => ActiveRecord::DangerousAttributeError: save is defined by Active Record. Check to make sure that you don't have an attribute or method with the same name.
45
+ #
46
+ # Person.instance_method_already_implemented?(:name)
47
+ # # => false
48
+ def instance_method_already_implemented?(method_name)
49
+ if dangerous_attribute_method?(method_name)
50
+ raise DangerousAttributeError, "#{method_name} is defined by Active Record. Check to make sure that you don't have an attribute or method with the same name."
51
+ end
52
+
53
+ if superclass == Base
54
+ super
55
+ else
56
+ # If ThisClass < ... < SomeSuperClass < ... < Base and SomeSuperClass
57
+ # defines its own attribute method, then we don't want to overwrite that.
58
+ defined = method_defined_within?(method_name, superclass, Base) &&
59
+ !superclass.instance_method(method_name).owner.is_a?(GeneratedAttributeMethods)
60
+ defined || super
61
+ end
62
+ end
63
+
64
+ # A method name is 'dangerous' if it is already (re)defined by Active Fedora, but
65
+ # not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
66
+ def dangerous_attribute_method?(name) # :nodoc:
67
+ method_defined_within?(name, Base)
68
+ end
69
+
70
+ def method_defined_within?(name, klass, superklass = klass.superclass) # :nodoc:
71
+ if klass.method_defined?(name) || klass.private_method_defined?(name)
72
+ if superklass.method_defined?(name) || superklass.private_method_defined?(name)
73
+ klass.instance_method(name).owner != superklass.instance_method(name).owner
74
+ else
75
+ true
76
+ end
77
+ else
78
+ false
79
+ end
80
+ end
81
+
82
+ # A class method is 'dangerous' if it is already (re)defined by Active Record, but
83
+ # not by any ancestors. (So 'puts' is not dangerous but 'new' is.)
84
+ def dangerous_class_method?(method_name)
85
+ BLACKLISTED_CLASS_METHODS.include?(method_name.to_s) || class_method_defined_within?(method_name, Base)
86
+ end
87
+
88
+ def class_method_defined_within?(name, klass, superklass = klass.superclass) # :nodoc:
89
+ if klass.respond_to?(name, true)
90
+ if superklass.respond_to?(name, true)
91
+ klass.method(name).owner != superklass.method(name).owner
92
+ else
93
+ true
94
+ end
95
+ else
96
+ false
29
97
  end
30
98
  end
31
99
 
32
100
  private
33
101
 
34
- def method_body
35
- raise NotImplementedError
102
+ # @param name [Symbol] name of the attribute to generate
103
+ def generate_method(name)
104
+ generated_attribute_methods.synchronize do
105
+ define_attribute_methods name
106
+ end
36
107
  end
37
108
  end
38
109
 
@@ -43,6 +114,19 @@ module ActiveFedora
43
114
  include Dirty
44
115
  end
45
116
 
117
+ # Returns +true+ if the given attribute is in the attributes hash, otherwise +false+.
118
+ #
119
+ # class Person < ActiveRecord::Base
120
+ # end
121
+ #
122
+ # person = Person.new
123
+ # person.has_attribute?(:name) # => true
124
+ # person.has_attribute?('age') # => true
125
+ # person.has_attribute?(:nothing) # => false
126
+ def has_attribute?(attr_name)
127
+ attribute_names.include?(attr_name.to_s)
128
+ end
129
+
46
130
  # Returns an array of names for the attributes available on this object.
47
131
  #
48
132
  # class Person < ActiveFedora::Base
@@ -69,6 +153,58 @@ module ActiveFedora
69
153
  end
70
154
  end
71
155
 
156
+ # Returns an <tt>#inspect</tt>-like string for the value of the
157
+ # attribute +attr_name+. String attributes are truncated up to 50
158
+ # characters, Date and Time attributes are returned in the
159
+ # <tt>:db</tt> format, Array attributes are truncated up to 10 values.
160
+ # Other attributes return the value of <tt>#inspect</tt> without
161
+ # modification.
162
+ #
163
+ # person = Person.create!(name: 'David Heinemeier Hansson ' * 3)
164
+ #
165
+ # person.attribute_for_inspect(:name)
166
+ # # => "\"David Heinemeier Hansson David Heinemeier Hansson ...\""
167
+ #
168
+ # person.attribute_for_inspect(:created_at)
169
+ # # => "\"2012-10-22 00:15:07\""
170
+ #
171
+ # person.attribute_for_inspect(:tag_ids)
172
+ # # => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]"
173
+ def attribute_for_inspect(attr_name)
174
+ value = self[attr_name]
175
+
176
+ if value.is_a?(String) && value.length > 50
177
+ "#{value[0, 50]}...".inspect
178
+ elsif value.is_a?(Date) || value.is_a?(Time)
179
+ %("#{value.to_s(:db)}")
180
+ elsif value.is_a?(Array) && value.size > 10
181
+ inspected = value.first(10).inspect
182
+ %(#{inspected[0...-1]}, ...])
183
+ else
184
+ value.inspect
185
+ end
186
+ end
187
+
188
+ # Returns +true+ if the specified +attribute+ has been set by the user or by a
189
+ # database load and is neither +nil+ nor <tt>empty?</tt> (the latter only applies
190
+ # to objects that respond to <tt>empty?</tt>, most notably Strings). Otherwise, +false+.
191
+ # Note that it always returns +true+ with boolean attributes.
192
+ #
193
+ # class Task < ActiveRecord::Base
194
+ # end
195
+ #
196
+ # task = Task.new(title: '', is_done: false)
197
+ # task.attribute_present?(:title) # => false
198
+ # task.attribute_present?(:is_done) # => true
199
+ # task.title = 'Buy milk'
200
+ # task.is_done = true
201
+ # task.attribute_present?(:title) # => true
202
+ # task.attribute_present?(:is_done) # => true
203
+ def attribute_present?(attribute)
204
+ value = self[attribute]
205
+ !value.nil? && !(value.respond_to?(:empty?) && value.empty?)
206
+ end
207
+
72
208
  # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
73
209
  # "2004-12-12" in a date column is cast to a date object, like Date.new(2004, 12, 12)). It raises
74
210
  # <tt>ActiveModel::MissingAttributeError</tt> if the identified attribute is missing.
@@ -103,21 +239,5 @@ module ActiveFedora
103
239
  def []=(attr_name, value)
104
240
  write_attribute(attr_name, value)
105
241
  end
106
-
107
- module ClassMethods
108
- def initialize_generated_modules # :nodoc:
109
- @generated_attribute_methods = Module.new { extend Mutex_m }
110
- include @generated_attribute_methods
111
- end
112
-
113
- private
114
-
115
- # @param name [Symbol] name of the attribute to generate
116
- def generate_method(name)
117
- generated_attribute_methods.synchronize do
118
- define_attribute_methods name
119
- end
120
- end
121
- end
122
242
  end
123
243
  end
@@ -19,12 +19,6 @@ module ActiveFedora
19
19
  end
20
20
  end
21
21
 
22
- def attributes=(properties)
23
- sanitize_for_mass_assignment(properties).each do |k, v|
24
- respond_to?(:"#{k}=") ? send(:"#{k}=", v) : raise(UnknownAttributeError, "#{self.class} does not have an attribute `#{k}'")
25
- end
26
- end
27
-
28
22
  def attribute_names
29
23
  self.class.attribute_names
30
24
  end
@@ -33,13 +27,6 @@ module ActiveFedora
33
27
  attribute_names.each_with_object("id" => id) { |key, hash| hash[key] = self[key] }
34
28
  end
35
29
 
36
- # Calling inspect may trigger a bunch of datastream loads, but it's mainly for debugging, so no worries.
37
- def inspect
38
- values = ["id: #{id.inspect}"]
39
- values << self.class.attribute_names.map { |attr| "#{attr}: #{self[attr].inspect}" }
40
- "#<#{self.class} #{values.flatten.join(', ')}>"
41
- end
42
-
43
30
  def [](key)
44
31
  if assoc = association(key.to_sym)
45
32
  # This is for id attributes stored in the rdf graph.
@@ -93,14 +80,14 @@ module ActiveFedora
93
80
  private
94
81
 
95
82
  def array_reader(field, *args)
96
- raise UnknownAttributeError, "#{self.class} does not have an attribute `#{field}'" unless self.class.delegated_attributes.key?(field)
83
+ raise UnknownAttributeError.new(self, field) unless self.class.delegated_attributes.key?(field)
97
84
 
98
85
  val = self.class.delegated_attributes[field].reader(self, *args)
99
86
  self.class.multiple?(field) ? val : val.first
100
87
  end
101
88
 
102
89
  def array_setter(field, args)
103
- raise UnknownAttributeError, "#{self.class} does not have an attribute `#{field}'" unless self.class.delegated_attributes.key?(field)
90
+ raise UnknownAttributeError.new(self, field) unless self.class.delegated_attributes.key?(field)
104
91
  if self.class.multiple?(field)
105
92
  if args.present? && !args.respond_to?(:each)
106
93
  raise ArgumentError, "You attempted to set the attribute `#{field}' on `#{self.class}' to a scalar value. However, this attribute is declared as being multivalued."
@@ -156,8 +143,8 @@ module ActiveFedora
156
143
  options = fields.pop
157
144
  delegate_target = options.delete(:datastream)
158
145
  raise ArgumentError, "You must provide a datastream to has_attributes" if delegate_target.blank?
159
- Deprecation.warn(Attributes, "has_attributes is deprecated and will be removed in ActiveFedora 10.0. Instead use:\n property #{fields.first.inspect}, delegate_to: '#{delegate_target}', ...")
160
146
 
147
+ Deprecation.warn Attributes, "has_attributes is deprecated and will be removed in ActiveFedora 10.0. Consider using the Form pattern to save all related models or directly delegate the properties and save the target separately"
161
148
  define_delegated_accessor(fields, delegate_target, options, &block)
162
149
  end
163
150
 
@@ -172,7 +159,7 @@ module ActiveFedora
172
159
  # @param [Symbol] field the field to query
173
160
  # @return [Boolean]
174
161
  def multiple?(field)
175
- raise UnknownAttributeError, "#{self} does not have an attribute `#{field}'" unless delegated_attributes.key?(field)
162
+ raise UnknownAttributeError.new(nil, field, self) unless delegated_attributes.key?(field)
176
163
  delegated_attributes[field].multiple
177
164
  end
178
165
 
@@ -180,6 +167,7 @@ module ActiveFedora
180
167
  if properties.key?(:predicate)
181
168
  define_active_triple_accessor(name, properties, &block)
182
169
  elsif properties.key?(:delegate_to)
170
+ Deprecation.warn Attributes, "delegated properties are deprecated and will be removed in ActiveFedora 10.0. Consider using the Form pattern to save all related models or directly delegate the properties and save the target separately"
183
171
  define_delegated_accessor([name], properties.delete(:delegate_to), properties.reverse_merge(multiple: true), &block)
184
172
  else
185
173
  raise "You must provide `:delegate_to' or `:predicate' options to property"
@@ -239,7 +227,7 @@ module ActiveFedora
239
227
  # @param [String] dsid the datastream id
240
228
  # @return [Class] the class of the datastream
241
229
  def datastream_class_for_name(dsid)
242
- reflection = reflect_on_association(dsid.to_sym)
230
+ reflection = _reflect_on_association(dsid.to_sym)
243
231
  reflection ? reflection.klass : ActiveFedora::File
244
232
  end
245
233
 
@@ -75,24 +75,20 @@ module ActiveFedora
75
75
  module AutosaveAssociation
76
76
  extend ActiveSupport::Concern
77
77
 
78
- ASSOCIATION_TYPES = %w( HasMany BelongsTo HasAndBelongsToMany DirectlyContains IndirectlyContains).freeze
78
+ ASSOCIATION_TYPES = [:has_many, :belongs_to, :has_and_belongs_to_many, :directly_contains, :indirectly_contains].freeze
79
79
 
80
80
  module AssociationBuilderExtension #:nodoc:
81
- def self.included(base)
82
- base.valid_options << :autosave
81
+ def self.valid_options
82
+ [:autosave]
83
83
  end
84
84
 
85
- def build
86
- reflection = super
87
- model.send(:add_autosave_association_callbacks, reflection)
88
- reflection
85
+ def self.build(model, reflection)
86
+ model.send(:define_autosave_association_callbacks, reflection) if ASSOCIATION_TYPES.include? reflection.macro
89
87
  end
90
88
  end
91
89
 
92
90
  included do
93
- ASSOCIATION_TYPES.each do |type|
94
- Associations::Builder.const_get(type).send(:include, AssociationBuilderExtension)
95
- end
91
+ Associations::Builder::Association.extensions << AssociationBuilderExtension
96
92
  end
97
93
 
98
94
  module ClassMethods
@@ -127,7 +123,7 @@ module ActiveFedora
127
123
  # the callbacks to get defined multiple times, there are guards that
128
124
  # check if the save or validation methods have already been defined
129
125
  # before actually defining them.
130
- def add_autosave_association_callbacks(reflection)
126
+ def define_autosave_association_callbacks(reflection)
131
127
  save_method = :"autosave_associated_records_for_#{reflection.name}"
132
128
  validation_method = :"validate_associated_records_for_#{reflection.name}"
133
129
  collection = reflection.collection?
@@ -287,7 +283,7 @@ module ActiveFedora
287
283
  end
288
284
 
289
285
  # reconstruct the scope now that we know the owner's id
290
- association.send(:construct_scope) if association.respond_to?(:construct_scope)
286
+ association.send(:reset_scope) if association.respond_to?(:reset_scope)
291
287
  end
292
288
  end
293
289
 
@@ -1,5 +1,3 @@
1
- SOLR_DOCUMENT_ID = "id".freeze unless defined?(SOLR_DOCUMENT_ID) && !SOLR_DOCUMENT_ID.nil?
2
- ENABLE_SOLR_UPDATES = true unless defined?(ENABLE_SOLR_UPDATES)
3
1
  require 'active_support/descendants_tracker'
4
2
  require 'active_fedora/errors'
5
3
  require 'active_fedora/log_subscriber'
@@ -28,8 +26,10 @@ module ActiveFedora
28
26
  extend ActiveSupport::DescendantsTracker
29
27
  extend LdpCache::ClassMethods
30
28
 
29
+ include AttributeAssignment
31
30
  include Core
32
31
  include Identifiable
32
+ include Inheritance
33
33
  include Persistence
34
34
  include Indexing
35
35
  include Scoping
@@ -1,6 +1,6 @@
1
1
  module ActiveFedora
2
2
  class CachingConnection < Ldp::Client
3
- def initialize(host)
3
+ def initialize(host, options = {})
4
4
  super
5
5
  @cache = {}
6
6
  @cache_enabled = false
@@ -227,21 +227,21 @@ module ActiveFedora
227
227
  end
228
228
 
229
229
  def destroy(*) #:nodoc:
230
- run_callbacks(:destroy) { super }
230
+ _run_destroy_callbacks { super }
231
231
  end
232
232
 
233
233
  private
234
234
 
235
- def create_record(*) #:nodoc:
236
- run_callbacks(:create) do
237
- run_callbacks(:save) { super }
238
- end
235
+ def create_or_update(*)
236
+ _run_save_callbacks { super }
239
237
  end
240
238
 
241
- def update_record(*) #:nodoc:
242
- run_callbacks(:update) do
243
- run_callbacks(:save) { super }
244
- end
239
+ def _create_record(*) #:nodoc:
240
+ _run_create_callbacks { super }
241
+ end
242
+
243
+ def _update_record(*) #:nodoc:
244
+ _run_update_callbacks { super }
245
245
  end
246
246
  end
247
247
  end
@@ -0,0 +1,67 @@
1
+ module ActiveFedora
2
+ module Common
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def initialize_generated_modules # :nodoc:
7
+ generated_association_methods
8
+ end
9
+
10
+ def generated_association_methods
11
+ @generated_association_methods ||= begin
12
+ mod = const_set(:GeneratedAssociationMethods, Module.new)
13
+ include mod
14
+ mod
15
+ end
16
+ end
17
+ end
18
+
19
+ def ldp_source
20
+ @ldp_source
21
+ end
22
+
23
+ # Returns true if +comparison_object+ is the same exact object, or +comparison_object+
24
+ # is of the same type and +self+ has an ID and it is equal to +comparison_object.id+.
25
+ #
26
+ # Note that new records are different from any other record by definition, unless the
27
+ # other record is the receiver itself.
28
+ #
29
+ # Note also that destroying a record preserves its ID in the model instance, so deleted
30
+ # models are still comparable.
31
+ def ==(other)
32
+ other.equal?(self) ||
33
+ (other.instance_of?(self.class) &&
34
+ !id.nil? &&
35
+ other.id == id)
36
+ end
37
+
38
+ # Allows sort on objects
39
+ def <=>(other)
40
+ if other.is_a?(self.class)
41
+ to_key <=> other.to_key
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ # Freeze datastreams such that they can be loaded from Fedora, but can't be changed
48
+ def freeze
49
+ @frozen = true
50
+ end
51
+
52
+ def frozen?
53
+ @frozen.present?
54
+ end
55
+
56
+ # Returns +true+ if the record is read only. Records loaded through joins with piggy-back
57
+ # attributes will be marked as read only since they cannot be saved.
58
+ def readonly?
59
+ @readonly
60
+ end
61
+
62
+ # Marks this record as read only.
63
+ def readonly!
64
+ @readonly = true
65
+ end
66
+ end
67
+ end
@@ -2,6 +2,7 @@ module ActiveFedora
2
2
  module Core
3
3
  extend ActiveSupport::Autoload
4
4
  extend ActiveSupport::Concern
5
+ include ActiveFedora::Common
5
6
 
6
7
  autoload :FedoraIdTranslator
7
8
  autoload :FedoraUriTranslator
@@ -13,10 +14,7 @@ module ActiveFedora
13
14
  # Accepts a logger conforming to the interface of Log4r which can be
14
15
  # retrieved on both a class and instance level by calling +logger+.
15
16
  mattr_accessor :logger, instance_writer: false
16
- end
17
-
18
- def ldp_source
19
- @ldp_source
17
+ mattr_accessor :belongs_to_required_by_default, instance_accessor: false
20
18
  end
21
19
 
22
20
  # Constructor. You may supply a custom +:id+, or we call the Fedora Rest API for the
@@ -31,10 +29,10 @@ module ActiveFedora
31
29
  raise IllegalOperation, "Attempting to recreate existing ldp_source: `#{ldp_source.subject}'" unless ldp_source.new?
32
30
  assert_content_model
33
31
  load_attached_files
34
- self.attributes = attributes
32
+ assign_attributes(attributes) if attributes
35
33
 
36
34
  yield self if block_given?
37
- run_callbacks :initialize
35
+ _run_initialize_callbacks
38
36
  end
39
37
 
40
38
  ##
@@ -77,13 +75,6 @@ module ActiveFedora
77
75
  self
78
76
  end
79
77
 
80
- def ==(other)
81
- other.equal?(self) ||
82
- (other.instance_of?(self.class) &&
83
- other.id == id &&
84
- !other.new_record?)
85
- end
86
-
87
78
  def freeze
88
79
  @resource.freeze
89
80
  # @attributes = @attributes.clone.freeze
@@ -93,15 +84,14 @@ module ActiveFedora
93
84
 
94
85
  delegate :frozen?, to: :attached_files
95
86
 
96
- # Returns +true+ if the record is read only. Records loaded through joins with piggy-back
97
- # attributes will be marked as read only since they cannot be saved.
98
- def readonly?
99
- @readonly
100
- end
87
+ # Returns the contents of the record as a nicely formatted string.
88
+ def inspect
89
+ inspection = ["id: #{id.inspect}"]
90
+ inspection += self.class.attribute_names.collect do |name|
91
+ "#{name}: #{attribute_for_inspect(name)}" if has_attribute?(name)
92
+ end
101
93
 
102
- # Marks this record as read only.
103
- def readonly!
104
- @readonly = true
94
+ "#<#{self.class} #{inspection.compact.join(', ')}>"
105
95
  end
106
96
 
107
97
  protected
@@ -113,8 +103,6 @@ module ActiveFedora
113
103
  end
114
104
 
115
105
  module ClassMethods
116
- SLASH = '/'.freeze
117
-
118
106
  def generated_association_methods
119
107
  @generated_association_methods ||= begin
120
108
  mod = const_set(:GeneratedAssociationMethods, Module.new)
@@ -0,0 +1,24 @@
1
+ module ActiveFedora
2
+ # Create model classifiers for resources or solr documents
3
+ class DefaultModelMapper
4
+ attr_reader :classifier_class, :solr_field, :predicate
5
+
6
+ def initialize(classifier_class: ActiveFedora::ModelClassifier, solr_field: ActiveFedora::QueryResultBuilder::HAS_MODEL_SOLR_FIELD, predicate: ActiveFedora::RDF::Fcrepo::Model.hasModel)
7
+ @classifier_class = classifier_class
8
+ @solr_field = solr_field
9
+ @predicate = predicate
10
+ end
11
+
12
+ def classifier(resource)
13
+ models = if resource.respond_to? :graph
14
+ resource.graph.query([nil, predicate, nil]).map { |rg| rg.object.to_s }
15
+ elsif resource.respond_to? :[]
16
+ resource[solr_field] || []
17
+ else
18
+ []
19
+ end
20
+
21
+ classifier_class.new(models)
22
+ end
23
+ end
24
+ end
@@ -9,6 +9,13 @@ module ActiveFedora #:nodoc:
9
9
 
10
10
  # Raised when attempting to access an attribute that has not been defined
11
11
  class UnknownAttributeError < NoMethodError
12
+ attr_reader :record, :attribute
13
+
14
+ def initialize(record, attribute, klass = nil)
15
+ @record = record
16
+ @attribute = attribute
17
+ super("unknown attribute '#{attribute}' for #{klass || @record.class}.")
18
+ end
12
19
  end
13
20
 
14
21
  # Raised when there is an error with the configuration files.
@@ -34,6 +41,22 @@ module ActiveFedora #:nodoc:
34
41
  class AssociationTypeMismatch < ActiveFedoraError
35
42
  end
36
43
 
44
+ class AssociationNotFoundError < ConfigurationError #:nodoc:
45
+ end
46
+
47
+ # This error is raised when trying to destroy a parent instance in N:1 or 1:1 associations
48
+ # (has_many, has_one) when there is at least 1 child associated instance.
49
+ # ex: if @project.tasks.size > 0, DeleteRestrictionError will be raised when trying to destroy @project
50
+ class DeleteRestrictionError < ActiveFedoraError #:nodoc:
51
+ def initialize(name = nil)
52
+ if name
53
+ super("Cannot delete record because of dependent #{name}")
54
+ else
55
+ super("Delete restriction error.")
56
+ end
57
+ end
58
+ end
59
+
37
60
  # Raised when ActiveFedora cannot find the predicate corresponding to the given property
38
61
  # in the predicate registy
39
62
  class UnregisteredPredicateError < ActiveFedoraError
@@ -44,6 +67,25 @@ module ActiveFedora #:nodoc:
44
67
  class RecordNotSaved < ActiveFedoraError
45
68
  end
46
69
 
70
+ # Raised by {ActiveFedora::Base#destroy!}[rdoc-ref:Persistence#destroy!]
71
+ # when a call to {#destroy}[rdoc-ref:Persistence#destroy!]
72
+ # would return false.
73
+ #
74
+ # begin
75
+ # complex_operation_that_internally_calls_destroy!
76
+ # rescue ActiveFedora::RecordNotDestroyed => invalid
77
+ # puts invalid.record.errors
78
+ # end
79
+ #
80
+ class RecordNotDestroyed < ActiveFedoraError
81
+ attr_reader :record
82
+
83
+ def initialize(message = nil, record = nil)
84
+ @record = record
85
+ super(message)
86
+ end
87
+ end
88
+
47
89
  # Raised on attempt to update record that is instantiated as read only.
48
90
  class ReadOnlyRecord < ActiveFedoraError
49
91
  end
@@ -30,7 +30,7 @@ module ActiveFedora
30
30
  # things like ldp:IndirectContainers, but it's imposes a significant
31
31
  # performance penalty on every request
32
32
  # @connection ||= InboundRelationConnection.new(CachingConnection.new(authorized_connection))
33
- @connection ||= CachingConnection.new(authorized_connection)
33
+ @connection ||= CachingConnection.new(authorized_connection, omit_ldpr_interaction_model: true)
34
34
  end
35
35
 
36
36
  def clean_connection