active-fedora 6.8.0 → 7.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +15 -5
  4. data/CONTRIBUTING.md +2 -0
  5. data/Gemfile +0 -2
  6. data/History.txt +2 -32
  7. data/README.md +143 -0
  8. data/Rakefile +5 -7
  9. data/active-fedora.gemspec +9 -9
  10. data/gemfiles/rails3.gemfile +11 -0
  11. data/gemfiles/rails4.gemfile +10 -0
  12. data/lib/active_fedora.rb +31 -4
  13. data/lib/active_fedora/association_relation.rb +18 -0
  14. data/lib/active_fedora/associations.rb +38 -171
  15. data/lib/active_fedora/associations/association.rb +163 -0
  16. data/lib/active_fedora/associations/association_scope.rb +39 -0
  17. data/lib/active_fedora/associations/belongs_to_association.rb +47 -25
  18. data/lib/active_fedora/associations/builder/association.rb +55 -0
  19. data/lib/active_fedora/associations/builder/belongs_to.rb +100 -0
  20. data/lib/active_fedora/associations/builder/collection_association.rb +56 -0
  21. data/lib/active_fedora/associations/builder/has_and_belongs_to_many.rb +30 -0
  22. data/lib/active_fedora/associations/builder/has_many.rb +63 -0
  23. data/lib/active_fedora/associations/builder/singular_association.rb +32 -0
  24. data/lib/active_fedora/associations/{association_collection.rb → collection_association.rb} +203 -53
  25. data/lib/active_fedora/associations/collection_proxy.rb +862 -0
  26. data/lib/active_fedora/associations/has_and_belongs_to_many_association.rb +35 -25
  27. data/lib/active_fedora/associations/has_many_association.rb +36 -11
  28. data/lib/active_fedora/associations/singular_association.rb +62 -0
  29. data/lib/active_fedora/attributes.rb +43 -139
  30. data/lib/active_fedora/autosave_association.rb +317 -0
  31. data/lib/active_fedora/base.rb +10 -327
  32. data/lib/active_fedora/callbacks.rb +1 -3
  33. data/lib/active_fedora/content_model.rb +16 -0
  34. data/lib/active_fedora/core.rb +151 -0
  35. data/lib/active_fedora/datastream_attribute.rb +76 -0
  36. data/lib/active_fedora/datastream_hash.rb +8 -13
  37. data/lib/active_fedora/datastreams.rb +39 -26
  38. data/lib/active_fedora/digital_object.rb +2 -2
  39. data/lib/active_fedora/fedora_attributes.rb +45 -0
  40. data/lib/active_fedora/fixture_loader.rb +1 -1
  41. data/lib/active_fedora/indexing.rb +6 -1
  42. data/lib/active_fedora/model.rb +0 -17
  43. data/lib/active_fedora/nested_attributes.rb +2 -2
  44. data/lib/active_fedora/null_relation.rb +7 -0
  45. data/lib/active_fedora/om_datastream.rb +3 -4
  46. data/lib/active_fedora/persistence.rb +41 -29
  47. data/lib/active_fedora/querying.rb +2 -163
  48. data/lib/active_fedora/rdf.rb +1 -0
  49. data/lib/active_fedora/rdf/indexing.rb +67 -0
  50. data/lib/active_fedora/rdf_datastream.rb +2 -50
  51. data/lib/active_fedora/rdf_node.rb +12 -7
  52. data/lib/active_fedora/rdf_node/term_proxy.rb +30 -21
  53. data/lib/active_fedora/rdfxml_rdf_datastream.rb +1 -1
  54. data/lib/active_fedora/reflection.rb +163 -20
  55. data/lib/active_fedora/relation.rb +33 -130
  56. data/lib/active_fedora/relation/calculations.rb +19 -0
  57. data/lib/active_fedora/relation/delegation.rb +22 -0
  58. data/lib/active_fedora/relation/finder_methods.rb +247 -0
  59. data/lib/active_fedora/relation/merger.rb +22 -0
  60. data/lib/active_fedora/relation/query_methods.rb +58 -0
  61. data/lib/active_fedora/relation/spawn_methods.rb +46 -0
  62. data/lib/active_fedora/relationship_graph.rb +0 -2
  63. data/lib/active_fedora/rels_ext_datastream.rb +1 -4
  64. data/lib/active_fedora/rubydora_connection.rb +1 -1
  65. data/lib/active_fedora/scoping.rb +20 -0
  66. data/lib/active_fedora/scoping/default.rb +38 -0
  67. data/lib/active_fedora/scoping/named.rb +32 -0
  68. data/lib/active_fedora/semantic_node.rb +54 -106
  69. data/lib/active_fedora/serialization.rb +19 -0
  70. data/lib/active_fedora/sharding.rb +58 -0
  71. data/lib/active_fedora/solr_digital_object.rb +15 -5
  72. data/lib/active_fedora/solr_instance_loader.rb +1 -1
  73. data/lib/active_fedora/solr_service.rb +1 -1
  74. data/lib/active_fedora/unsaved_digital_object.rb +6 -4
  75. data/lib/active_fedora/version.rb +1 -1
  76. data/lib/tasks/active_fedora.rake +3 -0
  77. data/lib/tasks/active_fedora_dev.rake +6 -5
  78. data/spec/config_helper.rb +14 -14
  79. data/spec/integration/associations_spec.rb +168 -455
  80. data/spec/integration/attributes_spec.rb +12 -11
  81. data/spec/integration/auditable_spec.rb +11 -11
  82. data/spec/integration/autosave_association_spec.rb +25 -0
  83. data/spec/integration/base_spec.rb +163 -163
  84. data/spec/integration/belongs_to_association_spec.rb +166 -0
  85. data/spec/integration/bug_spec.rb +7 -7
  86. data/spec/integration/collection_association_spec.rb +58 -0
  87. data/spec/integration/complex_rdf_datastream_spec.rb +88 -88
  88. data/spec/integration/datastream_collections_spec.rb +69 -69
  89. data/spec/integration/datastream_spec.rb +43 -43
  90. data/spec/integration/datastreams_spec.rb +63 -63
  91. data/spec/integration/delete_all_spec.rb +46 -39
  92. data/spec/integration/fedora_solr_sync_spec.rb +5 -5
  93. data/spec/integration/field_to_solr_name_spec.rb +34 -0
  94. data/spec/integration/full_featured_model_spec.rb +100 -101
  95. data/spec/integration/has_and_belongs_to_many_associations_spec.rb +341 -0
  96. data/spec/integration/has_many_associations_spec.rb +172 -24
  97. data/spec/integration/json_serialization_spec.rb +31 -0
  98. data/spec/integration/load_from_solr_spec.rb +48 -0
  99. data/spec/integration/model_spec.rb +35 -40
  100. data/spec/integration/nested_attribute_spec.rb +42 -43
  101. data/spec/integration/ntriples_datastream_spec.rb +131 -113
  102. data/spec/integration/om_datastream_spec.rb +67 -67
  103. data/spec/integration/persistence_spec.rb +7 -7
  104. data/spec/integration/rdf_nested_attributes_spec.rb +56 -56
  105. data/spec/integration/relation_delegation_spec.rb +26 -25
  106. data/spec/integration/relation_spec.rb +42 -0
  107. data/spec/integration/rels_ext_datastream_spec.rb +20 -20
  108. data/spec/integration/scoped_query_spec.rb +61 -51
  109. data/spec/integration/solr_instance_loader_spec.rb +5 -5
  110. data/spec/integration/solr_service_spec.rb +46 -46
  111. data/spec/samples/hydra-mods_article_datastream.rb +334 -334
  112. data/spec/samples/hydra-rights_metadata_datastream.rb +57 -57
  113. data/spec/samples/marpa-dc_datastream.rb +17 -17
  114. data/spec/samples/models/audio_record.rb +16 -16
  115. data/spec/samples/models/image.rb +2 -2
  116. data/spec/samples/models/mods_article.rb +5 -5
  117. data/spec/samples/models/oral_history.rb +18 -18
  118. data/spec/samples/models/seminar.rb +24 -24
  119. data/spec/samples/models/seminar_audio_file.rb +17 -17
  120. data/spec/samples/oral_history_sample_model.rb +21 -21
  121. data/spec/samples/special_thing.rb +14 -14
  122. data/spec/spec_helper.rb +11 -7
  123. data/spec/support/an_active_model.rb +2 -8
  124. data/spec/support/freeze_mocks.rb +12 -0
  125. data/spec/support/mock_fedora.rb +17 -16
  126. data/spec/unit/active_fedora_spec.rb +58 -60
  127. data/spec/unit/attributes_spec.rb +314 -0
  128. data/spec/unit/base_active_model_spec.rb +28 -27
  129. data/spec/unit/base_cma_spec.rb +5 -5
  130. data/spec/unit/base_datastream_management_spec.rb +27 -27
  131. data/spec/unit/base_extra_spec.rb +76 -48
  132. data/spec/unit/base_spec.rb +277 -348
  133. data/spec/unit/callback_spec.rb +18 -19
  134. data/spec/unit/code_configurator_spec.rb +17 -17
  135. data/spec/unit/config_spec.rb +8 -16
  136. data/spec/unit/content_model_spec.rb +79 -60
  137. data/spec/unit/datastream_collections_spec.rb +229 -229
  138. data/spec/unit/datastream_spec.rb +51 -63
  139. data/spec/unit/datastreams_spec.rb +87 -87
  140. data/spec/unit/file_configurator_spec.rb +217 -217
  141. data/spec/unit/has_and_belongs_to_many_collection_spec.rb +44 -25
  142. data/spec/unit/has_many_collection_spec.rb +26 -8
  143. data/spec/unit/inheritance_spec.rb +13 -12
  144. data/spec/unit/model_spec.rb +39 -45
  145. data/spec/unit/nom_datastream_spec.rb +15 -15
  146. data/spec/unit/ntriples_datastream_spec.rb +123 -118
  147. data/spec/unit/om_datastream_spec.rb +227 -233
  148. data/spec/unit/persistence_spec.rb +34 -15
  149. data/spec/unit/predicates_spec.rb +73 -73
  150. data/spec/unit/property_spec.rb +17 -9
  151. data/spec/unit/qualified_dublin_core_datastream_spec.rb +33 -33
  152. data/spec/unit/query_spec.rb +222 -198
  153. data/spec/unit/rdf_datastream_spec.rb +21 -28
  154. data/spec/unit/rdf_list_nested_attributes_spec.rb +34 -34
  155. data/spec/unit/rdf_list_spec.rb +65 -64
  156. data/spec/unit/rdf_node_spec.rb +7 -7
  157. data/spec/unit/rdf_xml_writer_spec.rb +10 -10
  158. data/spec/unit/rdfxml_rdf_datastream_spec.rb +27 -27
  159. data/spec/unit/relationship_graph_spec.rb +51 -51
  160. data/spec/unit/rels_ext_datastream_spec.rb +68 -74
  161. data/spec/unit/rspec_matchers/belong_to_associated_active_fedora_object_matcher_spec.rb +15 -15
  162. data/spec/unit/rspec_matchers/have_many_associated_active_fedora_objects_matcher_spec.rb +15 -15
  163. data/spec/unit/rspec_matchers/have_predicate_matcher_spec.rb +15 -15
  164. data/spec/unit/rspec_matchers/match_fedora_datastream_matcher_spec.rb +12 -12
  165. data/spec/unit/rubydora_connection_spec.rb +5 -5
  166. data/spec/unit/semantic_node_spec.rb +48 -107
  167. data/spec/unit/serializers_spec.rb +4 -4
  168. data/spec/unit/service_definitions_spec.rb +26 -26
  169. data/spec/unit/simple_datastream_spec.rb +17 -17
  170. data/spec/unit/solr_config_options_spec.rb +29 -28
  171. data/spec/unit/solr_digital_object_spec.rb +17 -25
  172. data/spec/unit/solr_service_spec.rb +95 -82
  173. data/spec/unit/unsaved_digital_object_spec.rb +24 -23
  174. data/spec/unit/validations_spec.rb +21 -21
  175. metadata +110 -159
  176. data/.rspec +0 -1
  177. data/.rubocop.yml +0 -1
  178. data/.rubocop_todo.yml +0 -938
  179. data/CONSOLE_GETTING_STARTED.textile +0 -1
  180. data/NOKOGIRI_DATASTREAMS.textile +0 -1
  181. data/README.textile +0 -116
  182. data/lib/active_fedora/associations/association_proxy.rb +0 -178
  183. data/lib/active_fedora/delegating.rb +0 -72
  184. data/lib/active_fedora/nokogiri_datastream.rb +0 -11
  185. data/spec/integration/delegating_spec.rb +0 -59
  186. data/spec/rails3_test_app/.gitignore +0 -4
  187. data/spec/rails3_test_app/.rspec +0 -1
  188. data/spec/rails3_test_app/Gemfile +0 -40
  189. data/spec/rails3_test_app/Rakefile +0 -7
  190. data/spec/rails3_test_app/app/controllers/application_controller.rb +0 -3
  191. data/spec/rails3_test_app/app/helpers/application_helper.rb +0 -2
  192. data/spec/rails3_test_app/app/views/layouts/application.html.erb +0 -14
  193. data/spec/rails3_test_app/config.ru +0 -4
  194. data/spec/rails3_test_app/config/application.rb +0 -42
  195. data/spec/rails3_test_app/config/boot.rb +0 -6
  196. data/spec/rails3_test_app/config/database.yml +0 -22
  197. data/spec/rails3_test_app/config/environment.rb +0 -5
  198. data/spec/rails3_test_app/config/environments/development.rb +0 -25
  199. data/spec/rails3_test_app/config/environments/production.rb +0 -49
  200. data/spec/rails3_test_app/config/environments/test.rb +0 -35
  201. data/spec/rails3_test_app/config/initializers/backtrace_silencers.rb +0 -7
  202. data/spec/rails3_test_app/config/initializers/inflections.rb +0 -10
  203. data/spec/rails3_test_app/config/initializers/mime_types.rb +0 -5
  204. data/spec/rails3_test_app/config/initializers/secret_token.rb +0 -7
  205. data/spec/rails3_test_app/config/initializers/session_store.rb +0 -8
  206. data/spec/rails3_test_app/config/locales/en.yml +0 -5
  207. data/spec/rails3_test_app/config/routes.rb +0 -58
  208. data/spec/rails3_test_app/db/seeds.rb +0 -7
  209. data/spec/rails3_test_app/run_tests +0 -3
  210. data/spec/rails3_test_app/script/rails +0 -6
  211. data/spec/rails3_test_app/spec/spec_helper.rb +0 -27
  212. data/spec/rails3_test_app/spec/unit/rails_3_init.rb +0 -15
  213. data/spec/unit/association_proxy_spec.rb +0 -12
  214. data/spec/unit/base_delegate_spec.rb +0 -197
  215. data/spec/unit/base_delegate_to_spec.rb +0 -73
