active-fedora 8.7.0 → 9.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +8 -15
  3. data/Gemfile +5 -5
  4. data/History.txt +0 -80
  5. data/README.md +1 -0
  6. data/Rakefile +0 -3
  7. data/active-fedora.gemspec +8 -7
  8. data/config/fedora.yml +5 -4
  9. data/config/predicate_mappings.yml +5 -5
  10. data/gemfiles/rails4.1.gemfile +10 -0
  11. data/gemfiles/rails4.2.beta.gemfile +10 -0
  12. data/lib/active_fedora.rb +151 -117
  13. data/lib/active_fedora/associations.rb +47 -15
  14. data/lib/active_fedora/associations/association.rb +29 -8
  15. data/lib/active_fedora/associations/association_scope.rb +5 -5
  16. data/lib/active_fedora/associations/belongs_to_association.rb +20 -63
  17. data/lib/active_fedora/associations/builder/association.rb +61 -25
  18. data/lib/active_fedora/associations/builder/belongs_to.rb +7 -94
  19. data/lib/active_fedora/associations/builder/collection_association.rb +11 -43
  20. data/lib/active_fedora/associations/builder/contains.rb +28 -0
  21. data/lib/active_fedora/associations/builder/has_and_belongs_to_many.rb +13 -3
  22. data/lib/active_fedora/associations/builder/has_many.rb +16 -10
  23. data/lib/active_fedora/associations/builder/property.rb +14 -0
  24. data/lib/active_fedora/associations/builder/singular_association.rb +14 -18
  25. data/lib/active_fedora/associations/builder/singular_property.rb +12 -0
  26. data/lib/active_fedora/associations/collection_association.rb +57 -80
  27. data/lib/active_fedora/associations/contains_association.rb +50 -0
  28. data/lib/active_fedora/associations/has_and_belongs_to_many_association.rb +44 -24
  29. data/lib/active_fedora/associations/has_many_association.rb +46 -14
  30. data/lib/active_fedora/associations/rdf.rb +86 -0
  31. data/lib/active_fedora/associations/singular_association.rb +4 -8
  32. data/lib/active_fedora/associations/singular_rdf.rb +15 -0
  33. data/lib/active_fedora/attached_files.rb +195 -0
  34. data/lib/active_fedora/attribute_methods.rb +122 -0
  35. data/lib/active_fedora/attribute_methods/dirty.rb +16 -0
  36. data/lib/active_fedora/attribute_methods/read.rb +61 -0
  37. data/lib/active_fedora/attribute_methods/write.rb +47 -0
  38. data/lib/active_fedora/attributes.rb +93 -44
  39. data/lib/active_fedora/attributes/primary_key.rb +12 -0
  40. data/lib/active_fedora/autosave_association.rb +2 -2
  41. data/lib/active_fedora/base.rb +16 -15
  42. data/lib/active_fedora/callbacks.rb +7 -7
  43. data/lib/active_fedora/change_set.rb +34 -0
  44. data/lib/active_fedora/cleaner.rb +62 -0
  45. data/lib/active_fedora/config.rb +4 -22
  46. data/lib/active_fedora/core.rb +173 -99
  47. data/lib/active_fedora/datastream.rb +4 -117
  48. data/lib/active_fedora/datastreams.rb +2 -263
  49. data/lib/active_fedora/datastreams/nokogiri_datastreams.rb +28 -51
  50. data/lib/active_fedora/{datastream_attribute.rb → delegated_attribute.rb} +57 -26
  51. data/lib/active_fedora/errors.rb +61 -0
  52. data/lib/active_fedora/fedora.rb +19 -0
  53. data/lib/active_fedora/fedora_attributes.rb +58 -26
  54. data/lib/active_fedora/file.rb +318 -0
  55. data/lib/active_fedora/file_configurator.rb +32 -32
  56. data/lib/active_fedora/file_path_builder.rb +24 -0
  57. data/lib/active_fedora/files_hash.rb +82 -0
  58. data/lib/active_fedora/fixity_service.rb +40 -0
  59. data/lib/active_fedora/indexing.rb +55 -82
  60. data/lib/active_fedora/indexing_service.rb +70 -0
  61. data/lib/active_fedora/ldp_resource.rb +26 -0
  62. data/lib/active_fedora/loadable_from_json.rb +112 -0
  63. data/lib/active_fedora/model.rb +5 -19
  64. data/lib/active_fedora/nested_attributes.rb +5 -6
  65. data/lib/active_fedora/nom_datastream.rb +15 -25
  66. data/lib/active_fedora/om_datastream.rb +26 -197
  67. data/lib/active_fedora/persistence.rb +95 -71
  68. data/lib/active_fedora/predicates.rb +4 -4
  69. data/lib/active_fedora/qualified_dublin_core_datastream.rb +17 -18
  70. data/lib/active_fedora/querying.rb +3 -4
  71. data/lib/active_fedora/railtie.rb +3 -6
  72. data/lib/active_fedora/rdf.rb +3 -1
  73. data/lib/active_fedora/rdf/datastream_indexing.rb +11 -0
  74. data/lib/active_fedora/rdf/fcrepo.rb +10 -324
  75. data/lib/active_fedora/rdf/indexing.rb +20 -16
  76. data/lib/active_fedora/rdf/ldp.rb +6 -0
  77. data/lib/active_fedora/rdf/ntriples_rdf_datastream.rb +1 -1
  78. data/lib/active_fedora/rdf/persistence.rb +5 -6
  79. data/lib/active_fedora/rdf/rdf_datastream.rb +44 -37
  80. data/lib/active_fedora/rdf/rdfxml_datastream.rb +13 -0
  81. data/lib/active_fedora/rdf/rels_ext.rb +26 -0
  82. data/lib/active_fedora/reflection.rb +256 -199
  83. data/lib/active_fedora/relation.rb +18 -6
  84. data/lib/active_fedora/relation/finder_methods.rb +69 -38
  85. data/lib/active_fedora/relation/query_methods.rb +7 -3
  86. data/lib/active_fedora/rspec_matchers/belong_to_associated_active_fedora_object_matcher.rb +7 -7
  87. data/lib/active_fedora/rspec_matchers/have_many_associated_active_fedora_objects_matcher.rb +8 -8
  88. data/lib/active_fedora/rspec_matchers/have_predicate_matcher.rb +9 -11
  89. data/lib/active_fedora/simple_datastream.rb +18 -13
  90. data/lib/active_fedora/solr_instance_loader.rb +18 -38
  91. data/lib/active_fedora/solr_service.rb +37 -20
  92. data/lib/active_fedora/sparql_insert.rb +45 -0
  93. data/lib/active_fedora/test_support.rb +1 -22
  94. data/lib/active_fedora/version.rb +1 -1
  95. data/lib/active_fedora/versionable.rb +90 -0
  96. data/lib/active_fedora/with_metadata.rb +37 -0
  97. data/lib/active_fedora/with_metadata/metadata_node.rb +70 -0
  98. data/lib/generators/active_fedora/config/config_generator.rb +0 -1
  99. data/lib/generators/active_fedora/config/solr/solr_generator.rb +1 -1
  100. data/lib/generators/active_fedora/model/model_generator.rb +5 -5
  101. data/lib/generators/active_fedora/model/templates/datastream_spec.rb.erb +1 -1
  102. data/lib/generators/active_fedora/model/templates/model_spec.rb.erb +2 -2
  103. data/lib/tasks/active_fedora_dev.rake +21 -27
  104. data/spec/config_helper.rb +1 -1
  105. data/spec/fixtures/mixed_rdf_descMetadata.nt +6 -6
  106. data/spec/fixtures/rails_root/config/predicate_mappings.yml +3 -19
  107. data/spec/fixtures/solr_rdf_descMetadata.nt +6 -6
  108. data/spec/integration/associations_spec.rb +133 -153
  109. data/spec/integration/attached_files_spec.rb +164 -0
  110. data/spec/integration/attributes_spec.rb +73 -12
  111. data/spec/integration/autosave_association_spec.rb +3 -3
  112. data/spec/integration/base_spec.rb +57 -351
  113. data/spec/integration/belongs_to_association_spec.rb +86 -76
  114. data/spec/integration/bug_spec.rb +3 -3
  115. data/spec/integration/collection_association_spec.rb +4 -4
  116. data/spec/integration/complex_rdf_datastream_spec.rb +54 -56
  117. data/spec/integration/delete_all_spec.rb +18 -15
  118. data/spec/integration/eradicate_spec.rb +54 -0
  119. data/spec/integration/fedora_solr_sync_spec.rb +7 -5
  120. data/spec/integration/field_to_solr_name_spec.rb +5 -5
  121. data/spec/integration/file_fixity_spec.rb +40 -0
  122. data/spec/integration/file_spec.rb +122 -0
  123. data/spec/integration/full_featured_model_spec.rb +53 -63
  124. data/spec/integration/has_and_belongs_to_many_associations_spec.rb +141 -114
  125. data/spec/integration/has_many_associations_spec.rb +142 -64
  126. data/spec/integration/json_serialization_spec.rb +50 -8
  127. data/spec/integration/model_spec.rb +12 -29
  128. data/spec/integration/nested_attribute_spec.rb +28 -20
  129. data/spec/integration/ntriples_datastream_spec.rb +60 -57
  130. data/spec/integration/om_datastream_spec.rb +51 -140
  131. data/spec/integration/rdf_nested_attributes_spec.rb +16 -14
  132. data/spec/integration/relation_delegation_spec.rb +7 -9
  133. data/spec/integration/relation_spec.rb +9 -7
  134. data/spec/integration/scoped_query_spec.rb +26 -26
  135. data/spec/integration/solr_instance_loader_spec.rb +69 -53
  136. data/spec/integration/solr_service_spec.rb +12 -73
  137. data/spec/integration/versionable_spec.rb +477 -0
  138. data/spec/integration/with_metadata_spec.rb +52 -0
  139. data/spec/samples/hydra-mods_article_datastream.rb +10 -6
  140. data/spec/samples/models/mods_article.rb +6 -2
  141. data/spec/samples/oral_history_sample.xml +1 -1
  142. data/spec/samples/oral_history_xml.xml +1 -1
  143. data/spec/samples/special_thing.rb +3 -3
  144. data/spec/spec_helper.rb +22 -12
  145. data/spec/support/an_active_model.rb +3 -7
  146. data/spec/unit/active_fedora_spec.rb +20 -17
  147. data/spec/unit/attached_files_spec.rb +203 -0
  148. data/spec/unit/attributes_spec.rb +286 -207
  149. data/spec/unit/base_active_model_spec.rb +8 -8
  150. data/spec/unit/base_datastream_management_spec.rb +11 -24
  151. data/spec/unit/base_extra_spec.rb +17 -67
  152. data/spec/unit/base_spec.rb +163 -428
  153. data/spec/unit/builder/has_and_belongs_to_many_spec.rb +2 -2
  154. data/spec/unit/callback_spec.rb +38 -23
  155. data/spec/unit/change_set_spec.rb +46 -0
  156. data/spec/unit/code_configurator_spec.rb +5 -5
  157. data/spec/unit/config_spec.rb +9 -14
  158. data/spec/unit/core_spec.rb +59 -8
  159. data/spec/unit/file_configurator_spec.rb +55 -53
  160. data/spec/unit/file_path_builder_spec.rb +18 -0
  161. data/spec/unit/file_spec.rb +221 -0
  162. data/spec/unit/files_hash_spec.rb +53 -0
  163. data/spec/unit/fixity_service_spec.rb +34 -0
  164. data/spec/unit/has_and_belongs_to_many_association_spec.rb +134 -0
  165. data/spec/unit/has_many_association_spec.rb +51 -0
  166. data/spec/unit/indexing_service_spec.rb +23 -0
  167. data/spec/unit/indexing_spec.rb +26 -0
  168. data/spec/unit/inheritance_spec.rb +9 -10
  169. data/spec/unit/model_spec.rb +15 -33
  170. data/spec/unit/nom_datastream_spec.rb +13 -10
  171. data/spec/unit/ntriples_datastream_spec.rb +81 -96
  172. data/spec/unit/om_datastream_spec.rb +137 -227
  173. data/spec/unit/persistence_spec.rb +28 -34
  174. data/spec/unit/predicates_spec.rb +29 -29
  175. data/spec/unit/property_spec.rb +1 -3
  176. data/spec/unit/qualified_dublin_core_datastream_spec.rb +27 -32
  177. data/spec/unit/query_spec.rb +116 -149
  178. data/spec/unit/rdf_datastream_spec.rb +25 -43
  179. data/spec/unit/rdf_resource_datastream_spec.rb +24 -123
  180. data/spec/unit/{rdfxml_rdf_datastream_spec.rb → rdfxml_datastream_spec.rb} +21 -25
  181. data/spec/unit/readonly_spec.rb +23 -0
  182. data/spec/unit/rspec_matchers/belong_to_associated_active_fedora_object_matcher_spec.rb +6 -6
  183. data/spec/unit/rspec_matchers/have_many_associated_active_fedora_objects_matcher_spec.rb +6 -6
  184. data/spec/unit/rspec_matchers/have_predicate_matcher_spec.rb +6 -6
  185. data/spec/unit/serializers_spec.rb +1 -1
  186. data/spec/unit/simple_datastream_spec.rb +12 -23
  187. data/spec/unit/solr_config_options_spec.rb +14 -17
  188. data/spec/unit/solr_service_spec.rb +38 -77
  189. data/spec/unit/sparql_insert_spec.rb +32 -0
  190. data/spec/unit/validations_spec.rb +8 -11
  191. metadata +96 -121
  192. data/lib/active_fedora/auditable.rb +0 -9
  193. data/lib/active_fedora/content_model.rb +0 -70
  194. data/lib/active_fedora/datastream_collections.rb +0 -302
  195. data/lib/active_fedora/datastream_hash.rb +0 -35
  196. data/lib/active_fedora/digital_object.rb +0 -55
  197. data/lib/active_fedora/fixture_exporter.rb +0 -33
  198. data/lib/active_fedora/fixture_loader.rb +0 -48
  199. data/lib/active_fedora/rdf/identifiable.rb +0 -66
  200. data/lib/active_fedora/rdf/project_hydra.rb +0 -12
  201. data/lib/active_fedora/rdf/rdfxml_rdf_datastream.rb +0 -13
  202. data/lib/active_fedora/rdf_xml_writer.rb +0 -49
  203. data/lib/active_fedora/relationship_graph.rb +0 -101
  204. data/lib/active_fedora/reload_on_save.rb +0 -16
  205. data/lib/active_fedora/rels_ext_datastream.rb +0 -100
  206. data/lib/active_fedora/rspec_matchers/match_fedora_datastream_matcher.rb +0 -41
  207. data/lib/active_fedora/rubydora_connection.rb +0 -35
  208. data/lib/active_fedora/semantic_node.rb +0 -164
  209. data/lib/active_fedora/service_definitions.rb +0 -88
  210. data/lib/active_fedora/sharding.rb +0 -58
  211. data/lib/active_fedora/solr_digital_object.rb +0 -68
  212. data/lib/active_fedora/unsaved_digital_object.rb +0 -58
  213. data/lib/generators/active_fedora/config/fedora/fedora_generator.rb +0 -12
  214. data/lib/generators/active_fedora/config/fedora/templates/fedora.yml +0 -38
  215. data/lib/generators/active_fedora/config/fedora/templates/fedora_conf/conf/development/fedora.fcfg +0 -953
  216. data/lib/generators/active_fedora/config/fedora/templates/fedora_conf/conf/test/fedora.fcfg +0 -953
  217. data/lib/tasks/active_fedora.rake +0 -83
  218. data/spec/fixtures/sharded_fedora.yml +0 -11
  219. data/spec/integration/auditable_spec.rb +0 -29
  220. data/spec/integration/datastream_collections_spec.rb +0 -127
  221. data/spec/integration/datastream_spec.rb +0 -90
  222. data/spec/integration/datastreams_spec.rb +0 -173
  223. data/spec/integration/load_from_solr_spec.rb +0 -66
  224. data/spec/integration/rels_ext_datastream_spec.rb +0 -82
  225. data/spec/support/mock_fedora.rb +0 -44
  226. data/spec/unit/content_model_spec.rb +0 -86
  227. data/spec/unit/datastream_collections_spec.rb +0 -420
  228. data/spec/unit/datastream_spec.rb +0 -83
  229. data/spec/unit/datastreams_spec.rb +0 -243
  230. data/spec/unit/has_and_belongs_to_many_collection_spec.rb +0 -96
  231. data/spec/unit/has_many_collection_spec.rb +0 -35
  232. data/spec/unit/rdf_vocab_spec.rb +0 -30
  233. data/spec/unit/rdf_xml_writer_spec.rb +0 -63
  234. data/spec/unit/relationship_graph_spec.rb +0 -115
  235. data/spec/unit/reload_on_save_spec.rb +0 -24
  236. data/spec/unit/rels_ext_datastream_spec.rb +0 -170
  237. data/spec/unit/rspec_matchers/match_fedora_datastream_matcher_spec.rb +0 -44
  238. data/spec/unit/rubydora_connection_spec.rb +0 -12
  239. data/spec/unit/semantic_node_spec.rb +0 -112
  240. data/spec/unit/service_definitions_spec.rb +0 -63
  241. data/spec/unit/solr_digital_object_spec.rb +0 -97
  242. data/spec/unit/unsaved_digital_object_spec.rb +0 -48
