-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
225 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
lib/active_fedora/associations/basic_contains_association.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module ActiveFedora | ||
module Associations | ||
class BasicContainsAssociation < ContainsAssociation #:nodoc: | ||
def find_target | ||
uris = owner.resource.query(predicate: options[:predicate]) | ||
.map { |r| r.object.to_s } | ||
|
||
uris.map { |object_uri| klass.find(klass.uri_to_id(object_uri)) } | ||
end | ||
|
||
def insert_record(record, force = true, validate = true) | ||
record.base_path_for_resource = owner.uri.to_s | ||
super | ||
end | ||
|
||
def add_to_target(record, skip_callbacks = false) | ||
record.base_path_for_resource = owner.uri.to_s | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module ActiveFedora::Associations::Builder | ||
class BasicContains < CollectionAssociation #:nodoc: | ||
def self.macro | ||
:is_a_container | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
require 'spec_helper' | ||
|
||
describe ActiveFedora::Base do | ||
let(:model) { Source.new } | ||
|
||
context "with a file" do | ||
before do | ||
class Source < ActiveFedora::Base | ||
is_a_container | ||
end | ||
end | ||
|
||
after do | ||
Object.send(:remove_const, :Source) | ||
end | ||
|
||
it 'is empty' do | ||
expect(model.contains).to eq [] | ||
end | ||
|
||
it 'can build a child' do | ||
child = model.contains.build | ||
expect(child).to be_kind_of ActiveFedora::File | ||
child.content = "hello" | ||
model.save! | ||
expect(child).to be_persisted | ||
expect(child.uri.to_s).to include model.uri.to_s | ||
end | ||
|
||
it 'can create a child on a persisted parent' do | ||
model.save! | ||
child = model.contains.build | ||
expect(child).to be_kind_of ActiveFedora::File | ||
child.content = "hello" | ||
model.save! | ||
expect(child).to be_persisted | ||
expect(child.uri.to_s).to include model.uri.to_s | ||
end | ||
end | ||
|
||
context "with an AF::Base object" do | ||
before do | ||
class Thing < ActiveFedora::Base | ||
property :title, predicate: ::RDF::Vocab::DC.title | ||
end | ||
class Source < ActiveFedora::Base | ||
is_a_container class_name: 'Thing' | ||
end | ||
end | ||
after do | ||
Object.send(:remove_const, :Source) | ||
Object.send(:remove_const, :Thing) | ||
end | ||
|
||
let(:model) { Source.new } | ||
|
||
it 'is empty' do | ||
expect(model.contains).to eq [] | ||
end | ||
|
||
describe "creating" do | ||
it 'can build a child' do | ||
child = model.contains.build | ||
expect(model.contains.build(title: ['my title'])).to be_kind_of Thing | ||
model.save! | ||
expect(child).to be_persisted | ||
expect(child.uri.to_s).to include model.uri.to_s | ||
end | ||
|
||
it 'can create a child on a persisted parent' do | ||
model.save! | ||
child = model.contains.create(title: ['my title']) | ||
expect(child).to be_kind_of Thing | ||
expect(child).to be_persisted | ||
expect(child.uri.to_s).to include model.uri.to_s | ||
end | ||
end | ||
|
||
describe "loading" do | ||
before do | ||
model.save! | ||
model.contains.create(title: ['title 1']) | ||
model.contains.create(title: ['title 2']) | ||
model.reload | ||
end | ||
|
||
it "has the two contained objects" do | ||
expect(model.contains.size).to eq 2 | ||
expect(model.contains.map(&:title)).to eq [['title 1'], ['title 2']] | ||
end | ||
end | ||
|
||
describe "#destroy_all" do | ||
before do | ||
model.save! | ||
model.contains.create(title: ['title 1']) | ||
model.contains.create(title: ['title 2']) | ||
model.reload | ||
end | ||
|
||
it "destroys the two contained objects" do | ||
expect { model.contains.destroy_all } | ||
.to change { model.contains.size }.by(-2) | ||
.and change { Thing.count }.by(-2) | ||
end | ||
end | ||
end | ||
end |