@@ -0,0 +1,56 @@
1
+ module ActiveFedora::Associations::Builder
2
+ class CollectionAssociation < Association #:nodoc:
3
+ CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
4
+
5
+ self.valid_options += [
6
+ :before_add, :after_add, :before_remove, :after_remove
7
+ ]
8
+
9
+
10
+ def self.build(model, name, options)
11
+ new(model, name, options).build
12
+ end
13
+
14
+ def initialize(model, name, options)
15
+ super(model, name, options)
16
+ end
17
+
18
+ def build
19
+ reflection = super
20
+ CALLBACKS.each { |callback_name| define_callback(callback_name) }
21
+ reflection
22
+ end
23
+
24
+ def writable?
25
+ true
26
+ end
27
+
28
+ private
29
+
30
+ def define_callback(callback_name)
31
+ full_callback_name = "#{callback_name}_for_#{name}"
32
+
33
+ # TODO : why do i need method_defined? I think its because of the inheritance chain
34
+ model.class_attribute full_callback_name.to_sym unless model.method_defined?(full_callback_name)
35
+ model.send("#{full_callback_name}=", Array(options[callback_name.to_sym]))
36
+ end
37
+
38
+ def define_readers
39
+ super
40
+
41
+ name = self.name
42
+ mixin.redefine_method("#{name.to_s.singularize}_ids") do
43
+ association(name).ids_reader
44
+ end
45
+ end
46
+
47
+ def define_writers
48
+ super
49
+
50
+ name = self.name
51
+ mixin.redefine_method("#{name.to_s.singularize}_ids=") do |ids|
52
+ association(name).ids_writer(ids)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,30 @@
1
+ module ActiveFedora::Associations::Builder
2
+ class HasAndBelongsToMany < CollectionAssociation #:nodoc:
3
+ self.macro = :has_and_belongs_to_many
4
+
5
+ self.valid_options += [:inverse_of]
6
+
7
+ def build
8
+ reflection = super
9
+ redefine_destroy
10
+ reflection
11
+ end
12
+
13
+ private
14
+
15
+ def redefine_destroy
16
+ # Don't use a before_destroy callback since users' before_destroy
17
+ # callbacks will be executed after the association is wiped out.
18
+ name = self.name
19
+ model.send(:include, Module.new {
20
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
21
+ def destroy # def destroy
22
+ super # super
23
+ #{name}.clear # posts.clear
24
+ end # end
25
+ RUBY
26
+ })
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,63 @@
1
+ module ActiveFedora::Associations::Builder
2
+ class HasMany < CollectionAssociation #:nodoc:
3
+ self.macro = :has_many
4
+
5
+ self.valid_options += [:dependent, :inverse_of]
6
+
7
+ def build
8
+ reflection = super
9
+ configure_dependency
10
+ reflection
11
+ end
12
+
13
+ private
14
+
15
+ def configure_dependency
16
+ if options[:dependent]
17
+ unless [:destroy, :delete_all, :nullify, :restrict].include?(options[:dependent])
18
+ raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, " \
19
+ ":nullify or :restrict (#{options[:dependent].inspect})"
20
+ end
21
+
22
+ send("define_#{options[:dependent]}_dependency_method")
23
+ model.before_destroy dependency_method_name
24
+ end
25
+ end
26
+
27
+ def define_destroy_dependency_method
28
+ name = self.name
29
+ model.send(:define_method, dependency_method_name) do
30
+ send(name).each do |o|
31
+ # No point in executing the counter update since we're going to destroy the parent anyway
32
+ counter_method = ('belongs_to_counter_cache_before_destroy_for_' + self.class.name.downcase).to_sym
33
+ if o.respond_to?(counter_method)
34
+ class << o
35
+ self
36
+ end.send(:define_method, counter_method, Proc.new {})
37
+ end
38
+ end
39
+
40
+ send(name).delete_all
41
+ end
42
+ end
43
+
44
+ def define_delete_all_dependency_method
45
+ name = self.name
46
+ model.send(:define_method, dependency_method_name) do
47
+ send(name).delete_all
48
+ end
49
+ end
50
+ alias :define_nullify_dependency_method :define_delete_all_dependency_method
51
+
52
+ def define_restrict_dependency_method
53
+ name = self.name
54
+ model.send(:define_method, dependency_method_name) do
55
+ raise ActiveRecord::DeleteRestrictionError.new(name) unless send(name).empty?
56
+ end
57
+ end
58
+
59
+ def dependency_method_name
60
+ "has_many_dependent_for_#{name}"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,32 @@
1
+ module ActiveFedora::Associations::Builder
2
+ class SingularAssociation < Association #:nodoc:
3
+ self.valid_options += [:dependent, :counter_cache, :inverse_of]
4
+
5
+ def constructable?
6
+ true
7
+ end
8
+
9
+ def define_accessors
10
+ super
11
+ define_constructors if constructable?
12
+ end
13
+
14
+ private
15
+
16
+ def define_constructors
17
+ name = self.name
18
+
19
+ mixin.redefine_method("build_#{name}") do |*params|
20
+ association(name).build(*params)
21
+ end
22
+
23
+ mixin.redefine_method("create_#{name}") do |*params|
24
+ association(name).create(*params)
25
+ end
26
+
27
+ mixin.redefine_method("create_#{name}!") do |*params|
28
+ association(name).create!(*params)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,9 +1,74 @@
1
1
  module ActiveFedora