@@ -2,18 +2,28 @@ require 'active_support/core_ext/module/delegation'
2
2
  require 'active_support/core_ext/object/blank'
3
3
 
4
4
  module ActiveFedora
5
+ class InverseOfAssociationNotFoundError < RuntimeError #:nodoc:
6
+ def initialize(reflection, associated_class = nil)
7
+ super("Could not find the inverse association for #{reflection.name} (#{reflection.options[:inverse_of].inspect} in #{associated_class.nil? ? reflection.class_name : associated_class.name})")
8
+ end
9
+ end
10
+
5
11
  module Associations
12
+
6
13
  extend ActiveSupport::Concern
7
14
 
8
15
  autoload :Association, 'active_fedora/associations/association'
9
16
  autoload :AssociationScope, 'active_fedora/associations/association_scope'
10
17
  autoload :SingularAssociation, 'active_fedora/associations/singular_association'
18
+ autoload :RDF, 'active_fedora/associations/rdf'
19
+ autoload :SingularRDF, 'active_fedora/associations/singular_rdf'
11
20
  autoload :CollectionAssociation, 'active_fedora/associations/collection_association'
12
21
  autoload :CollectionProxy, 'active_fedora/associations/collection_proxy'
13
22
 
14
23
  autoload :HasManyAssociation, 'active_fedora/associations/has_many_association'
