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
@@ -3,9 +3,9 @@ require 'spec_helper'
3
3
  describe ActiveFedora::Base do
4
4
  describe "with inverse" do
5
5
  before do
6
- class Book < ActiveFedora::Base
7
- has_and_belongs_to_many :topics, :property=>:has_topic, :inverse_of=>:is_topic_of
8
- has_and_belongs_to_many :collections, :property=>:is_member_of_collection
6
+ class Book < ActiveFedora::Base
7
+ has_and_belongs_to_many :topics, predicate: ::RDF::FOAF.primaryTopic, inverse_of: :books
8
+ has_and_belongs_to_many :collections, predicate: ActiveFedora::RDF::RelsExt.isMemberOfCollection
9
9
  end
10
10
 
11
11
  class SpecialInheritedBook < Book
@@ -15,8 +15,8 @@ describe ActiveFedora::Base do
15
15
  class Collection < ActiveFedora::Base
16
16
  end
17
17
 
18
- class Topic < ActiveFedora::Base
19
- has_and_belongs_to_many :books, :property=>:is_topic_of
18
+ class Topic < ActiveFedora::Base
19
+ has_and_belongs_to_many :books, predicate: ::RDF::FOAF.isPrimaryTopicOf
20
20
  end
21
21
  end
22
22
 
@@ -28,91 +28,96 @@ describe ActiveFedora::Base do
28
28
  end
29
29
 
30
30
  describe "an unsaved instance" do
31
+ let(:topic1) { Topic.create }
31
32
  before do
32
- @topic1 = Topic.create
33
- @topic2 = Topic.create
34
33
  @book = Book.create
35
- @special_book = SpecialInheritedBook.create #TODO this isnt' needed in every test.
36
34
  end
35
+
37
36
  it "habtm should set and remove relationships bidirectionally" do
38
- @book.topics << @topic1
39
- expect(@book.topics).to eq([@topic1])
40
- expect(@topic1.books).to eq([@book])
41
- expect(@topic1.reload.books).to eq([@book])
37
+ @book.topics << topic1
38
+ expect(@book.topics).to eq [topic1]
39
+ expect(topic1.books).to eq [@book]
40
+ expect(topic1.reload.books).to eq [@book]
42
41
 
43
- @book.topics.delete(@topic1)
44
- #@topic1.books.delete(@book)
45
- expect(@book.topics).to eq([])
46
- expect(@topic1.books).to eq([])
42
+ @book.topics.delete(topic1)
43
+ expect(@book.topics).to be_empty
44
+ expect(topic1.books).to be_empty
47
45
  end
48
- it "Should allow for more than 10 items" do
49
46
 
50
- (0..11).each do
47
+ it "Should allow for more than 10 items" do
48
+ (1..12).each do
51
49
  @book.topics << Topic.create
52
50
  end
53
51
  @book.save
54
- expect(@book.topics.count).to eq(12)
55
- book2 = Book.find(@book.pid)
56
- expect(book2.topics.count).to eq(12)
57
- end
58
-
59
- it "Should find inherited objects along with base objects" do
60
- @book.topics << @topic1
61
- @special_book.topics << @topic1
62
- expect(@topic1.books).to eq([@book, @special_book])
63
- expect(@topic1.reload.books).to eq([@book, @special_book])
52
+ expect(@book.topics.count).to eq 12
53
+ book2 = Book.find(@book.id)
54
+ expect(book2.topics.count).to eq 12
55
+ end
56
+
57
+ context "with subclassed objects" do
58
+ let!(:special_book) { SpecialInheritedBook.create }
59
+ it "Should find inherited objects along with base objects" do
60
+ @book.topics << topic1
61
+ special_book.topics << topic1
62
+ expect(topic1.books).to eq [@book, special_book]
63
+ expect(topic1.reload.books).to eq [@book, special_book]
64
+ end
64
65
  end
65
66
 
66
67
  it "Should cast found books to the correct cmodel" do