2
2
  module Associations
3
- class AssociationCollection < AssociationProxy #:nodoc:
3
+ class CollectionAssociation < Association #:nodoc:
4
+ attr_reader :proxy
5
+
4
6
  def initialize(owner, reflection)
5
7
  super
8
+
6
9
  construct_query
10
+ @proxy = CollectionProxy.new(self)
11
+ end
12
+
13
+
14
+ # Implements the reader method, e.g. foo.items for Foo.has_many :items
15
+ # @param opts [Boolean, Hash] if true, force a reload
16
+ # @option opts [Symbol] :response_format can be ':solr' to return a solr result.
17
+ def reader(opts = false)
18
+ if opts.kind_of?(Hash)
19
+ if opts.delete(:response_format) == :solr
20
+ return load_from_solr(opts)
21
+ end
22
+ raise ArgumentError, "Hash parameter must include :response_format=>:solr (#{opts.inspect})"
23
+ else
24
+ force_reload = opts
25
+ end
26
+ reload if force_reload || stale_target?
27
+ proxy
28
+ end
29
+
30
+ # Implements the writer method, e.g. foo.items= for Foo.has_many :items
31
+ def writer(records)
32
+ replace(records)
33
+ end
34
+
35
+ # Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
36
+ def ids_reader
37
+ if loaded?
38
+ load_target.map do |record|
39
+ record.pid
40
+ end
41
+ else
42
+ load_from_solr.map do |solr_record|
43
+ solr_record['id']
44
+ end
45
+ end
46
+ end
47
+
48
+ # Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items
49
+ def ids_writer(ids)
50
+ ids = Array(ids).reject { |id| id.blank? }
51
+ replace(klass.find(ids))#.index_by { |r| r.id }.values_at(*ids))
52
+ #TODO, like this when find() can return multiple records
53
+ #send("#{reflection.name}=", reflection.klass.find(ids))
54
+ #send("#{reflection.name}=", ids.collect { |id| reflection.klass.find(id)})
55
+ end
56
+
57
+ def reset
58
+ reset_target!
59
+ @loaded = false
60
+ end
61
+
62
+ def find(*args)
63
+ scope.find(*args)
64
+ end
65
+
66
+ def first(*args)
67
+ first_or_last(:first, *args)
68
+ end
69
+
70
+ def last(*args)
71
+ first_or_last(:last, *args)
7
72
  end