15
24
  autoload :BelongsToAssociation, 'active_fedora/associations/belongs_to_association'
16
25
  autoload :HasAndBelongsToManyAssociation, 'active_fedora/associations/has_and_belongs_to_many_association'
26
+ autoload :ContainsAssociation, 'active_fedora/associations/contains_association'
17
27
 
18
28
  module Builder
19
29
  autoload :Association, 'active_fedora/associations/builder/association'
@@ -23,8 +33,11 @@ module ActiveFedora
23
33
  autoload :BelongsTo, 'active_fedora/associations/builder/belongs_to'
24
34
  autoload :HasMany, 'active_fedora/associations/builder/has_many'
25
35
  autoload :HasAndBelongsToMany, 'active_fedora/associations/builder/has_and_belongs_to_many'
26
- end
36
+ autoload :Contains, 'active_fedora/associations/builder/contains'
27
37
 
38
+ autoload :Property, 'active_fedora/associations/builder/property'
39
+ autoload :SingularProperty, 'active_fedora/associations/builder/singular_property'
40
+ end
28
41
 
29
42
  # Clears out the association cache.
30
43
  def clear_association_cache #:nodoc:
@@ -47,13 +60,14 @@ module ActiveFedora
47
60
 
48
61
  association
49
62
  end
50
-
51
-
52
- private
63
+
64
+
65
+ private
53
66
 