67
- @topic1.books[0].class == Book
68
- @topic1.books[1].class == SpecialInheritedBook
68
+ topic1.books[0].class == Book
69
+ topic1.books[1].class == SpecialInheritedBook
69
70
  end
70
71
 
71
- after do
72
- @topic1.delete
73
- @topic2.delete
74
- @book.delete
75
- @special_book.delete
76
- end
77
72
  end
73
+
78
74
  describe "a saved instance" do
79
- before do
80
- @book = Book.create
81
- @topic1 = Topic.create
82
- @topic2 = Topic.create
83
- end
75
+ let!(:book) { Book.create }
76
+ let!(:topic1) { Topic.create }
77
+ let!(:topic2) { Topic.create }
78
+
84
79
  it "should set relationships bidirectionally" do
85
- @book.topics << @topic1
86
- expect(@book.topics).to eq([@topic1])
87
- expect(@book.relationships(:has_topic)).to eq([@topic1.internal_uri])
88
- expect(@topic1.relationships(:has_topic)).to eq([])
89
- expect(@topic1.relationships(:is_topic_of)).to eq([@book.internal_uri])
90
- expect(Topic.find(@topic1.pid).books).to eq([@book]) #Can't have saved it because @book isn't saved yet.
80
+ book.topics << topic1
81
+ expect(book.topics).to eq [topic1]
82
+ expect(book['topic_ids']).to eq [topic1.id]
83
+ expect(topic1['book_ids']).to eq [book.id]
84
+ expect(Topic.find(topic1.id).books).to eq [book] #Can't have saved it because book isn't saved yet.
91
85
  end
86
+
92
87
  it "should save new child objects" do
93
- @book.topics << Topic.new
94
- expect(@book.topics.first.pid).not_to be_nil
88
+ book.topics << Topic.new
89
+ expect(book.topics.first.id).to_not be_nil
95
90
  end
91
+
96
92
  it "should clear out the old associtions" do
97
- @book.topics = [@topic1]
98
- @book.topics = [@topic2]
99
- expect(@book.topic_ids).to eq([@topic2.pid])
93
+ book.topics = [topic1]
94
+ book.topics = [topic2]
95
+ expect(book.topic_ids).to eq [topic2.id]
100
96
  end
101
- after do
102
- @book.delete
103
- @topic1.delete
104
- @topic2.delete
97
+
98
+ context "with members" do
99
+ before do
100
+ book.topics = [topic1, topic2]
101
+ book.save
102
+ end
103
+
104
+ context "destroy" do
105
+ it "should remove the associations" do
106
+ book.destroy
107
+ end
108
+ end
105
109
  end
106
110
  end
107
111
  end
108
112
 
109
113
  describe "when inverse is not specified" do
110
114
  before do
111
- class Book < ActiveFedora::Base
112
- has_and_belongs_to_many :collections, :property=>:is_member_of_collection
115
+ class Book < ActiveFedora::Base
116
+ has_and_belongs_to_many :collections, predicate: ActiveFedora::RDF::RelsExt.isMemberOfCollection
113
117
  end
114
118
 
115
119
  class Collection < ActiveFedora::Base
120
+ has_and_belongs_to_many :books, predicate: ActiveFedora::RDF::RelsExt.isMemberOfCollection
116
121
  end
117
122
  end
118
123
 
@@ -129,33 +134,29 @@ describe ActiveFedora::Base do
129
134
  book.collections << collection
130
135
  book.save!
131
136
  end
132
- after do
133
- collection.delete
134
- book.delete
135
- end
136
137
 
137
138
  it "should have a collection" do
138
- expect(book.relationships(:is_member_of_collection)).to eq([collection.internal_uri])
139
- expect(book.collections).to eq([collection])
139
+ expect(book['collection_ids']).to eq [collection.id]
140
+ expect(book.collections).to eq [collection]
140
141
  end
141
142
  it "habtm should not set foreign relationships if :inverse_of is not specified" do
142
- expect(collection.relationships(:is_member_of_collection)).to eq([])
143
+ expect(collection['book_ids']).to be_empty
143
144
  end
144
145
  it "should load the collections" do
