Skip to content

Commit

Permalink
Factor out the Ldp::Orm class
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Nov 10, 2014
1 parent cfb7949 commit 7fe03b9
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 47 deletions.
28 changes: 14 additions & 14 deletions lib/active_fedora/core.rb
Expand Up @@ -22,7 +22,10 @@ module Core
# Accepts a proc that takes a uri and transforms it to an id
mattr_accessor :translate_uri_to_id, instance_writer: false

attr_reader :orm
end

def ldp_source
@ldp_source
end

# Constructor. You may supply a custom +:id+, or we call the Fedora Rest API for the
Expand All @@ -31,8 +34,8 @@ module Core
# +:namespace+ value to Fedora::Repository.nextid to generate the next id available within
# the given namespace.
def initialize(attributes_or_resource_or_url = nil)
attributes = initialize_orm_and_attributes(attributes_or_resource_or_url)
raise IllegalOperation, "Attempting to recreate existing orm" unless @orm.new?
attributes = initialize_resource_and_attributes(attributes_or_resource_or_url)
raise IllegalOperation, "Attempting to recreate existing ldp_source" unless @ldp_source.new?
@association_cache = {}
assert_content_model
load_attached_files
Expand All @@ -45,7 +48,7 @@ def reload
check_persistence unless persisted?
clear_association_cache
clear_attached_files
@orm = Ldp::Orm.new(LdpResource.new(conn, uri))
@ldp_source = LdpResource.new(conn, uri)
@resource = nil
load_attached_files
self
Expand All @@ -61,7 +64,7 @@ def reload
# post.init_with_resource(Ldp::Resource.new('http://example.com/post/1'))
# post.title # => 'hello world'
def init_with_resource(rdf_resource)
@orm = Ldp::Orm.new(rdf_resource)
@ldp_source = rdf_resource
@association_cache = {}
load_attached_files
run_callbacks :find
Expand Down Expand Up @@ -178,13 +181,10 @@ def check_persistence
end
end

def initialize_orm_and_attributes attributes_or_resource_or_url
def initialize_resource_and_attributes attributes_or_resource_or_url
case attributes_or_resource_or_url
when Ldp::Resource::RdfSource
@orm = Ldp::Orm.new(subject_or_data)
attributes = get_attributes_from_orm(@orm)
when String
@orm = Ldp::Orm.new(build_ldp_resource(attributes_or_resource_or_url))
@ldp_source = build_ldp_resource(attributes_or_resource_or_url)
@attributes = {}.with_indifferent_access
when Hash
attributes = attributes_or_resource_or_url
Expand All @@ -198,13 +198,13 @@ def initialize_orm_and_attributes attributes_or_resource_or_url
end

attributes = attributes.with_indifferent_access if attributes
@orm = if id
Ldp::Orm.new(build_ldp_resource(id))
@ldp_source = if id
build_ldp_resource(id)
else
Ldp::Orm.new(build_ldp_resource)
build_ldp_resource
end
when NilClass
@orm = Ldp::Orm.new(build_ldp_resource)
@ldp_source = build_ldp_resource
attributes = {}.with_indifferent_access
else
raise ArgumentError, "#{attributes_or_resource_or_url.class} is not acceptable"
Expand Down
4 changes: 2 additions & 2 deletions lib/active_fedora/fedora_attributes.rb
Expand Up @@ -41,7 +41,7 @@ def pid

def uri
# TODO could we return a RDF::URI instead?
uri = @orm.try(:resource).try(:subject_uri)
uri = @ldp_source.try(:subject_uri)
uri.value == '' ? uri : uri.to_s
end

Expand All @@ -52,7 +52,7 @@ def uri
#
# set_value, get_value, and property accessors are delegated to this object.
def resource
@resource ||= self.class.resource_class.new(@orm.graph.rdf_subject, @orm.graph)
@resource ||= self.class.resource_class.new(@ldp_source.graph.rdf_subject, @ldp_source.graph)
end

module ClassMethods
Expand Down
4 changes: 2 additions & 2 deletions lib/active_fedora/indexing.rb
Expand Up @@ -87,8 +87,8 @@ def load_instance_from_solr(id, solr_doc=nil)
end