54
67
  # Returns the specified association instance if it responds to :loaded?, nil otherwise.
55
68
  def association_instance_get(name)
56
- @association_cache[name.to_sym]
69
+ raise "use a symbol" if name.is_a? String
70
+ @association_cache[name]
57
71
  end
58
72
 
59
73
  # Set the specified association instance.
@@ -67,7 +81,23 @@ module ActiveFedora
67
81
  Builder::HasMany.build(self, name, options)
68
82
  end
69
83
 
70
-
84
+ # This method is used to specify the details of a contained resource.
85
+ # Pass the name as the first argument and a hash of options as the second argument
86
+ # Note that this method doesn't actually execute the block, but stores it, to be executed
87
+ # by any the implementation of the datastream(specified as :class_name)
88
+ #
89
+ # @param [String] :name the handle to refer to this child as
90
+ # @param [Hash] options
91
+ # @option options [Class] :class_name The class that will represent this child, should extend ``ActiveFedora::File''
92
+ # @option options [String] :url
93
+ # @option options [Boolean] :autocreate Always create this datastream on new objects
94
+ # @yield block executed by some types of child resources
95
+ def contains(name, options = {}, &block)
96
+ options[:block] = block if block
97
+ raise ArgumentError, "You must provide a name (dsid) for the datastream" unless name
98
+ Associations::Builder::Contains.build(self, name.to_sym, options)
99
+ end
100
+
71
101
  # Specifies a one-to-one association with another class. This method should only be used