145
- reloaded = Book.find(book.pid)
146
- expect(reloaded.collections).to eq([collection])
146
+ reloaded = Book.find(book.id)
147
+ expect(reloaded.collections).to eq [collection]
147
148
  end
148
149
 
149
150
  describe "#empty?" do
150
151
  subject { book.collections }
151
- its(:empty?) { is_expected.to be_falsey }
152
+ it { should_not be_empty }
152
153
  end
153
154
  end
154
155
 
155
- context "when a book isn't in a collection" do
156
+ context "when a book isn't in a collection" do
156
157
  describe "#empty?" do
157
158
  subject { book.collections }
158
- its(:empty?) { should be_truthy }
159
+ it { should be_empty }
159
160
  end
160
161
  end
161
162
  end
@@ -164,8 +165,8 @@ describe ActiveFedora::Base do
164
165
  describe "when destroying the association" do
165
166
  describe "without callbacks" do
166
167
  before do
167
- class Book < ActiveFedora::Base
168
- has_and_belongs_to_many :collections, property: :is_member_of_collection
168
+ class Book < ActiveFedora::Base
169
+ has_and_belongs_to_many :collections, predicate: ActiveFedora::RDF::RelsExt.isMemberOfCollection
169
170
  end
170
171
 
171
172
  class Collection < ActiveFedora::Base
@@ -184,38 +185,33 @@ describe ActiveFedora::Base do
184
185
  book.collections << collection1 << collection2
185
186
  book.save!
186
187
  end
187
- after do
188
- collection1.delete
189
- collection2.delete
190
- book.delete
191
- end
192
188
 
193
189
  it "delete should cause the entries to be removed from RELS-EXT, but not destroy the original record" do
194
- expect(book.collections).to eq([collection1, collection2])
190
+ expect(book.collections).to eq [collection1, collection2]
195
191
  book.collections.delete(collection1)
196
- expect(book.collections).to eq([collection2])
192
+ expect(book.collections).to eq [collection2]
197
193
  book.save!
198
194
  book.reload
199
- expect(book.collections).to eq([collection2])
200
- expect(Collection.find(collection1.pid)).to_not be_nil
195
+ expect(book.collections).to eq [collection2]
196
+ expect(Collection.find(collection1.id)).to_not be_nil
201
197
  end
202
198
 
203
199
  it "destroy should cause the entries to be removed from RELS-EXT, but not destroy the original record" do
204
- expect(book.collections).to eq([collection1, collection2])
200
+ expect(book.collections).to eq [collection1, collection2]
205
201
  book.collections.destroy(collection1)
206
- expect(book.collections).to eq([collection2])
202
+ expect(book.collections).to eq [collection2]
207
203
  book.save!
208
204
  book.reload
209
- expect(book.collections).to eq([collection2])
210
- expect(Collection.find(collection1.pid)).to_not be_nil
205
+ expect(book.collections).to eq [collection2]
206
+ expect(Collection.find(collection1.id)).to_not be_nil
211
207
  end
212
208
  end
213
209
 
214
210
  describe "with remove callbacks" do
215
211
  before do
216
- class Book < ActiveFedora::Base
217
- has_and_belongs_to_many :collections,
218
- property: :is_member_of_collection,
212
+ class Book < ActiveFedora::Base
213
+ has_and_belongs_to_many :collections,
214
+ predicate: ActiveFedora::RDF::RelsExt.isMemberOfCollection,
219
215
  before_remove: :foo, after_remove: :bar
220
216
  end
221
217
 
@@ -234,10 +230,6 @@ describe ActiveFedora::Base do
234
230
  book.collections << collection
235
231
  book.save!
236
232
  end
237
- after do
238
- collection.delete
239
- book.delete
240
- end
241
233
 
242
234
  it "destroy should cause the before_remove and after_remove callback to be triggered" do
243
235
  expect(book).to receive(:foo).with(collection)
@@ -253,20 +245,20 @@ describe ActiveFedora::Base do
253
245
 
254
246
  it "should not remove if an exception is thrown in before_remove" do