8
73
 
9
74
  # Returns the size of the collection
@@ -38,21 +103,22 @@ module ActiveFedora
38
103
  concat(other_array.select { |v| !current.include?(v) })
39
104
  end
40
105
 
41
-
42
- def to_ary
43
- load_target
44
- if @target.is_a?(Array)
45
- @target.to_ary
106
+ def include?(record)
107
+ if record.is_a?(reflection.klass)
108
+ if record.new_record?
109
+ target.include?(record)
110
+ else
111
+ loaded? ? target.include?(record) : scope.exists?(record)
112
+ end
46
113
  else
47
- Array.wrap(@target)
114
+ false
48
115
  end
49
116
  end
50
- alias_method :to_a, :to_ary
51
117
 
52
- def reset
53
- reset_target!
54
- @loaded = false
118
+ def to_ary
119
+ load_target.dup
55
120
  end
121
+ alias_method :to_a, :to_ary
56
122
 
57
123
  def build(attributes = {}, &block)
58
124
  if attributes.is_a?(Array)
@@ -60,6 +126,7 @@ module ActiveFedora
60
126
  else
61
127
  build_record(attributes) do |record|
62
128
  block.call(record) if block_given?
129
+ add_to_target(record)
63
130
  set_belongs_to_association_for(record)
64
131
  end