72
102
  # if this class contains the foreign key.
73
103
  #
@@ -86,12 +116,12 @@ module ActiveFedora
86
116
  #
87
117
  # A Post class declares <tt>belongs_to :author</tt>, which will add:
88
118
  # * <tt>Post#author</tt> (similar to <tt>Author.find(author_id)</tt>)
89
- # * <tt>Post#author=(author)</tt>
119
+ # * <tt>Post#author=(author)</tt>
90
120
  # The declaration can also include an options hash to specialize the behavior of the association.
91
121
  #
92
122
  # === Options
93
123
  #
94
- # [:property]
124
+ # [:predicate]
95
125
  # the association predicate to use when storing the association +REQUIRED+
96
126
  # [:class_name]
97
127
  # Specify the class name of the association. Use it only if that name can't be inferred
@@ -99,11 +129,12 @@ module ActiveFedora
99
129
  # if the real class name is Person, you'll have to specify it with this option.
100
130
  #
101
131
  # Option examples:
102
- # belongs_to :firm, :property => :client_of
103
- # belongs_to :author, :class_name => "Person", :property => :author_of
132
+ # belongs_to :firm, predicate: OurVocab.clientOf
133
+ # belongs_to :author, class_name: "Person", predicate: OurVocab.authorOf
104
134
  def belongs_to(name, options = {})
105
- raise "You must specify a property name for #{name}" if !options[:property]
106
135
  Builder::BelongsTo.build(self, name, options)
136
+
137
+ Builder::SingularProperty.build(self, name, options)
107
138
  end
108
139
 
109
140
 
@@ -160,17 +191,18 @@ module ActiveFedora
160
191
  # Specify the class name of the association. Use it only if that name can't be inferred
161
192
  # from the association name. So <tt>has_and_belongs_to_many :projects</tt> will by default be linked to the
162
193
  # Project class, but if the real class name is SuperProject, you'll have to specify it with this option.
163
- # [:property]
194
+ # [:predicate]
164
195
  # <b>REQUIRED</b> Specify the predicate to use when storing the relationship.
165
196
  # [:inverse_of]
166
197
  # Specify the predicate to use when storing the relationship on the foreign object. If it is not provided, the relationship will not set the foriegn association.
167
198
  #