255
247
  expect(book).to receive(:foo).with(collection).and_raise
256
- expect(book).not_to receive(:bar)
248
+ expect(book).to_not receive(:bar)
257
249
  begin
258
250
  book.collections.delete(collection)
259
251
  rescue RuntimeError
260
252
  end
261
- expect(book.collections).to eq([collection])
253
+ expect(book.collections).to eq [collection]
262
254
  end
263
255
  end
264
256
 
265
257
  describe "with add callbacks" do
266
258
  before do
267
- class Book < ActiveFedora::Base
268
- has_and_belongs_to_many :collections,
269
- property: :is_member_of_collection,
259
+ class Book < ActiveFedora::Base
260
+ has_and_belongs_to_many :collections,
261
+ predicate: ActiveFedora::RDF::RelsExt.isMemberOfCollection,
270
262
  before_add: :foo, after_add: :bar
271
263
  end
272
264
 
@@ -279,12 +271,8 @@ describe ActiveFedora::Base do
279
271
  Object.send(:remove_const, :Collection)
280
272
  end
281
273
 
282
- let (:book) { Book.create }
283
- let (:collection) { Collection.create }
284
- after do
285
- collection.delete
286
- book.delete
287
- end
274
+ let(:book) { Book.create }
275
+ let(:collection) { Collection.create }
288
276
 
289
277
  it "shift should cause the before_add and after_add callback to be triggered" do
290
278
  expect(book).to receive(:foo).with(collection)
@@ -300,19 +288,45 @@ describe ActiveFedora::Base do
300
288
 
301
289
  it "should not add if an exception is thrown in before_add" do
302
290
  expect(book).to receive(:foo).with(collection).and_raise
303
- expect(book).not_to receive(:bar)
291
+ expect(book).to_not receive(:bar)
304
292
  begin
305
293
  book.collections << collection
306
294
  rescue RuntimeError
307
295
  end
308
- expect(book.collections).to eq([])
296
+ expect(book.collections).to eq []
309
297
  end
310
298
  end
299
+ end
300
+ end
311
301
 
302
+ describe "create" do
303
+ before do
304
+ class Book < ActiveFedora::Base
305
+ has_and_belongs_to_many :collections, predicate: ActiveFedora::RDF::RelsExt.isMemberOfCollection
306
+ end
307
+
308
+ class Collection < ActiveFedora::Base
309
+ property :title, predicate: ::RDF::DC.title
310
+ end
311
+ end
312
+
313
+ after do
314
+ Object.send(:remove_const, :Book)
315
+ Object.send(:remove_const, :Collection)
312
316
  end
313
317
 
318
+ let(:book) { Book.create }
319
+
320
+ it "should create" do
321
+ collection = book.collections.create(title: ["Permanent"])
322
+ expect(collection).to be_kind_of Collection
323
+ expect(book.collections).to include collection
324
+ book.save
325
+ expect(book.reload.collections.first.title).to eq ['Permanent']
326
+ end
314
327
  end
315
328
 
329
+
316
330
  describe "Autosave" do
317
331
  before do
318
332
  class Item < ActiveFedora::Base
@@ -322,8 +336,9 @@ describe "Autosave" do
322
336
  end
323
337
  has_attributes :title, datastream: 'foo'
324
338
  end
339
+
325
340
  class Component < ActiveFedora::Base
326
- has_and_belongs_to_many :items, :property => :is_part_of
341
+ has_and_belongs_to_many :items, predicate: ActiveFedora::RDF::RelsExt.isPartOf
327
342
  has_metadata "foo", type: ActiveFedora::SimpleDatastream do |m|
328
343
  m.field "description", :string
329
344
  end
@@ -332,25 +347,37 @@ describe "Autosave" do
332
347
  end
333
348
 
334
349
  after do
335
- Object.send(:remove_const, :Item)
336
- Object.send(:remove_const, :Component)
350
+ Object.send(:remove_const, :Item)
351
+ Object.send(:remove_const, :Component)
337
352
  end
338
353
 