65
132
  end
@@ -71,7 +138,7 @@ module ActiveFedora
71
138
  result = true
72
139
  load_target unless loaded?
73
140
 
74
- flatten_deeper(records).each do |record|
141
+ records.flatten.each do |record|
75
142
  raise_on_type_mismatch(record)
76
143
  add_record_to_target_with_callbacks(record) do |r|
77
144
  result &&= insert_record(record)
@@ -84,6 +151,26 @@ module ActiveFedora
84
151
  alias_method :push, :<<
85
152
  alias_method :concat, :<<
86
153
 
154
+ # Remove all records from this association
155
+ #
156
+ # See delete for more info.
157
+ def delete_all
158
+ delete(load_target).tap do
159
+ reset
160
+ loaded!
161
+ end
162
+ end
163
+
164
+ # Remove all records from this association
165
+ #
166
+ # See delete for more info.
167
+ def destroy_all
168
+ destroy(load_target).tap do
169
+ reset
170
+ loaded!
171
+ end
172
+ end
173
+
87
174
  # Removes +records+ from this association calling +before_remove+ and
88
175
  # +after_remove+ callbacks.
89
176
  #
@@ -92,10 +179,17 @@ module ActiveFedora
92
179
  # are actually removed from the database, that depends precisely on
93
180
  # +delete_records+. They are in any case removed from the collection.