168
199
  # Option examples:
169
- # has_and_belongs_to_many :projects, :property=>:works_on
170
- # has_and_belongs_to_many :nations, :class_name => "Country", :property=>:is_citizen_of
171
- # has_and_belongs_to_many :topics, :property=>:has_topic, :inverse_of=>:is_topic_of
200
+ # has_and_belongs_to_many :projects, predicate: OurVocab.worksOn
201
+ # has_and_belongs_to_many :nations, class_name: "Country", predicate: OurVocab.isCitizenOf
202
+ # has_and_belongs_to_many :topics, predicate: RDF::FOAF.isPrimaryTopicOf, inverse_of: :is_topic_of
172
203
  def has_and_belongs_to_many(name, options = {})
173
204
  Builder::HasAndBelongsToMany.build(self, name, options)
205
+ Builder::Property.build(self, name, options.slice(:class_name, :predicate))
174
206
  end
175
207
  end
176
208
  end
@@ -10,12 +10,12 @@ module ActiveFedora
10
10
  #
11
11
  class Association
12
12
  attr_reader :owner, :target, :reflection
13
+ attr_accessor :inversed
13
14
  delegate :options, :klass, to: :reflection
14
15
 
15
16
  def initialize(owner, reflection)
16
- #reflection.check_validity!
17
+ reflection.check_validity!
17
18
  @owner, @reflection = owner, reflection
18
- @updated = false
19
19
  reset
20
20
  # construct_scope
21
21
  end
@@ -24,6 +24,7 @@ module ActiveFedora
24
24
  def reset
25
25
  @loaded = false
26
26
  @target = nil
27
+ @inversed = false
27
28
  end
28
29
 
29
30
  # Reloads the \target and returns +self+ on success.
@@ -43,6 +44,7 @@ module ActiveFedora
43
44
  def loaded!
44
45
  @loaded = true
45
46
  @stale_state = stale_state
47
+ @inversed = false
46
48
  end
47
49
 
48
50
  # The target is stale if the target no longer points to the record(s) that the
@@ -52,7 +54,7 @@ module ActiveFedora
52
54
  #
53
55
  # Note that if the target has not been loaded, it is not considered stale.
54
56
  def stale_target?
55
- loaded? && @stale_state != stale_state
57
+ !inversed && loaded? && @stale_state != stale_state
56
58
  end
57
59
 
58
60
  # Sets the target of this proxy to <tt>\target</tt>, and the \loaded flag to +true+.
@@ -76,16 +78,21 @@ module ActiveFedora
76
78
  @association_scope ||= AssociationScope.new(self).scope
77
79
  end
78
80
  end
79
-
81
+
80
82
  # Set the inverse association, if possible
81
83
  def set_inverse_instance(record)
82
84
  if record && invertible_for?(record)
83
- inverse = record.association(inverse_reflection_for(record).name)
84
- inverse.target = owner
85
+ inverse = record.association(inverse_reflection_for(record).name.to_sym)
86
+ if inverse.is_a? ActiveFedora::Associations::HasAndBelongsToManyAssociation
87
+ inverse.target << owner
88
+ else
89
+ inverse.target = owner
90
+ end
91
+ inverse.inversed = true
85
92
  end
86
93
  end
87
94
 
88
-
95
+
89
96
  # Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
90
97
  # through association's scope)
91
98
  def target_scope
@@ -108,9 +115,16 @@ module ActiveFedora
108
115
  target
109
116
  end
110
117
 
118
+ def initialize_attributes(record) #:nodoc:
119
+ skip_assign = [reflection.foreign_key].compact
120
+ attributes = create_scope.except(*(record.changed - skip_assign))
121
+ record.attributes = attributes
122
+ set_inverse_instance(record)
123
+ end
124
+
111
125
  private
112
126
 
113
-
127
+
114
128
  def find_target?
115
129
  !loaded? && (!owner.new_record? || foreign_key_present?) && klass
116
130
  end
@@ -158,6 +172,13 @@ module ActiveFedora
158
172
  def stale_state
159
173
  end
160
174
 
175
+ def build_record(attributes)
176
+ # TODO make initalize take a block and get rid of the tap
177
+ reflection.build_association(attributes).tap do |record|
178
+ initialize_attributes(record)
179
+ end
180
+ end
181
+
161
182
  end
162
183
  end
163
184
  end
@@ -3,7 +3,7 @@ module ActiveFedora
3
3
  class AssociationScope #:nodoc:
4
4
 
5
5
  attr_reader :association
6
-
6
+
7
7
  delegate :klass, :owner, :reflection, :interpolate, :to => :association
8
8
  delegate :chain, :scope_chain, :options, :source_options, :active_record, :to => :reflection
9
9
 
@@ -11,29 +11,29 @@ module ActiveFedora
11
11
  @association = association
12
12
  end
13
13
 
14
-
15
14
  def scope