339
354
  describe "From the has_and_belongs_to_many side" do
340
- let(:component) { Component.create(items: [Item.new(title: 'my title')]) }
355
+ describe "dependent records" do
356
+ let(:component) { Component.create(items: [Item.new(title: 'my title')]) }
341
357
 
342
- it "should save dependent records" do
343
- component.reload
344
- expect(component.items.first.title).to eq('my title')
358
+ it "should be saved" do
359
+ component.reload
360
+ expect(component.items.first.title).to eq 'my title'
361
+ end
362
+ end
363
+
364
+ describe "shifting" do
365
+ let(:component) { Component.new }
366
+ let(:item) { Item.create }
367
+
368
+ it "should set item_ids" do
369
+ component.items << item
370
+ expect(component.item_ids).to eq [item.id]
371
+ end
345
372
  end
346
373
  end
374
+
347
375
  describe "From the has_many side" do
348
376
  let(:item) { Item.create(components: [Component.new(description: 'my description')]) }
349
377
 
350
378
  it "should save dependent records" do
351
379
  item.reload
352
- expect(item.components.first.description).to eq('my description')
380
+ expect(item.components.first.description).to eq 'my description'
353
381
  end
354
382
  end
355
-
356
383
  end
@@ -2,18 +2,19 @@ require 'spec_helper'
2
2
 
3
3
  describe "Collection members" do
4
4
  before :all do
5
- class Library < ActiveFedora::Base
5
+ class Library < ActiveFedora::Base
6
6
  has_many :books
7
7
  end
8
8
 
9
- class Book < ActiveFedora::Base
10
- belongs_to :library, property: :has_constituent
9
+ class Book < ActiveFedora::Base
10
+ belongs_to :library, predicate: ActiveFedora::RDF::RelsExt.hasConstituent
11
11
  end
12
12
  end
13
13
  after :all do
14
14
  Object.send(:remove_const, :Book)
15
15
  Object.send(:remove_const, :Library)
16
16
  end
17
+
17
18
  describe "size of has_many" do
18
19
  let(:library) { Library.create }
19
20
  context "when no members" do
@@ -21,10 +22,11 @@ describe "Collection members" do
21
22
  expect(library.books).not_to be_loaded
22
23
  expect(library.books.size).to eq(0)
23
24
  expect(library.books).to be_loaded
24
- expect(library.books.any?).to be_falsey
25
+ expect(library.books.any?).to be false
25
26
  end
26
27
  end
27
28
  end
29
+
28
30
  describe "looking up has_many" do
29
31
  let(:book) { Book.create }
30
32
  let(:library) { Library.create() }
@@ -33,40 +35,41 @@ describe "Collection members" do
33
35
  library.save!
34
36
  library.reload
35
37
  end
38
+
36
39
  it "should read book_ids from solr" do
37
- expect(library.book_ids).to eq [book.pid]
40
+ expect(library.book_ids).to eq [book.id]
38
41
  end
39
42
  it "should read books from solr" do
40
43
  expect(library.books).to eq [book]
41
44
  end
42
45
 
43
46
  it "should cache the results" do
44
- expect(library.books.loaded?).to be_falsey
47
+ expect(library.books).to_not be_loaded
45
48
  expect(library.books).to eq [book]
46
- expect(library.books.loaded?).to be_truthy
49
+ expect(library.books).to be_loaded
47
50
  end
48
51
  it "should load from solr" do
49
- expect(library.books.load_from_solr.map {|r| r["id"]}).to eq([book.pid])
52
+ expect(library.books.load_from_solr.map {|r| r["id"]}).to eq([book.id])
50
53
  end
51
54
  it "should load from solr with options" do
52
55
  expect(library.books.load_from_solr(rows: 0).size).to eq(0)
53
56
  end
54
57
  it "should respond to #any?" do
55
- expect(library.books.any?).to be_truthy
56
- expect(library.books.any? {|book| book.library == nil}).to be_falsey
57
- expect(library.books.any? {|book| book.library == library}).to be_truthy
58
+ expect(library.books.any?).to be true
59
+ expect(library.books.any? {|book| book.library == nil}).to be false
60
+ expect(library.books.any? {|book| book.library == library}).to be true
58
61
  end