94
181
  def delete(*records)
95
- remove_records(records) do |_records, old_records|
96
- delete_records(old_records) if old_records.any?
97
- _records.each { |record| @target.delete(record) }
98
- end
182
+ delete_or_destroy(records, options[:dependent])
183
+ end
184
+
185
+ # Destroy +records+ and remove them from this association calling
186
+ # +before_remove+ and +after_remove+ callbacks.
187
+ #
188
+ # Note that this method will _always_ remove records from the database
189
+ # ignoring the +:dependent+ option.
190
+ def destroy(*records)
191
+ records = find(records) if records.any? { |record| record.kind_of?(Fixnum) || record.kind_of?(String) }
192
+ delete_or_destroy(records, :destroy)
99
193
  end
100
194
 
101
195
  def create(attrs = {})
@@ -115,58 +209,67 @@ module ActiveFedora
115
209
  record.save!
116
210
  end
117
211
  end
118
-
119
212
 
213
+ # Count all records using solr. Construct options and pass them with
214
+ # scope to the target class's +count+.
215
+ def count(options = {})
216
+ @reflection.klass.count(:conditions => @counter_query)
217
+ end
218
+
120
219
  def load_target
121
- if !@owner.new_record?
220
+ if find_target?
221
+ targets = []
222
+
122
223
  begin
