Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting .best_model_for from AF::Base #270

Merged
merged 1 commit into from
Nov 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions lib/active_fedora/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,7 @@ def adapt_to(klass)
# cases above exists in the :has_model, then instantiate as that extended
# model type instead.
def adapt_to_cmodel
best_model_match = self.class unless self.instance_of? ActiveFedora::Base

ActiveFedora::ContentModel.known_models_for( self ).each do |model_value|
# If this is of type ActiveFedora::Base, then set to the first found :has_model.
best_model_match ||= model_value

# If there is an inheritance structure, use the most specific case.
if best_model_match > model_value
best_model_match = model_value
end
end
best_model_match = ActiveFedora::ContentModel.best_model_for(self)

self.instance_of?(best_model_match) ? self : self.adapt_to(best_model_match)
end
Expand Down
16 changes: 16 additions & 0 deletions lib/active_fedora/content_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ def self.models_asserted_by(obj)
obj.relationships(:has_model)
end

def self.best_model_for(obj)
best_model_match = obj.class unless obj.instance_of? ActiveFedora::Base

known_models_for(obj).each do |model_value|
# If this is of type ActiveFedora::Base, then set to the first found :has_model.
best_model_match ||= model_value

# If there is an inheritance structure, use the most specific case.
if best_model_match > model_value
best_model_match = model_value
end
end

best_model_match
end

# returns an array of the model classes that are defined in the current
# application that the given object asserts (ie. if the object asserts
# a StreamingVideo model but the application doesn't define a
Expand Down
19 changes: 19 additions & 0 deletions spec/unit/content_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ class NamespacedModel < ActiveFedora::Base
@test_cmodel.should respond_to(:pid_suffix=)
end

describe '.best_model_for' do
it 'should be the input model if no relationships' do
mock_object = BaseModel.new
mock_object.should_receive(:relationships).with(:has_model).and_return([])
expect(ActiveFedora::ContentModel.best_model_for(mock_object)).to eq BaseModel
end

it 'should be based on inheritance hierarchy' do
mock_object = ActiveFedora::Base.new
mock_object.should_receive(:relationships).with(:has_model).and_return(["info:fedora/fedora-system:ServiceDefinition-3.0", 'info:fedora/afmodel:SampleModel', 'info:fedora/afmodel:BaseModel'])
expect(ActiveFedora::ContentModel.best_model_for(mock_object)).to eq SampleModel
end

it 'should find the deepest descendant of the on inheritance hierarchy' do
mock_object = BaseModel.new
mock_object.should_receive(:relationships).with(:has_model).and_return(["info:fedora/fedora-system:ServiceDefinition-3.0", 'info:fedora/afmodel:SampleModel', 'info:fedora/afmodel:BaseModel'])
expect(ActiveFedora::ContentModel.best_model_for(mock_object)).to eq SampleModel
end
end

describe "models_asserted_by" do
it "should return an array of all of the content models asserted by the given object" do
Expand Down