16
15
  scope = klass.unscoped
17
16
  add_constraints(scope)
18
17
  end
19
18
 
20
19
  private
20
+
21
21
  def add_constraints(scope)
22
22
  chain.each_with_index do |reflection, i|
23
23
  if reflection.source_macro == :belongs_to
24
+ # Create a partial solr query using the ids. We may add additional filters such as class_name later
25
+ scope = scope.where( ActiveFedora::SolrService.construct_query_for_ids([owner[reflection.foreign_key]]))
24
26
  elsif reflection.source_macro == :has_and_belongs_to_many
25
27
  else
26
- scope = scope.where( ActiveFedora::SolrService.construct_query_for_rel(association.send(:find_predicate) => owner.internal_uri))
28
+ scope = scope.where( ActiveFedora::SolrService.construct_query_for_rel(association.send(:find_predicate) => owner.id))
27
29
  end
28
30
 
29
-
30
31
  is_first_chain = i == 0
31
32
  klass = is_first_chain ? self.klass : reflection.klass
32
33
  end
33
34
 
34
35
  scope
35
36
  end
36
-
37
37
  end
38
38
  end
39
39
  end
@@ -2,49 +2,19 @@ module ActiveFedora
2
2
  module Associations
3
3
  class BelongsToAssociation < SingularAssociation #:nodoc:
4
4
 
5
- def id_writer(id)
6
- @full_result = nil
7
- remove_matching_property_relationship
8
- return if id.blank? or id == ActiveFedora::UnsavedDigitalObject::PLACEHOLDER
9
- @owner.add_relationship(@reflection.options[:property], ActiveFedora::Base.internal_uri(id))
10
- end
11
-
12
- def id_reader
13
- begin
14
- # need to find the id with the correct class
15
- ids = @owner.ids_for_outbound(@reflection.options[:property])
16
-
17
- return if ids.empty?
18
-
19
- # This incurs a lot of overhead, but it's necessary if the users use one property for more than one association.
20
- # e.g.
21
- # belongs_to :author, property: :has_member, class_name: 'Person'
22
- # belongs_to :publisher, property: :has_member
23
-
24
- if @owner.reflections.one? { |k, v| v.options[:property] == @reflection.options[:property] }
25
- @full_results = nil
26
- ids.first
27
- else
28
- @full_results = SolrService.query(construct_query(ids))
29
- @full_results.first['id'] if @full_results.present?
30
- end
31
- end
32
- end
33
-
34
5
  def replace(record)
35
- if record.nil?
36
- id_writer(nil)
37
- else
6
+ if record
38
7
  raise_on_type_mismatch(record)
39
- id_writer(record.id)
8
+ # update_counters(record)
9
+ replace_keys(record)
10
+ set_inverse_instance(record)
40
11
  @updated = true
41
-
42
- #= (AssociationProxy === record ? record.target : record)
12
+ else
13
+ # decrement_counters
14
+ remove_keys
43
15
  end
44
- self.target = record
45
16
 
46
- loaded!
47
- record
17
+ self.target = record
48
18
  end
49
19
 
50
20
  def reset
@@ -57,18 +27,13 @@ module ActiveFedora
57
27
  end
58
28
 
59
29
  private
60
- def find_target
61
- return unless id_reader
62
30
 
63
- if @full_result # side-effect from #id_reader
64
- ActiveFedora::SolrService.reify_solr_results([@full_result]).first
65
- else
66
- begin
67
- ActiveFedora::Base.find(id_reader, cast: true)
68
- rescue ActiveFedora::ObjectNotFoundError
69
- nil
70
- end
71
- end
31
+ def replace_keys(record)
32
+ owner[reflection.foreign_key] = record.id
33
+ end
34
+
35
+ def remove_keys
36
+ owner[reflection.foreign_key] = nil
72
37
  end
73
38
 
74
39
  # Constructs a query that checks solr for the correct id & class_name combination
@@ -77,27 +42,19 @@ module ActiveFedora
77
42
  candidate_classes = klass.descendants.select {|d| d.name }
78
43
  candidate_classes += [klass] unless klass == ActiveFedora::Base
79
44
  model_pairs = candidate_classes.inject([]) { |arr, klass| arr << [:has_model, klass.to_class_uri]; arr }
80
- '(' + ActiveFedora::SolrService.construct_query_for_pids(ids) + ') AND (' +
45
+ '(' + ActiveFedora::SolrService.construct_query_for_ids(ids) + ') AND (' +
81
46
  ActiveFedora::SolrService.construct_query_for_rel(model_pairs, 'OR') + ')'
82
47
  end
83
48
 