123
- if !loaded?
124
- if @target.is_a?(Array) && @target.any?
125
- @target = find_target.map do |f|
126
- i = @target.index(f)
127
- if i
128
- @target.delete_at(i).tap do |t|
129
- keys = ["id"] + t.changes.keys + (f.attribute_names - t.attribute_names)
130
- t.attributes = f.attributes.except(*keys)
131
- end
132
- else
133
- f
134
- end
135
- end + @target
136
- else
137
- @target = find_target
138
- end
139
- end
224
+ targets = find_target
140
225
  rescue ObjectNotFoundError => e
141
226
  ActiveFedora::Base.logger.error "Solr and Fedora may be out of sync:\n" + e.message
142
227
  reset
143
228
  end
229
+
230
+ @target = merge_target_lists(targets, @target)
144
231
  end
145
232
 
146
- loaded if target
233
+ loaded!
147
234
  target
148
235
  end
149
236
 
150
237
  def find_target
151
- return [] if @finder_query.empty?
152
- solr_result = SolrService.query(@finder_query, :rows=>1000)
153
-
154
- # Set the default classname to base inheritance on.
155
- opts = {}
156
- opts[:class] = @reflection.class_name.constantize.to_class_uri unless @reflection.class_name.nil?
238
+ # TODO: don't reify, just store the solr results and lazily reify.
239
+ # For now, we set a hard limit of 1000 results.
240
+ return ActiveFedora::SolrService.reify_solr_results(load_from_solr(rows: 1000))
241
+ end
157
242
 
158
- #TODO, don't reify, just store the solr results and lazily reify.
159
- return ActiveFedora::SolrService.reify_solr_results(solr_result)
243
+ def merge_target_lists(loaded, existing)
244
+ return loaded if existing.empty?
245
+ return existing if loaded.empty?
246
+
247
+ loaded.map do |f|
248
+ i = existing.index(f)
249
+ if i
250
+ existing.delete_at(i).tap do |t|
251
+ keys = ["id"] + t.changes.keys + (f.attribute_names - t.attribute_names)
252
+ # FIXME: this call to attributes causes many NoMethodErrors
253
+ attributes = f.attributes
254
+ (attributes.keys - keys).each do |k|
255
+ t.send("#{k}=", attributes[k])
256
+ end
257
+ end
258
+ else
259
+ f
260
+ end
261
+ end + existing
160
262
  end