59
62
  end
60
63
  end
61
64
 
62
65
  describe "After save callbacks" do
63
66
  before :all do
64
- class Library < ActiveFedora::Base
67
+ class Library < ActiveFedora::Base
65
68
  has_many :books
66
69
  end
67
70
 
68
- class Book < ActiveFedora::Base
69
- belongs_to :library, :property=>:has_constituent
71
+ class Book < ActiveFedora::Base
72
+ belongs_to :library, predicate: ActiveFedora::RDF::RelsExt.hasConstituent
70
73
  after_save :find_self
71
74
  attr_accessor :library_books
72
75
 
@@ -80,27 +83,29 @@ describe "After save callbacks" do
80
83
  Object.send(:remove_const, :Book)
81
84
  Object.send(:remove_const, :Library)
82
85
  end
83
- let(:library) { Library.create() }
86
+
87
+ let(:library) { Library.create }
84
88
  let(:book) { Book.new(library: library) }
89
+
85
90
  it "should have the relationship available in after_save" do
86
91
  book.save!
87
92
  expect(book.library_books).to include book
88
93
  end
89
94
  end
90
95
 
91
- describe "When two or more relationships share the same property" do
96
+ describe "When two or more relationships share the same property" do
92
97
  before do
93
- class Book < ActiveFedora::Base
98
+ class Book < ActiveFedora::Base
94
99
  has_many :collections, :class_name=>'Collection'
95
100
  has_many :people
96
101
  end
97
102
 
98
103
  class Person < ActiveFedora::Base
99
- belongs_to :book, :property=>:is_part_of
104
+ belongs_to :book, predicate: ActiveFedora::RDF::RelsExt.isPartOf
100
105
  end
101
106
 
102
107
  class Collection < ActiveFedora::Base
103
- belongs_to :book, :property=>:is_part_of
108
+ belongs_to :book, predicate: ActiveFedora::RDF::RelsExt.isPartOf
104
109
  end
105
110
 
106
111
  @book = Book.create!
@@ -115,30 +120,57 @@ describe "When two or more relationships share the same property" do
115
120
 
116
121
  it "Should only return relationships of the correct class" do
117
122
  @book.reload
118
- expect(@book.people).to eq([@person1, @person2])
119
- expect(@book.collections).to eq([])
123
+ expect(@book.people).to eq [@person1, @person2]
124
+ expect(@book.collections).to eq []
125
+ end
126
+ end
127
+
128
+ describe "with an polymorphic association" do
129
+ before do
130
+ class Permissionable1 < ActiveFedora::Base
131
+ has_many :permissions, predicate: ActiveFedora::RDF::RelsExt.isPartOf, inverse_of: :access_to
132
+ end
133
+ class Permissionable2 < ActiveFedora::Base
134
+ has_many :permissions, predicate: ActiveFedora::RDF::RelsExt.isPartOf, inverse_of: :access_to
135
+ end
136
+
137
+ class Permission < ActiveFedora::Base
138
+ belongs_to :access_to, predicate: ActiveFedora::RDF::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
139
+ end
140
+ end
141
+
142
+ after do
143
+ Permissionable1.destroy_all
144
+ Object.send(:remove_const, :Permissionable1)
145
+ Object.send(:remove_const, :Permissionable2)
146
+ Object.send(:remove_const, :Permission)
147
+ end
148
+ let(:p1) { Permissionable1.create }
149
+
150
+ it "should make an association" do
151
+ expect(p1.permissions.create).to be_kind_of Permission
120
152
  end
121
153
  end
122
154
 
123
155
  describe "When relationship is restricted to AF::Base" do
124
156
  before do
125
- class Email < ActiveFedora::Base
126
- has_many :attachments, :property=>:is_part_of, :class_name=>'ActiveFedora::Base'
157
+ class Email < ActiveFedora::Base
158
+ has_many :attachments, predicate: ActiveFedora::RDF::RelsExt.isPartOf, :class_name=>'ActiveFedora::Base'
127
159
  end