84
- def remove_matching_property_relationship
85
- ids = @owner.ids_for_outbound(@reflection.options[:property])
86
- return if ids.empty?
87
- ids.each do |id|
88
- result = SolrService.query(ActiveFedora::SolrService.construct_query_for_pids([id]))
89
- hit = ActiveFedora::SolrService.reify_solr_results(result).first
90
- # We remove_relationship on subjects that match the same class, or if the subject is nil
91
- if hit.class == klass || hit.nil?
92
- @owner.remove_relationship(@reflection.options[:property], hit)
93
- end
94
- end
95
- end
96
-
97
49
  def foreign_key_present?
98
50
  owner[reflection.foreign_key]
99
51
  end
100
52
 
53
+ # belongs_to is not invertible (unless we implement has_one, then make an exception here)
54
+ def invertible_for?(_)
55
+ false
56
+ end
57
+
101
58
  def stale_state
102
59
  owner[reflection.foreign_key]
103
60
  end
@@ -1,55 +1,91 @@
1
1
  module ActiveFedora::Associations::Builder
2
2
  class Association #:nodoc:
3
3
  class_attribute :valid_options
4
- self.valid_options = [:class_name, :property]
4
+ self.valid_options = [:class_name, :predicate]
5
5
 
6
6
  # Set by subclasses
7
7
  class_attribute :macro
8
8
 
9
- attr_reader :model, :name, :options, :reflection, :mixin
9
+ attr_reader :model, :name, :options, :mixin
10
10
 
11
+ #configure_dependency
11
12
  def self.build(model, name, options)
12
- new(model, name, options).build
13
+ reflection = new(model, name, options).build
14
+ define_accessors(model, reflection)
15
+ define_callbacks(model, reflection)
16
+ reflection
13
17
  end
14
18
 
15
19
  def initialize(model, name, options)
16
20
  @model, @name, @options = model, name, options
17
- @mixin = Module.new
18
- @model.__send__(:include, @mixin)
21
+ translate_property_to_predicate
22
+ validate_options
19
23
  end
20
24
 
21
25
  def build
22
- validate_options
23
- reflection = model.create_reflection(self.class.macro, name, options, model)
24
- define_accessors
25
- reflection
26
+ configure_dependency if options[:dependent] # see https://github.com/rails/rails/commit/9da52a5e55cc665a539afb45783f84d9f3607282
27
+ model.create_reflection(self.class.macro, name, options, model)
28
+ end
29
+
30
+ def translate_property_to_predicate
31
+ return unless options[:property]
32
+ Deprecation.warn Association, "the :property option to `#{model}.#{macro} :#{name}' is deprecated and will be removed in active-fedora 10.0. Use :predicate instead", caller(5)
33
+ options[:predicate] = predicate(options.delete(:property))
26
34
  end
27
35
 
28
- private
36
+ def validate_options
37
+ options.assert_valid_keys(self.class.valid_options)
38
+ end
29
39
 
30
- def validate_options
31
- options.assert_valid_keys(self.class.valid_options)
32
- end
33
40
 
34
- def define_accessors
35
- define_readers
36
- define_writers
41
+ # Returns the RDF predicate as defined by the :property attribute
42
+ def predicate(property)
43
+ return property if property.kind_of? RDF::URI
44
+ ActiveFedora::Predicates.find_graph_predicate(property)
45
+ end
46
+
47
+ def self.define_callbacks(model, reflection)
48
+ if dependent = reflection.options[:dependent]
49
+ check_dependent_options(dependent)
50
+ add_destroy_callbacks(model, reflection)
37
51
  end
52
+ end
38
53
 
39
- def define_readers
40
- name = self.name
54
+ def self.define_accessors(model, reflection)
55
+ mixin = model.generated_association_methods
56
+ name = reflection.name
57
+ define_readers(mixin, name)
58
+ define_writers(mixin, name)
59
+ end
41
60
 
42
- mixin.send(:define_method, name) do |*params|
43
- association(name).reader(*params)
44
- end
61
+ def self.define_readers(mixin, name)
62
+ mixin.send(:define_method, name) do |*params|
63
+ association(name).reader(*params)
45
64
  end
65
+ end
46
66
 
47
- def define_writers
48
- name = self.name
67
+ def self.define_writers(mixin, name)
68
+ mixin.send(:define_method, "#{name}=") do |value|
69
+ association(name).writer(value)
70
+ end
71
+ end
49
72
 
50
- mixin.send(:define_method, "#{name}=") do |value|
51
- association(name).writer(value)
73
+ def configure_dependency
74
+ if options[:dependent]
75
+ unless [:destroy, :delete].include?(options[:dependent])
76
+ raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{options[:dependent].inspect})"
52
77
  end
78
+
79
+ method_name = "belongs_to_dependent_#{options[:dependent]}_for_#{name}"
80
+ model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
81
+ def #{method_name}
82
+ association = #{name}
83
+ association.#{options[:dependent]} if association
84
+ end
85
+ eoruby
86
+ model.after_destroy method_name
53
87
  end
88
+ end
89
+
54
90
  end
55
91
  end