active-fedora 6.4.0 → 6.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ed505bd4c93e672099b6338c7835745b8b3c0da
4
- data.tar.gz: 1a3ecd688f69dc30eadfad3712e173793a9971a7
3
+ metadata.gz: 967a4215aac7cba00431147cd02fd5e9f96b04f1
4
+ data.tar.gz: 3eb487a369aa83bfe1d9de8f1d016a5950ffa5a7
5
5
  SHA512:
6
- metadata.gz: 1379046abbce3ef04db6542c3ff0905899acd5395771700d23ce04ba1157a68874930c3d5c7fd4ac3ba7523db1362c127cb9734287bdff3922b69540c2437337
7
- data.tar.gz: a667ef87332c2d4656624cdf8cab47f3a27fdde1838023d85f5765d5b9c412656b82d94c10e2a6d7ca055bc13d9f831b09dc13c0f80fe472364d0f01c0967573
6
+ metadata.gz: 9fd89f5819540cb4cd994f4009151d089b5d45f4502a7a0ee1480c4e6eadf2a6386c83e0ae95ea182e9054f324a8b20ee011a34d6bfec987f07459048911cdae
7
+ data.tar.gz: 43f479f7be5b7a683eb748ac06e03150c897bc1b0ac9d1ea650a5b9c158fef9039aeeb1582b707a2136ac6c5b218aa011812e6492aa4b75e8f9e34078b75cb14
@@ -22,6 +22,7 @@ module ActiveFedora #:nodoc:
22
22
  class AssociationTypeMismatch < RuntimeError; end # :nodoc:
23
23
  class UnregisteredPredicateError < RuntimeError; end # :nodoc:
24
24
  class RecordNotSaved < RuntimeError; end # :nodoc:
25
+ class RecordNotFound < RuntimeError; end # :nodoc:
25
26
 
26
27
 
27
28
  eager_autoload do
@@ -36,7 +36,6 @@ module ActiveFedora
36
36
  module ClassMethods
37
37
 
38
38
  def has_many(association_id, options={})
39
- raise "You must specify a property name for #{name}" if !options[:property]
40
39
  reflection = create_has_many_reflection(association_id, options)
41
40
  add_association_callbacks(reflection.name, reflection.options)
42
41
  collection_accessor_methods(reflection, HasManyAssociation)
@@ -180,13 +180,40 @@ module ActiveFedora
180
180
  end
181
181
 
182
182
  def construct_query
183
- clauses = {@reflection.options[:property] => @owner.internal_uri}
183
+
184
+ clauses = {find_predicate => @owner.internal_uri}
184
185
  clauses[:has_model] = @reflection.class_name.constantize.to_class_uri if @reflection.class_name && @reflection.class_name != 'ActiveFedora::Base'
185
186
  @counter_query = @finder_query = ActiveFedora::SolrService.construct_query_for_rel(clauses)
186
187
  end
187
188
 
188
189
 
189
190
  private
191
+
192
+ def find_predicate
193
+ if @reflection.options[:property]
194
+ @reflection.options[:property]
195
+ elsif @reflection.class_name && @reflection.class_name != 'ActiveFedora::Base' && @reflection.macro != :has_and_belongs_to_many
196
+ inverse_relation = @owner.class.to_s.underscore.to_sym
197
+ begin
198
+ find_class_for_relation(@reflection.class_name.constantize)
199
+ rescue NameError
200
+ raise "No :property attribute was set or could be inferred for #{@reflection.macro} #{@reflection.name.inspect} on #{@owner.class}"
201
+ end
202
+ end
203
+ end
204
+
205
+ def find_class_for_relation(klass, inverse_relation=@owner.class.to_s.underscore.to_sym)
206
+ raise "Unable to find #{klass}." if inverse_relation == :'active_fedora/base'
207
+ if klass.reflections.key?(inverse_relation)
208
+ # Try it singular
209
+ return klass.reflections[inverse_relation].options[:property]
210
+ elsif klass.reflections.key?(inverse_relation.to_s.pluralize.to_sym)
211
+ # Try it plural
212
+ return klass.reflections[inverse_relation.to_s.pluralize.to_sym].options[:property]
213
+ end
214
+ find_class_for_relation(klass, @owner.class.superclass.to_s.underscore.to_sym)
215
+ end
216
+
190
217
  def create_record(attrs)
191
218
  attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
192
219
  ensure_owner_is_not_new
@@ -45,7 +45,7 @@ module ActiveFedora
45
45
  # Deletes the records according to the <tt>:dependent</tt> option.
46
46
  def delete_records(records)
47
47
  records.each do |r|
48
- r.remove_relationship(@reflection.options[:property], @owner)
48
+ r.remove_relationship(find_predicate, @owner)
49
49
  end
50
50
  end
51
51
 
@@ -2,7 +2,6 @@ require "om"
2
2
 
3
3
  module ActiveFedora
4
4
  class OmDatastream < Datastream
5
- # right now, OmDatastream is just an alias for NokogiriDatastream
6
5
 
7
6
  before_save do
8
7
  if content.blank?
@@ -76,8 +76,9 @@ module ActiveFedora
76
76
  :tableOfContents,
77
77
  :temporal,
78
78
  :title,
79
+ :type,
79
80
  :valid
80
- ] # removed :type, :format
81
+ ] # removed :format
81
82
  DCTERMS.freeze
82
83
 
83
84
  #Constructor. this class will call self.field for each DCTERM. In short, all DCTERMS fields will already exist
@@ -122,7 +123,8 @@ module ActiveFedora
122
123
  self.class.class_fields << name.to_s
123
124
  # add term to terminology