128
160
 
129
161
  class Image < ActiveFedora::Base
130
- belongs_to :email, :property=>:is_part_of
162
+ belongs_to :email, predicate: ActiveFedora::RDF::RelsExt.isPartOf
131
163
  end
132
164
 
133
165
  class PDF < ActiveFedora::Base
134
- belongs_to :email, :property=>:is_part_of
166
+ belongs_to :email, predicate: ActiveFedora::RDF::RelsExt.isPartOf
135
167
  end
136
168
  end
137
169
 
138
170
  after do
139
- Object.send(:remove_const, :Image)
140
- Object.send(:remove_const, :PDF)
141
- Object.send(:remove_const, :Email)
171
+ Object.send(:remove_const, :Image)
172
+ Object.send(:remove_const, :PDF)
173
+ Object.send(:remove_const, :Email)
142
174
  end
143
175
 
144
176
 
@@ -150,7 +182,7 @@ describe "When relationship is restricted to AF::Base" do
150
182
  end
151
183
  it "Should not restrict relationships " do
152
184
  @book.reload
153
- expect(@book.attachments).to eq([@image, @pdf])
185
+ expect(@book.attachments).to eq [@image, @pdf]
154
186
  end
155
187
  end
156
188
 
@@ -160,15 +192,15 @@ describe "When relationship is restricted to AF::Base" do
160
192
  let!(:pdf) { PDF.create }
161
193
 
162
194
  after do
163
- email.destroy
164
195
  pdf.destroy
165
196
  image.destroy
197
+ email.destroy
166
198
  end
167
199
 
168
200
  it "Should not restrict relationships " do
169
201
  email.attachment_ids = [image.id, pdf.id]
170
202
  email.reload
171
- expect(email.attachments).to eq([image, pdf])
203
+ expect(email.attachments).to eq [image, pdf]
172
204
  end
173
205
  end
174
206
  end
@@ -176,10 +208,10 @@ end
176
208
  describe "Deleting a dependent relationship" do
177
209
  before do
178
210
  class Item < ActiveFedora::Base
179
- has_many :components, :property => :is_part_of
211
+ has_many :components
180
212
  end
181
213
  class Component < ActiveFedora::Base
182
- belongs_to :item, :property => :is_part_of
214
+ belongs_to :item, predicate: ActiveFedora::RDF::RelsExt.isPartOf
183
215
  end
184
216
  end
185
217
 
@@ -191,15 +223,25 @@ describe "Deleting a dependent relationship" do
191
223
  let(:item) { Item.create }
192
224
  let(:component) { Component.create }
193
225
 
226
+ context "when the relationship is set by an id" do
227
+ let(:item_id) { item.id }
228
+ let!(:component1) { Component.create(item_id: item_id) }
229
+ let!(:component2) { Component.create(item_id: item_id) }
230
+
231
+ it "should set the inverse relationship" do
232
+ expect(component1.item.components).to match_array [component1, component2]
233
+ end
234
+ end
235
+
194
236
  it "should remove relationships" do
195
237
  component.item = item
196
238
  component.save!
197
- expect(item.components).to eq([component])
239
+ expect(item.components).to eq [component]
198
240
  item.components.delete(component)
199
241
  item.reload
200
242
  component.reload
201
- expect(component.relationships(:is_part_of)).to eq([])
202
- expect(item.components).to eq([])
243
+ expect(component['item_id']).to be_nil
244
+ expect(item.components).to eq []
203
245
  end
204
246
 
205
247
  it "should remove the relationships that point at that object" do
@@ -207,7 +249,7 @@ describe "Deleting a dependent relationship" do
207
249
  component.save!
208
250
  item.delete
209
251
  component.reload
210
- expect(component.relationships(:is_part_of)).to eq([])
252
+ expect(component['item_id']).to be_nil
211
253
  end
212
254
 
213
255
  it "should only try to delete objects that exist in the datastore (not cached objects)" do