161
263
 
162
- def load_from_solr
264
+ # @param opts [Hash] Options that will be passed through to ActiveFedora::SolrService.query.
265
+ def load_from_solr(opts = Hash.new)
163
266
  return [] if @finder_query.empty?
164
- SolrService.query(@finder_query, :rows=>1000)
267
+ solr_opts = {rows: opts.delete(:rows) || count}
268
+ SolrService.query(@finder_query, solr_opts.merge(opts))
165
269
  end
166
270
 
167
-
168
271
  def add_record_to_target_with_callbacks(record)
169
- # callback(:before_add, record)
272
+ callback(:before_add, record)
170
273
  yield(record) if block_given?
171
274
  @target ||= [] unless loaded?
172
275
  if index = @target.index(record)
@@ -174,11 +277,38 @@ module ActiveFedora
174
277
  else
175
278
  @target << record
176
279
  end
177
- # callback(:after_add, record)
280
+ callback(:after_add, record)
178
281
  # set_inverse_instance(record, @owner)
179
282
  record
180
283
  end
181
284
 
285
+ def add_to_target(record)
286
+ # transaction do
287
+ callback(:before_add, record)
288
+ yield(record) if block_given?
289
+
290
+ if @reflection.options[:uniq] && index = @target.index(record)
291
+ @target[index] = record
292
+ else
293
+ @target << record
294
+ end
295
+
296
+ callback(:after_add, record)
297
+ set_inverse_instance(record)
298
+ # end
299
+
300
+ record
301
+ end
302
+
303
+ def scope(opts = {})
304
+ scope = super()
305
+ scope.none! if opts.fetch(:nullify, true) && null_scope?
306
+ scope
307
+ end
308
+
309
+ def null_scope?
310
+ owner.new_record? && !foreign_key_present?
311
+ end
182
312
  protected
183
313
  def reset_target!
184
314
  @target = Array.new
@@ -194,6 +324,14 @@ module ActiveFedora
194
324
 
195
325
  private
196
326
 
327
+ # Assigns the ID of the owner to the corresponding foreign key in +record+.
328
+ # If the association is polymorphic the type of the owner is also set.
329
+ def set_belongs_to_association_for(record)
330
+ unless @owner.new_record?
331
+ record.add_relationship(find_predicate, @owner)
332
+ end
333
+ end
334
+
197
335
  def find_predicate
198
336
  if @reflection.options[:property]
199
337
  @reflection.options[:property]
@@ -243,13 +381,16 @@ module ActiveFedora
243
381
  end
244
382
  end
245
383
 
246
- def remove_records(*records)
247
- records = flatten_deeper(records)
384
+ def delete_or_destroy(records, method)
385
+ records = records.flatten
248
386
  records.each { |record| raise_on_type_mismatch(record) }
387
+ existing_records = records.reject { |r| r.new_record? }
249
388
 
250
389
  records.each { |record| callback(:before_remove, record) }
251
- old_records = records.reject { |r| r.new_record? }
252
- yield(records, old_records)
390
+
391
+ delete_records(existing_records, method) if existing_records.any?
392
+ records.each { |record| target.delete(record) }
393
+
253
394
  records.each { |record| callback(:after_remove, record) }
254
395
  end
255
396
 
@@ -276,6 +417,15 @@ module ActiveFedora
276
417
  raise ActiveFedora::RecordNotSaved, "You cannot call create unless the parent is saved"
277
418
  end
278
419
  end
420
+
421
+ # Fetches the first/last using solr if possible, otherwise from the target array.
422
+ def first_or_last(type, *args)
423
+ args.shift if args.first.is_a?(Hash) && args.first.empty?
424
+
425
+ #collection = fetch_first_or_last_using_find?(args) ? scoped : load_target
426
+ collection = load_target
427
+ collection.send(type, *args)
428
+ end
279
429
 
280
430
  end
281
431
  end