def get_descendent_uris(uri)
orm = Ldp::Orm.new(Ldp::Resource::RdfSource.new(ActiveFedora.fedora.connection, uri))
immediate_descendent_uris = orm.graph.query(predicate: RDF::LDP.contains).map { |descendent| descendent.object.to_s }
resource = Ldp::Resource::RdfSource.new(ActiveFedora.fedora.connection, uri)
immediate_descendent_uris = resource.graph.query(predicate: RDF::LDP.contains).map { |descendent| descendent.object.to_s }
all_descendents_uris = [uri]
immediate_descendent_uris.each do |descendent_uri|
all_descendents_uris += get_descendent_uris(descendent_uri)
Expand Down
1 change: 0 additions & 1 deletion lib/active_fedora/ldp_resource.rb
Expand Up @@ -22,6 +22,5 @@ def build_graph(original_graph)
# forces a cast to FedoraRdfResource
graph_without_inlined_resources(original_graph, inlined_resources)
end

end
end
2 changes: 1 addition & 1 deletion lib/active_fedora/loadable_from_json.rb
Expand Up @@ -92,7 +92,7 @@ def init_with_json(json)
attrs = JSON.parse(json)
id = attrs.delete('id')

@orm = Ldp::Orm.new(build_ldp_resource(id))
@ldp_source = build_ldp_resource(id)
@association_cache = {}
datastream_keys = self.class.child_resource_reflections.keys
datastream_keys.each do |key|
Expand Down
23 changes: 11 additions & 12 deletions lib/active_fedora/persistence.rb
Expand Up @@ -4,7 +4,7 @@ module Persistence
extend ActiveSupport::Concern

def new_record?
@orm.resource.new?
@ldp_source.new?
end

def persisted?
Expand Down Expand Up @@ -41,7 +41,7 @@ def update(attributes)
alias update_attributes update

def refresh
@orm = Ldp::Orm.new(LdpResource.new(conn, uri))
@ldp_source = LdpResource.new(conn, uri)
@resource = nil
end

Expand All @@ -60,9 +60,9 @@ def delete

id = self.id ## cache so it's still available after delete
# Clear out the ETag -- Remove when this bug is fixed: https://github.com/fcrepo4/fcrepo4/issues/442
orm.resource.instance_variable_set :@get, nil
@ldp_source.instance_variable_set :@get, nil
begin
@orm.resource.delete
@ldp_source.delete
rescue Ldp::NotFound
raise ObjectNotFoundError, "Unable to find #{id} in the repository"
end
Expand Down Expand Up @@ -120,8 +120,8 @@ def eradicate(uri)
gone?(uri) ? delete_tombstone(uri) : false
end

private
private

def gone? uri
ActiveFedora::Base.find(uri)
false
Expand All @@ -134,7 +134,6 @@ def delete_tombstone uri
ActiveFedora.fedora.connection.delete(tombstone)
true
end

end

protected
Expand All @@ -157,7 +156,7 @@ def update_needs_index?
def create_record(options = {})
assign_rdf_subject
serialize_attached_files
@orm = orm.create
@ldp_source = @ldp_source.create
@resource = nil
assign_uri_to_attached_files
should_update_index = create_needs_index? && options.fetch(:update_index, true)
Expand All @@ -169,7 +168,7 @@ def update_record(options = {})
serialize_attached_files

# Clear out the ETag -- Remove when this bug is fixed: https://github.com/fcrepo4/fcrepo4/issues/442
orm.resource.instance_variable_set :@get, nil
@ldp_source.instance_variable_set :@get, nil
result = execute_sparql_update
# result = orm.save
# Need to wait until this bug is fixed: https://github.com/fcrepo4/fcrepo4/issues/442
Expand All @@ -192,10 +191,10 @@ def assign_id
end

def assign_rdf_subject
if !id && new_id = assign_id
@orm = Ldp::Orm.new(LdpResource.new(ActiveFedora.fedora.connection, self.class.id_to_uri(new_id), @resource))
@ldp_source = if !id && new_id = assign_id
LdpResource.new(ActiveFedora.fedora.connection, self.class.id_to_uri(new_id), @resource)
else
@orm = Ldp::Orm.new(LdpResource.new(ActiveFedora.fedora.connection, @orm.resource.subject, @resource, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path))
LdpResource.new(ActiveFedora.fedora.connection, @ldp_source.subject, @resource, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path)
end
end