124
125
  unless self.class.terminology.has_term?(name.to_sym)
125
- term = OM::XML::Term.new(name.to_sym, {:xmlns=>"http://purl.org/dc/terms/", :namespace_prefix => "dcterms"}, self.class.terminology)
126
+ om_term_opts = {:xmlns=>"http://purl.org/dc/terms/", :namespace_prefix => "dcterms", :path => opts[:path]}
127
+ term = OM::XML::Term.new(name.to_sym, om_term_opts, self.class.terminology)
126
128
  self.class.terminology.add_term(term)
127
129
  term.generate_xpath_queries!
128
130
  end
@@ -3,6 +3,11 @@ module ActiveFedora
3
3
  rake_tasks do
4
4
  load "tasks/active_fedora.rake"
5
5
  end
6
+
7
+ initializer 'activefedora.autoload', :before => :set_autoload_paths do |app|
8
+ app.config.autoload_paths << 'app/models/datastreams'
9
+ end
10
+
6
11
  generators do
7
12
  require(
8
13
  'generators/active_fedora/config/config_generator'
@@ -12,14 +12,35 @@ module ActiveFedora
12
12
 
13
13
  UNASSIGNABLE_KEYS = %w( id _destroy )
14
14
 
15
+ # @params [Symbol] association_name
16
+ # @params [Hash, Array] attributes_collection
17
+ # @example
18
+ #
19
+ # assign_nested_attributes_for_collection_association(:people, {
20
+ # '1' => { id: '1', name: 'Peter' },
21
+ # '2' => { name: 'John' },
22
+ # '3' => { id: '2', _destroy: true }
23
+ # })
24
+ #
25
+ # Will update the name of the Person with ID 1, build a new associated
26
+ # person with the name 'John', and mark the associated Person with ID 2
27
+ # for destruction.
28
+ #
29
+ # Also accepts an Array of attribute hashes:
30
+ #
31
+ # assign_nested_attributes_for_collection_association(:people, [
32
+ # { id: '1', name: 'Peter' },
33
+ # { name: 'John' },
34
+ # { id: '2', _destroy: true }
35
+ # ])
15
36
  def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
16
37
  options = self.nested_attributes_options[association_name]
17
38
 
18
39
  # TODO
19
40
  #check_record_limit!(options[:limit], attributes_collection)
20
41
 
21
- if attributes_collection.is_a?(Hash) || attributes_collection.is_a?(String)
22
- attributes_collection = [attributes_collection]
42
+ if attributes_collection.is_a?(Hash)# || attributes_collection.is_a?(String)
43
+ attributes_collection = attributes_collection.values
23
44
  end
24
45
 
25
46
  association = self.send(association_name)
@@ -35,7 +56,7 @@ module ActiveFedora
35
56
  assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
36
57
  end
37
58
  else
38
- raise_nested_attributes_record_not_found(association_name, record.rdf_subject.to_s)
59
+ raise_nested_attributes_record_not_found(association_name, attributes['id'].to_s)
39
60
  end
40
61
  end
41
62
  end
@@ -48,7 +69,7 @@ module ActiveFedora
48
69
  end
49
70
 
50
71
  def raise_nested_attributes_record_not_found(association_name, record_id)
51
- raise RecordNotFound, "Couldn't find #{association_name} with ID=#{record_id} for #{self.class.name} with ID=#{id}"
72
+ raise RecordNotFound, "Couldn't find #{association_name} with ID=#{record_id} for #{self.class.name} with ID=#{rdf_subject.to_s}"
52
73
  end
53
74
 
54
75
  def call_reject_if(association_name, attributes)
@@ -64,6 +64,14 @@ module ActiveFedora
64
64
  @marked_for_destruction
65
65
  end
66
66
 
67
+ def new_record= val
68
+ @new_record = val
69
+ end
70
+
71
+ def new_record?
72
+ @new_record
73
+ end
74
+
67
75
  # if there are any existing statements with this predicate, replace them
68
76
  # @param [RDF::URI] subject the subject to insert into the graph
69
77
  # @param [Symbol, RDF::URI] predicate the predicate to insert into the graph
@@ -21,7 +21,9 @@ module ActiveFedora
21
21
  node = mint_node(attributes)
22
22
  parent.graph.insert([subject, predicate, node.rdf_subject])
23
23
  reset!
24
- target.find { |n| n.rdf_subject == node.rdf_subject}
24
+ new_node = target.find { |n| n.rdf_subject == node.rdf_subject}
25
+ new_node.new_record = true
26
+ new_node
25
27
  end
26
28
 
27
29
  def reset!
@@ -1,3 +1,3 @@
1
1
  module ActiveFedora
2
- VERSION = "6.4.0"
2
+ VERSION = "6.4.1"
3
3
  end
@@ -6,4 +6,6 @@ Example:
6
6
 
7
7
  This will create:
8
8
  app/models/journal.rb
9
+ app/models/datastreams/journal_metadata.rb
9
10
  spec/models/journal_spec.rb
11
+ spec/models/datastreams/journal_metadata_spec.rb
@@ -6,18 +6,25 @@ module ActiveFedora
6
6
  check_class_collision
7
7
 
8
8
  class_option :directory, :type => :string, :default => 'models', :desc => "Which directory to generate? (i.e. app/DIRECTORY)"
9
+ class_option :datastream_directory, :type => :string, :default => 'models/datastreams', :desc => "Which datastream directory to generate? (i.e. models/datastreams)"
9
10
  class_option :has_file_datastream, :type => :string, :default => nil, :desc => "Name a file datastream to create"
10
11
  class_option :descMetadata, :type => :string, :default => nil, :desc => "Add a descMetadata metadata datastream"
11
12
 
12
13
  def install
13
14
  template('model.rb.erb',File.join('app', directory, "#{file_name}.rb"))
15
+ template('datastream.rb.erb',File.join('app', datastream_directory, "#{file_name}_metadata.rb"))
14
16
  template('model_spec.rb.erb',File.join('spec', directory, "#{file_name}_spec.rb"))
17
+ template('datastream_spec.rb.erb',File.join('spec', datastream_directory, "#{file_name}_metadata_spec.rb"))
15
18
  end
16
19
 
17
20
  protected
18
21
 
19
22
  def directory
20
- options[:directory] || 'models'
23
+ options[:directory]
24
+ end
25
+
26
+ def datastream_directory
27
+ options[:datastream_directory]
21
28
  end
22
29
  end
23
30
  end
@@ -0,0 +1,32 @@
1
+ # Generated via
2
+ # `rails generate active_fedora::model <%= class_name %>`
3
+ class <%= class_name %>Metadata < ActiveFedora::OmDatastream
4
+
5
+ # Define a terminology for parsing this XML document
6
+ # See: https://github.com/projecthydra/om/wiki/Tame-your-XML-with-OM
7
+ #
8
+ # set_terminology do |t|
9
+ # t.root(path: "fields")
10
+ # t.title
11
+ # t.author
12
+ # end
13
+
14
+
15
+ # Describe what an empty document looks like
16
+ #
17
+ # def self.xml_template
18
+ # Nokogiri::XML.parse("<fields/>")
19
+ # end
20
+ #
21
+
22
+
23
+ # "If you need to add additional attributes to the SOLR document, define the
24
+ # #to_solr method and make sure to use super"
25
+ #
26
+ # def to_solr(solr_document={}, options={})
27
+ # super(solr_document, options)
28
+ # solr_document["my_attribute_s"] = my_attribute
29
+ # return solr_document
30
+ # end
31
+
32
+ end
@@ -0,0 +1,13 @@
1
+ # Generated via
2
+ # `rails generate active_fedora::model <%= class_name %>`
3
+ require 'spec_helper'
4
+
5
+ describe <%= class_name %>Metadata do
6
+ it 'should have a title' do
7
+ pending "Create a terminology for <%= class_name %>Metadata, then enable this test"
8
+ subject.title = 'War and Peace'
9
+ subject.title.should == ['War and Peace']
10
+ end
11
+
12
+ end
13
+
@@ -1,15 +1,12 @@
1
1
  # Generated via
2
2
  # `rails generate active_fedora::model <%= class_name %>`
3
- require 'active_fedora/base'
4
3
  class <%= class_name %> < ActiveFedora::Base
5
4
  <% if options['descMetadata'] %>
6
- has_metadata name: "descMetadata", type: <%= options['descMetadata'] %>
5
+ has_metadata "descMetadata", type: <%= options['descMetadata'] %>
7
6
  <% else %>
8
- # Uncomment the following lines to add a #descMetadata method that is a
9
- # datastream. You will need to specify the :type:, which may involve
10
- # creating a new Datastream object.
7
+ # Creating a #descMetadata method that returns the datastream.
11
8
  #
12
- # has_metadata name: "descMetadata", type: <%= class_name %>MetadataDatastream
9
+ has_metadata "descMetadata", type: <%= class_name %>Metadata
13
10
  <%- end -%>
14
11
  <% if options['has_file_datastream'] %>
15
12
  has_file_datastream "<%= options['has_file_datastream'] %>"
@@ -16,4 +16,8 @@ describe <%= class_name %> do
16
16
  subject.destroy
17
17
  end
18
18
 
19
- end
19
+ it 'should have a descMetadata datastream' do
20
+ subject.descMetadata.should be_kind_of <%= options['descMetadata'] ? options['descMetadata'] : "#{class_name}Metadata" %>
21
+ end
22
+
23
+ end
@@ -1,357 +1,369 @@
1
1
  require 'spec_helper'
2
2
 
3
- class Library < ActiveFedora::Base
4
- has_many :books, :property=>:has_constituent
5
- end
6
-
7
- class Book < ActiveFedora::Base
8
- belongs_to :library, :property=>:has_constituent
9
- belongs_to :author, :property=>:has_member, :class_name=>'Person'
10
- has_and_belongs_to_many :topics, :property=>:has_topic, :inverse_of=>:is_topic_of
11
- has_and_belongs_to_many :collections, :property=>:is_member_of_collection
12
- end
13
-
14
- class Person < ActiveFedora::Base
15
- end
16
-
17
- class Collection < ActiveFedora::Base
18
- end
19
-
20
- class Topic < ActiveFedora::Base
21
- has_and_belongs_to_many :books, :property=>:is_topic_of
22
- end
23
-
24
3
  describe ActiveFedora::Base do
25
- describe "an unsaved instance" do
26
- describe "of has_many" do
27
- before do
28
- @library = Library.new()
29
- @book = Book.new
30
- @book.save
31
- @book2 = Book.new
32
- @book2.save
33
- end
34
-
35
- it "should build child" do
36
- new_book = @library.books.build({})
37
- new_book.should be_new_record
38
- new_book.should be_kind_of Book
39
- new_book.library.should be_nil
40
- @library.books.should == [new_book]
41
- #TODO save the associated children too, requires something like ActiveRecord::AutosaveAssociation (ver 3.0.12)
42
- #@library.save
43
- #new_book.library.should == @library
44
- end
45
-
46
- it "should not create children if the parent isn't saved" do
47
- lambda {@library.books.create({})}.should raise_error ActiveFedora::RecordNotSaved, "You cannot call create unless the parent is saved"
48
- end
49
-
50
- it "should create children" do
51
- @library.save!
52
- new_book = @library.books.create({})
53
- new_book.should_not be_new_record
54
- new_book.should be_kind_of Book
55
- new_book.library.should == @library
56
- end
57
-
58
- it "should build parent" do
59
- new_library = @book.build_library({})
60
- new_library.should be_new_record
61
- new_library.should be_kind_of Library
62
- @book.library.should == new_library
4
+ describe "complex example" do
5
+ before do
6
+ class Library < ActiveFedora::Base
7
+ has_many :books, :property=>:has_constituent
63
8
  end
64
9
 
65
- it "should create parent" do
66
- new_library = @book.create_library({})
67
- new_library.should_not be_new_record
68
- new_library.should be_kind_of Library
69
- @book.library.should == new_library
10
+ class Book < ActiveFedora::Base
11
+ belongs_to :library, :property=>:has_constituent
12
+ belongs_to :author, :property=>:has_member, :class_name=>'Person'
13
+ has_and_belongs_to_many :topics, :property=>:has_topic, :inverse_of=>:is_topic_of
14
+ has_and_belongs_to_many :collections, :property=>:is_member_of_collection
70
15
  end
71
16
 
72
- it "should let you shift onto the association" do
73
- @library.new_record?.should be_true
74
- @library.books.size == 0
75
- @library.books.should == []
76
- @library.book_ids.should ==[]
77
- @library.books << @book
78
- @library.books.should == [@book]
79
- @library.book_ids.should ==[@book.pid]
80
-
17
+ class Person < ActiveFedora::Base
81
18
  end
82
19
 
83
- it "should let you set an array of objects" do
84
- @library.books = [@book, @book2]
85
- @library.books.should == [@book, @book2]
86
- @library.save
87
-
88
- @library.books = [@book]
89
- @library.books.should == [@book]
90
-
91
- end
92
- it "should let you set an array of object ids" do
93
- @library.book_ids = [@book.pid, @book2.pid]
94
- @library.books.should == [@book, @book2]
20
+ class Collection < ActiveFedora::Base
95
21
  end
96
22
 
97
- it "setter should wipe out previously saved relations" do
98
- @library.book_ids = [@book.pid, @book2.pid]
99
- @library.book_ids = [@book2.pid]
100
- @library.books.should == [@book2]
101
-
23
+ class Topic < ActiveFedora::Base
24
+ has_and_belongs_to_many :books, :property=>:is_topic_of
102
25
  end
26
+ end
103
27
 
104
- it "saving the parent should save the relationships on the children" do
105
- @library.save
106
- @library.books = [@book, @book2]
107
- @library.save
108
- @library = Library.find(@library.pid)
109
- @library.books.should == [@book, @book2]
110
- end
28
+ after do
29
+ Object.send(:remove_const, :Library)
30
+ Object.send(:remove_const, :Book)
31
+ Object.send(:remove_const, :Person)
32
+ Object.send(:remove_const, :Collection)
33
+ Object.send(:remove_const, :Topic)
34
+ end
111
35
 
36
+ describe "an unsaved instance" do
37
+ describe "of has_many" do
38
+ before do
39
+ @library = Library.new()
40
+ @book = Book.new
41
+ @book.save
42
+ @book2 = Book.new
43
+ @book2.save
44
+ end
112
45
 
113
- it "should let you lookup an array of objects with solr" do
114
- @library.save
115
- @book.library = @library
116
- @book2.library = @library
117
- @book.save
118
- @book2.save
46
+ it "should build child" do
47
+ new_book = @library.books.build({})
48
+ new_book.should be_new_record
49
+ new_book.should be_kind_of Book
50
+ new_book.library.should be_nil
51
+ @library.books.should == [new_book]
52
+ #TODO save the associated children too, requires something like ActiveRecord::AutosaveAssociation (ver 3.0.12)
53
+ #@library.save
54
+ #new_book.library.should == @library
55
+ end
119
56
 
120
- @library = Library.find(@library.pid)
121
- @library.books.should == [@book, @book2]
122
-
123
- solr_resp = @library.books(:response_format=>:solr)
124
- solr_resp.size.should == 2
125
- solr_resp[0]['id'].should == @book.pid
126
- solr_resp[1]['id'].should == @book2.pid
127
-
128
- end
57
+ it "should not create children if the parent isn't saved" do
58
+ lambda {@library.books.create({})}.should raise_error ActiveFedora::RecordNotSaved, "You cannot call create unless the parent is saved"
59
+ end
129
60
 
61
+ it "should create children" do
62
+ @library.save!
63
+ new_book = @library.books.create({})
64
+ new_book.should_not be_new_record
65
+ new_book.should be_kind_of Book
66
+ new_book.library.should == @library
67
+ end
130
68
 
69
+ it "should build parent" do
70
+ new_library = @book.build_library({})
71
+ new_library.should be_new_record
72
+ new_library.should be_kind_of Library
73
+ @book.library.should == new_library
74
+ end
131
75
 
132
- after do
133
- @book.delete
134
- @book2.delete
135
- end
136
- end
76
+ it "should create parent" do
77
+ new_library = @book.create_library({})
78
+ new_library.should_not be_new_record
79
+ new_library.should be_kind_of Library
80
+ @book.library.should == new_library
81
+ end
137
82
 
138
- describe "of belongs to" do
139
- before do
140
- @library = Library.new()
141
- @library.save
142
- @book = Book.new
143
- @book.save
144
- end
145
- it "shouldn't do anything if you set a nil id" do
146
- @book.library_id = nil
147
- end
148
- it "should be settable from the book side" do
149
- @book.library_id = @library.pid
150
- @book.library.should == @library
151
- @book.library.pid.should == @library.pid
152
- @book.attributes= {:library_id => ""}
153
- @book.library_id.should be_nil
154
- end
155
- after do
156
- @library.delete
157
- @book.delete
158
- end
159
- end
83
+ it "should let you shift onto the association" do
84
+ @library.new_record?.should be_true
85
+ @library.books.size == 0
86
+ @library.books.should == []
87
+ @library.book_ids.should ==[]
88
+ @library.books << @book
89
+ @library.books.should == [@book]
90
+ @library.book_ids.should ==[@book.pid]
160
91
 
161
- describe "of has_many_and_belongs_to" do
162
- before do
163
- @topic1 = Topic.create
164
- @topic2 = Topic.create
165
- @book = Book.create
166
- end
167
- it "habtm should set and remove relationships bidirectionally" do
168
- @book.topics << @topic1
169
- @book.topics.should == [@topic1]
170
- @topic1.books.should == [@book]
171
- @topic1.reload.books.should == [@book]
92
+ end
172
93
 
173
- @book.topics.delete(@topic1)
174
- #@topic1.books.delete(@book)
175
- @book.topics.should == []
176
- @topic1.books.should == []
177
- end
178
- after do
179
- @topic1.delete
180
- @topic2.delete
181
- end
182
- end
183
- end
94
+ it "should let you set an array of objects" do
95
+ @library.books = [@book, @book2]
96
+ @library.books.should == [@book, @book2]
97
+ @library.save
184
98
 
185
-
99
+ @library.books = [@book]
100
+ @library.books.should == [@book]
101
+
102
+ end
103
+ it "should let you set an array of object ids" do
104
+ @library.book_ids = [@book.pid, @book2.pid]
105
+ @library.books.should == [@book, @book2]
106
+ end
186
107
 
108
+ it "setter should wipe out previously saved relations" do
109
+ @library.book_ids = [@book.pid, @book2.pid]
110
+ @library.book_ids = [@book2.pid]
111
+ @library.books.should == [@book2]
112
+
113
+ end
187
114
 
188
- describe "a saved instance" do
189
- describe "of belongs_to" do
190
- before do
191
- @library = Library.new()
192
- @library.save()
193
- @book = Book.new
194
- @book.save
195
- @person = Person.new
196
- @person.save
197
- end
198
- it "should have many books once it has been saved" do
199
- @library.books << @book
115
+ it "saving the parent should save the relationships on the children" do
116
+ @library.save
117
+ @library.books = [@book, @book2]
118
+ @library.save
119
+ @library = Library.find(@library.pid)
120
+ @library.books.should == [@book, @book2]
121
+ end
200
122
 
201
- @book.library.pid.should == @library.pid
202
- @library.books.reload
203
- @library.books.should == [@book]
204
123
 
205
- @library2 = Library.find(@library.pid)
206
- @library2.books.should == [@book]
207
- end
124
+ it "should let you lookup an array of objects with solr" do
125
+ @library.save
126
+ @book.library = @library
127
+ @book2.library = @library
128
+ @book.save
129
+ @book2.save
208
130
 
209
- it "should have a count once it has been saved" do
210
- @library.books << @book << Book.create
211
- @library.save
131
+ @library = Library.find(@library.pid)
132
+ @library.books.should == [@book, @book2]
133
+
134
+ solr_resp = @library.books(:response_format=>:solr)
135
+ solr_resp.size.should == 2
136
+ solr_resp[0]['id'].should == @book.pid
137
+ solr_resp[1]['id'].should == @book2.pid
138
+
139
+ end
212
140
 
213
- # @book.library.pid.should == @library.pid
214
- # @library.books.reload
215
- # @library.books.should == [@book]
216
141
 
217
- @library2 = Library.find(@library.pid)
218
- @library2.books.size.should == 2
219
- end
220
142
 
221
- it "should respect the :class_name parameter" do
222
- @book.author = @person
223
- @book.save
224
- Book.find(@book.id).author_id.should == @person.pid
225
- Book.find(@book.id).author.send(:find_target).should be_kind_of Person
143
+ after do
144
+ @book.delete
145
+ @book2.delete
146
+ end
226
147
  end
227
148
 
228
- describe "when changing the belonger" do
149
+ describe "of belongs to" do
229
150
  before do
230
- @book.library = @library
151
+ @library = Library.new()
152
+ @library.save
153
+ @book = Book.new
231
154
  @book.save
232
- @library2 = Library.create
233
155
  end
234
- it "should replace an existing instance" do
235
- @book.library_id.should == @library.id
236
- @book.library = @library2
237
- @book.save
238
- Book.find(@book.id).library_id.should == @library2.id
156
+ it "shouldn't do anything if you set a nil id" do
157
+ @book.library_id = nil
158
+ end
159
+ it "should be settable from the book side" do
160
+ @book.library_id = @library.pid
161
+ @book.library.should == @library
162
+ @book.library.pid.should == @library.pid
163
+ @book.attributes= {:library_id => ""}
164
+ @book.library_id.should be_nil
239
165
  end
240
166
  after do
241
- @library2.delete
167
+ @library.delete
168
+ @book.delete
242
169
  end
243
170
  end
244
171
 
245
- after do
246
- @library.delete
247
- @book.delete
248
- end
249
- end
250
- describe "of has_many_and_belongs_to" do
251
- before do
252
- @book = Book.create
253
- end
254
- after do
255
- @book.delete
256
- end
257
- describe "when invese is specified" do
172
+ describe "of has_many_and_belongs_to" do
258
173
  before do
259
174
  @topic1 = Topic.create
260
175
  @topic2 = Topic.create
176
+ @book = Book.create
261
177
  end
262
- it "should set relationships bidirectionally" do
178
+ it "habtm should set and remove relationships bidirectionally" do
263
179
  @book.topics << @topic1
264
180
  @book.topics.should == [@topic1]
265
- @book.relationships(:has_topic).should == [@topic1.internal_uri]
266
- @topic1.relationships(:has_topic).should == []
267
- @topic1.relationships(:is_topic_of).should == [@book.internal_uri]
268
- Topic.find(@topic1.pid).books.should == [@book] #Can't have saved it because @book isn't saved yet.
269
- end
270
- it "should save new child objects" do
271
- @book.topics << Topic.new
272
- @book.topics.first.pid.should_not be_nil
273
- end
274
- it "should clear out the old associtions" do
275
- @book.topics = [@topic1]
276
- @book.topics = [@topic2]
277
- @book.topic_ids.should == [@topic2.pid]
181
+ @topic1.books.should == [@book]
182
+ @topic1.reload.books.should == [@book]
183
+
184
+ @book.topics.delete(@topic1)
185
+ #@topic1.books.delete(@book)
186
+ @book.topics.should == []
187
+ @topic1.books.should == []
278
188
  end
279
189
  after do
280
190
  @topic1.delete
281
191
  @topic2.delete
282
192
  end
283
193
  end
284
- describe "when invese is not specified" do
194
+ end
195
+
196
+
197
+
198
+
199
+ describe "a saved instance" do
200
+ describe "of belongs_to" do
285
201
  before do
286
- @c = Collection.create
287
- @book.collections << @c
202
+ @library = Library.new()
203
+ @library.save()
204
+ @book = Book.new
288
205
  @book.save
206
+ @person = Person.new
207
+ @person.save
208
+ end
209
+ it "should have many books once it has been saved" do
210
+ @library.books << @book
211
+
212
+ @book.library.pid.should == @library.pid
213
+ @library.books.reload
214
+ @library.books.should == [@book]
215
+
216
+ @library2 = Library.find(@library.pid)
217
+ @library2.books.should == [@book]
289
218
  end
219
+
220
+ it "should have a count once it has been saved" do
221
+ @library.books << @book << Book.create
222
+ @library.save
223
+
224
+ # @book.library.pid.should == @library.pid
225
+ # @library.books.reload
226
+ # @library.books.should == [@book]
227
+
228
+ @library2 = Library.find(@library.pid)
229
+ @library2.books.size.should == 2
230
+ end
231
+
232
+ it "should respect the :class_name parameter" do
233
+ @book.author = @person
234
+ @book.save
235
+ Book.find(@book.id).author_id.should == @person.pid
236
+ Book.find(@book.id).author.send(:find_target).should be_kind_of Person
237
+ end
238
+
239
+ describe "when changing the belonger" do
240
+ before do
241
+ @book.library = @library
242
+ @book.save
243
+ @library2 = Library.create
244
+ end
245
+ it "should replace an existing instance" do
246
+ @book.library_id.should == @library.id
247
+ @book.library = @library2
248
+ @book.save
249
+ Book.find(@book.id).library_id.should == @library2.id
250
+ end
251
+ after do
252
+ @library2.delete
253
+ end
254
+ end
255
+
290
256
  after do
291
- @c.delete
257
+ @library.delete
258
+ @book.delete
292
259
  end
293
- it "should have a collection" do
294
- @book.relationships(:is_member_of_collection).should == [@c.internal_uri]
295
- @book.collections.should == [@c]
260
+ end
261
+ describe "of has_many_and_belongs_to" do
262
+ before do
263
+ @book = Book.create
296
264
  end
297
- it "habtm should not set foreign relationships if :inverse_of is not specified" do
298
- @c.relationships(:is_member_of_collection).should == []
265
+ after do
266
+ @book.delete
299
267
  end
300
- it "should load the collections" do
301
- reloaded = Book.find(@book.pid)
302
- reloaded.collections.should == [@c]
268
+ describe "when invese is specified" do
269
+ before do
270
+ @topic1 = Topic.create
271
+ @topic2 = Topic.create
272
+ end
273
+ it "should set relationships bidirectionally" do
274
+ @book.topics << @topic1
275
+ @book.topics.should == [@topic1]
276
+ @book.relationships(:has_topic).should == [@topic1.internal_uri]
277
+ @topic1.relationships(:has_topic).should == []
278
+ @topic1.relationships(:is_topic_of).should == [@book.internal_uri]
279
+ Topic.find(@topic1.pid).books.should == [@book] #Can't have saved it because @book isn't saved yet.
280
+ end
281
+ it "should save new child objects" do
282
+ @book.topics << Topic.new
283
+ @book.topics.first.pid.should_not be_nil
284
+ end
285
+ it "should clear out the old associtions" do
286
+ @book.topics = [@topic1]
287
+ @book.topics = [@topic2]
288
+ @book.topic_ids.should == [@topic2.pid]
289
+ end
290
+ after do
291
+ @topic1.delete
292
+ @topic2.delete
293
+ end
294
+ end
295
+ describe "when invese is not specified" do
296
+ before do
297
+ @c = Collection.create
298
+ @book.collections << @c
299
+ @book.save
300
+ end
301
+ after do
302
+ @c.delete
303
+ end
304
+ it "should have a collection" do
305
+ @book.relationships(:is_member_of_collection).should == [@c.internal_uri]
306
+ @book.collections.should == [@c]
307
+ end
308
+ it "habtm should not set foreign relationships if :inverse_of is not specified" do
309
+ @c.relationships(:is_member_of_collection).should == []
310
+ end
311
+ it "should load the collections" do
312
+ reloaded = Book.find(@book.pid)
313
+ reloaded.collections.should == [@c]
314
+ end
303
315
  end
304
316
  end
305
317
  end
306
- end
307
318
 
308
- describe "setting belongs_to" do
309
- before do
310
- @library = Library.new()
311
- @library.save()
312
- @book = Book.new
313
- end
314
- it "should set the association" do
315
- @book.library = @library
316
- @book.library.pid.should == @library.pid
317
- @book.save
319
+ describe "setting belongs_to" do
320
+ before do
321
+ @library = Library.new()
322
+ @library.save()
323
+ @book = Book.new
324
+ end
325
+ it "should set the association" do
326
+ @book.library = @library
327
+ @book.library.pid.should == @library.pid
328
+ @book.save
318
329
 
319
330
 
320
- Book.find(@book.pid).library.pid.should == @library.pid
321
-
322
- end
323
- it "should clear the association" do
324
- @book.library = @library
325
- @book.library = nil
326
- @book.save
331
+ Book.find(@book.pid).library.pid.should == @library.pid
332
+
333
+ end
334
+ it "should clear the association" do
335
+ @book.library = @library
336
+ @book.library = nil
337
+ @book.save
327
338
 
328
- Book.find(@book.pid).library.should be_nil
329
-
330
- end
339
+ Book.find(@book.pid).library.should be_nil
340
+
341
+ end
331
342
 
332
- it "should replace the association" do
333
- @library2 = Library.new
334
- @library2.save
335
- @book.library = @library
336
- @book.save
337
- @book.library = @library2
338
- @book.save
339
- Book.find(@book.pid).library.pid.should == @library2.pid
343
+ it "should replace the association" do
344
+ @library2 = Library.new
345
+ @library2.save
346
+ @book.library = @library
347
+ @book.save
348
+ @book.library = @library2
349
+ @book.save
350
+ Book.find(@book.pid).library.pid.should == @library2.pid
340
351
 
341
- end
352
+ end
342
353
 
343
- it "should be able to be set by id" do
344
- @book.library_id = @library.pid
345
- @book.library_id.should == @library.pid
346
- @book.library.pid.should == @library.pid
347
- @book.save
348
- Book.find(@book.pid).library_id.should == @library.pid
349
- end
354
+ it "should be able to be set by id" do
355
+ @book.library_id = @library.pid
356
+ @book.library_id.should == @library.pid
357
+ @book.library.pid.should == @library.pid
358
+ @book.save
359
+ Book.find(@book.pid).library_id.should == @library.pid
360
+ end
350
361
 
351
- after do
352
- @library.delete
353
- @book.delete
354
- @library2.delete if @library2
362
+ after do
363
+ @library.delete
364
+ @book.delete
365
+ @library2.delete if @library2
366
+ end
355
367
  end
356
368
  end
357
369
 
@@ -503,7 +515,11 @@ describe ActiveFedora::Base do
503
515
  @master.media_object = @media
504
516
  @master.save
505
517
  @master.reload
518
+ end
506
519
 
520
+ after :all do
521
+ Object.send(:remove_const, :MasterFile)
522
+ Object.send(:remove_const, :MediaObject)
507
523
  end
508
524
 
509
525
  it "should also remove the relationships that point at that object" do
@@ -513,5 +529,80 @@ describe ActiveFedora::Base do
513
529
  end
514
530
  end
515
531
 
532
+ describe "has_many" do
533
+ describe "when an object doesn't have a property, and the class_name is predictable" do
534
+ before (:all) do
535
+ class Bauble < ActiveFedora::Base
536
+ belongs_to :media_object, property: :is_part_of
537
+ end
538
+ class MediaObject < ActiveFedora::Base
539
+ has_many :baubles
540
+ end
541
+ end
542
+ after :all do
543
+ Object.send(:remove_const, :Bauble)
544
+ Object.send(:remove_const, :MediaObject)
545
+ end
546
+
547
+ it "it should find the predicate" do
548
+ MediaObject.new.baubles.send(:find_predicate).should == :is_part_of
549
+ end
550
+ end
551
+
552
+ describe "when an object doesn't have a property, but has a class_name" do
553
+ before (:all) do
554
+ class MasterFile < ActiveFedora::Base
555
+ belongs_to :media_object, property: :is_part_of
556
+ end
557
+ class MediaObject < ActiveFedora::Base
558
+ has_many :parts, :class_name=>'MasterFile'
559
+ end
560
+ end
561
+ after :all do
562
+ Object.send(:remove_const, :MasterFile)
563
+ Object.send(:remove_const, :MediaObject)
564
+ end
565
+
566
+ it "it should find the predicate" do
567
+ MediaObject.new.parts.send(:find_predicate).should == :is_part_of
568
+ end
569
+ end
570
+
571
+ describe "an object has an explicity property" do
572
+ before (:all) do
573
+ class Bauble < ActiveFedora::Base
574
+ belongs_to :media_object, property: :is_part_of
575
+ end
576
+ class MediaObject < ActiveFedora::Base
577
+ has_many :baubles, property: :has_baubles
578
+ end
579
+ end
580
+ after :all do
581
+ Object.send(:remove_const, :Bauble)
582
+ Object.send(:remove_const, :MediaObject)
583
+ end
584
+
585
+ it "it should find the predicate" do
586
+ MediaObject.new.baubles.send(:find_predicate).should == :has_baubles
587
+ end
588
+ end
589
+ describe "an object doesn't have a property" do
590
+ before (:all) do
591
+ class Bauble < ActiveFedora::Base
592
+ belongs_to :media_object, property: :is_part_of
593
+ end
594
+ class MediaObject < ActiveFedora::Base
595
+ has_many :shoes
596
+ end
597
+ end
598
+ after :all do
599
+ Object.send(:remove_const, :Bauble)
600
+ Object.send(:remove_const, :MediaObject)
601
+ end
516
602
 
603
+ it "it should find the predicate" do
604
+ expect { MediaObject.new.shoes.send(:find_predicate) }.to raise_error RuntimeError, "No :property attribute was set or could be inferred for has_many :shoes on MediaObject"
605
+ end
606
+ end
607
+ end
517
608
  end
@@ -27,6 +27,19 @@ describe "Nested Rdf Objects" do
27
27
  ds = SpecDatastream.new(mock_obj)
28
28
  end
29
29
 
30
+ describe "#new_record?" do
31
+ it "should be true when its built" do
32
+ v = ds.parts.build(label: 'Alternator')
33
+ v.should be_new_record
34
+ end
35
+
36
+ it "should not be new when it's loaded from fedora" do
37
+ ds.content = '_:g70324142325120 <http://purl.org/dc/terms/title> "Alternator" .
38
+ <info:fedora/test:124> <http://purl.org/dc/terms/hasPart> _:g70324142325120 .'
39
+ ds.parts.first.should_not be_new_record
40
+ end
41
+ end
42
+
30
43
  it "should not choke on invalid data" do
31
44
  # set a string in the graph where model expects a node
32
45
  ds.parts = ["foo"]
@@ -3,8 +3,8 @@ require 'spec_helper'
3
3
  describe "When two or more relationships share the same property" do
4
4
  before do
5
5
  class Book < ActiveFedora::Base
6
- has_many :collections, :property=>:is_part_of, :class_name=>'Collection'
7
- has_many :people, :property=>:is_part_of
6
+ has_many :collections, :class_name=>'Collection'
7
+ has_many :people
8
8
  end
9
9
 
10
10
  class Person < ActiveFedora::Base
@@ -70,18 +70,20 @@ describe "Nesting attribute behavior of RDFDatastream" do
70
70
  let(:params) do
71
71
  { myResource:
72
72
  {
73
- topic_attributes: [
73
+ topic_attributes: {
74
+ '0' =>
74
75
  {
75
76
  elementList_attributes: [{
76
77
  topicElement:"Cosmology"
77
78
  }]
78
79
  },
80
+ '1' =>
79
81
  {
80
82
  elementList_attributes: [{
81
83
  topicElement:"Quantum Behavior"
82
84
  }]
83
85
  }
84
- ],
86
+ },
85
87
  personalName_attributes: [
86
88
  {
87
89
  elementList_attributes: [{
@@ -153,6 +155,11 @@ describe "Nesting attribute behavior of RDFDatastream" do
153
155
  subject.parts.map{|p| p.label.first}.should == ['Alternator', 'Universal Joint', 'Transmission', 'Oil Pump']
154
156
 
155
157
  end
158
+ it "should raise an error when the object isn't found" do
159
+ expect {
160
+ subject.parts_attributes= [{id: '_:g70192865051320', label: "Universal Joint"}]
161
+ }.to raise_error ActiveFedora::RecordNotFound
162
+ end
156
163
  end
157
164
  end
158
165
  end
@@ -45,7 +45,7 @@ describe ActiveFedora::QualifiedDublinCoreDatastream do
45
45
  end
46
46
 
47
47
  it "should create the right number of fields" do
48
- ActiveFedora::QualifiedDublinCoreDatastream::DCTERMS.size.should == 53
48
+ ActiveFedora::QualifiedDublinCoreDatastream::DCTERMS.size.should == 54
49
49
  end
50
50
 
51
51
  it "should have unmodifiable constants" do
@@ -114,6 +114,7 @@ describe ActiveFedora::QualifiedDublinCoreDatastream do
114
114
  end
115
115
 
116
116
  end
117
+
117
118
  describe 'custom fields' do
118
119
  it 'should grab the term' do
119
120
  sample_xml = "<dc xmlns:dcterms='http://purl.org/dc/terms/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><dcterms:cust>custom</dcterms:cust></dc>"
@@ -123,4 +124,12 @@ describe ActiveFedora::QualifiedDublinCoreDatastream do
123
124
  end
124
125
  end
125
126
 
127
+ describe "#field should accept :path option" do
128
+ it "should be able to map :dc_type to the path 'type'" do
129
+ test_ds = ActiveFedora::QualifiedDublinCoreDatastream.from_xml(@sample_xml)
130
+ test_ds.field :dc_type, :string, path: "type", multiple: true
131
+ test_ds.dc_type.should == ['sound']
132
+ end
133
+ end
134
+
126
135
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active-fedora
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.4.0
4
+ version: 6.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Zumwalt
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-07-01 00:00:00.000000000 Z
13
+ date: 2013-07-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rsolr
@@ -384,6 +384,8 @@ files:
384
384
  - lib/generators/active_fedora/config/solr/templates/solr_conf/solr.xml
385
385
  - lib/generators/active_fedora/model/USAGE
386
386
  - lib/generators/active_fedora/model/model_generator.rb
387
+ - lib/generators/active_fedora/model/templates/datastream.rb.erb
388
+ - lib/generators/active_fedora/model/templates/datastream_spec.rb.erb
387
389
  - lib/generators/active_fedora/model/templates/model.rb.erb
388
390
  - lib/generators/active_fedora/model/templates/model_spec.rb.erb
389
391
  - lib/tasks/active_fedora.rake