@@ -227,45 +269,81 @@ describe "Deleting a dependent relationship" do
227
269
  end
228
270
 
229
271
  describe "Autosave" do
230
- before do
231
- class Item < ActiveFedora::Base
232
- has_many :components
233
- has_metadata "foo", type: ActiveFedora::SimpleDatastream do |m|
234
- m.field "title", :string
272
+ context "a has_many - belongs_to relationship" do
273
+ before do
274
+ class Item < ActiveFedora::Base
275
+ has_many :components
276
+ has_metadata "foo", type: ActiveFedora::SimpleDatastream do |m|
277
+ m.field "title", :string
278
+ end
279
+ has_attributes :title, datastream: 'foo'
235
280
  end
236
- has_attributes :title, datastream: 'foo'
237
- end
238
- class Component < ActiveFedora::Base
239
- belongs_to :item, :property => :is_part_of
240
- has_metadata "foo", type: ActiveFedora::SimpleDatastream do |m|
241
- m.field "description", :string
281
+ class Component < ActiveFedora::Base
282
+ belongs_to :item, predicate: ActiveFedora::RDF::RelsExt.isPartOf
283
+ has_metadata "foo", type: ActiveFedora::SimpleDatastream do |m|
284
+ m.field "description", :string
285
+ end
286
+ has_attributes :description, datastream: 'foo'
242
287
  end
243
- has_attributes :description, datastream: 'foo'
244
288
  end
245
- end
246
289
 
247
- after do
290
+ after do
248
291
  Object.send(:remove_const, :Item)
249
292
  Object.send(:remove_const, :Component)
250
- end
293
+ end
251
294
 
252
- describe "From the belongs_to side" do
253
- let(:component) { Component.create(item: Item.new(title: 'my title')) }
295
+ context "From the belongs_to side" do
296
+ let(:component) { Component.create(item: Item.new(title: 'my title')) }
254
297
 
255
- it "should save dependent records" do
256
- component.reload
257
- expect(component.item.title).to eq('my title')
298
+ it "should save dependent records" do
299
+ component.reload
300
+ expect(component.item.title).to eq 'my title'
301
+ end
258
302
  end
259
- end
260
- describe "From the has_many side" do
261
- let(:item) { Item.create(components: [Component.new(description: 'my description')]) }
262
303
 
263
- it "should save dependent records" do
264
- item.reload
265
- expect(item.components.first.description).to eq('my description')
304
+ context "From the has_many side" do
305
+ let(:item) { Item.create(components: [Component.new(description: 'my description')]) }
306
+
307
+ it "should save dependent records" do
308
+ item.reload
309
+ expect(item.components.first.description).to eq 'my description'
310
+ end
266
311
  end
267
312
  end
268
313
 
269
- end
314
+ context "a has_many - has_and_belongs_to_many relationship" do
315
+ context "with ActiveFedora::Base as classes" do
316
+ before do
317
+ class Novel < ActiveFedora::Base
318
+ has_many :books, predicate: ActiveFedora::RDF::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
319
+ has_and_belongs_to_many :contents, predicate: ActiveFedora::RDF::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
320
+ end
321
+ class Text < ActiveFedora::Base
322
+ has_many :books, predicate: ActiveFedora::RDF::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
323
+ end
324
+ end
325
+ let(:text) { Text.create}
326
+ let(:novel) { Novel.create}
327
+
328
+ after do
329
+ Object.send(:remove_const, :Novel)
330
+ Object.send(:remove_const, :Text)
331
+ end
270
332
 
333
+ it "should work when added via the has_many" do
334
+ text.books << novel
335
+ novel.save
336
+ expect(novel.reload.contents).to eq [text]
337
+ expect(text.reload.books).to eq [novel]
338
+ end
339
+
340
+ it "should work when added via the has_and_belongs_to_many" do
341
+ novel.contents << text
342
+ novel.save!
343
+ text.reload
344
+ expect(text.books).to eq [novel]
345
+ end
271
346
 
347
+ end
348
+ end
349
+ end