Expand Down
5 changes: 4 additions & 1 deletion spec/integration/delete_all_spec.rb
Expand Up @@ -38,13 +38,16 @@ def inc_counter
describe "when a model is missing" do
let(:model3) { SpecModel::Basic.create! }
let!(:id) { model3.id }
before { model3.orm.delete }

before { Ldp::Resource::RdfSource.new(ActiveFedora.fedora.connection, model3.uri).delete }

after do
ActiveFedora::SolrService.instance.conn.tap do |conn|
conn.delete_by_query "id:\"#{id}\""
conn.commit
end
end

it "should be able to skip a missing model" do
expect(ActiveFedora::Base.logger).to receive(:error).with("Although #{id} was found in Solr, it doesn't seem to exist in Fedora. The index is out of synch.")
SpecModel::Basic.destroy_all
Expand Down
5 changes: 3 additions & 2 deletions spec/integration/fedora_solr_sync_spec.rb
Expand Up @@ -18,10 +18,11 @@ class ChildThing < ActiveFedora::Base
end

let(:parent) { ParentThing.create }
subject { ChildThing.create :parent => parent }
subject { ChildThing.create parent: parent }

before { Ldp::Resource::RdfSource.new(ActiveFedora.fedora.connection, subject.uri).delete }

it "should not go into an infinite loop" do
subject.orm.delete
parent.reload
expect(ActiveFedora::Base.logger).to receive(:error).with("Solr and Fedora may be out of sync:\n")
expect(parent.things).to eq []
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/scoped_query_spec.rb
Expand Up @@ -114,7 +114,7 @@ def to_solr(doc = {})

describe "when one of the objects in solr isn't in fedora" do
let!(:id) { test_instance2.id }
before { test_instance2.orm.delete }
before { Ldp::Resource::RdfSource.new(ActiveFedora.fedora.connection, test_instance2.uri).delete }
after do
ActiveFedora::SolrService.instance.conn.tap do |conn|
conn.delete_by_query "id:\"#{id}\""
Expand Down
20 changes: 10 additions & 10 deletions spec/unit/base_extra_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'

describe ActiveFedora::Base do

describe ".update_index" do
before do
mock_conn = double("SolrConnection")
Expand All @@ -12,12 +12,12 @@
allow(mock_ss).to receive(:conn).and_return(mock_conn)
allow(ActiveFedora::SolrService).to receive(:instance).and_return(mock_ss)
end

it "should call .to_solr on all Datastreams and pass the resulting document to solr" do
# Actually uses self.to_solr internally to gather solr info from all metadata datastreams
mock1 = double("ds1", :to_solr => {})
mock2 = double("ds2", :to_solr => {})

mock_datastreams = {:ds1 => mock1, :ds2 => mock2}
expect(mock1).to receive(:to_solr).and_return({})
expect(mock2).to receive(:to_solr).and_return({})
Expand All @@ -31,33 +31,33 @@
end

end

describe ".delete" do

before do
allow(subject).to receive(:new_record?).and_return(false)
allow(subject.orm.resource.client).to receive(:delete)
allow(ActiveFedora.fedora.connection).to receive(:delete)
end

it "should delete object from repository and index" do
expect(ActiveFedora::SolrService).to receive(:delete).with(nil)
subject.delete
end
end

describe "to_class_uri" do
before :all do
module SpecModel
class CamelCased < ActiveFedora::Base
end
end
end

after :all do
SpecModel.send(:remove_const, :CamelCased)
end
subject {SpecModel::CamelCased.to_class_uri}

it { should == 'SpecModel::CamelCased' }
end
end
2 changes: 1 addition & 1 deletion spec/unit/base_spec.rb
Expand Up @@ -225,7 +225,7 @@ def increment_id
allow(@test_object).to receive(:new_record?).and_return(true)
expect(@test_object).to receive(:serialize_attached_files)
expect(@test_object).to receive(:assign_rdf_subject)
expect(@test_object.orm).to receive(:create)
expect(@test_object.ldp_source).to receive(:create)
expect(@test_object).to receive(:refresh)
expect(@test_object).to receive(:update_index)
@test_object.save
Expand Down

0 comments on commit 7fe03b9

Please sign in to comment.