Skip to content

Commit

Permalink
Merge branch 'master' into issues-jrgriffiniii-1317-hydra-samvera
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Pendragon authored Sep 17, 2018
2 parents f823a21 + 534b65c commit 139ebe0
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/active_fedora/associations/collection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def initialize(association)
merge! association.scope(nullify: false)
end

delegate :each, to: :to_a

def target
@association.target
end
Expand Down
15 changes: 14 additions & 1 deletion lib/active_fedora/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ module ActiveFedora
# = Active Fedora Persistence
module Persistence
extend ActiveSupport::Concern
extend ActiveSupport::Autoload
autoload :NullIdentifierService

included do
class_attribute :identifier_service_class
self.identifier_service_class = NullIdentifierService
end

def new_record?
return true if @ldp_source.subject.nil?
Expand Down Expand Up @@ -209,7 +216,13 @@ def execute_sparql_update
end

# Override to tie in an ID minting service
def assign_id; end
def assign_id
identifier_service.mint
end

def identifier_service
@identifier_service ||= identifier_service_class.new
end

# This is only used when creating a new record. If the object doesn't have an id
# and assign_id can mint an id for the object, then assign it to the resource.
Expand Down
11 changes: 11 additions & 0 deletions lib/active_fedora/persistence/null_identifier_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveFedora
module Persistence
# An identifier service that doesn't mint IDs, so that the autocreated
# identifiers from Fedora will be used.
class NullIdentifierService
# Effectively a no-op
# @return [NilClass]
def mint; end
end
end
end
10 changes: 10 additions & 0 deletions lib/active_fedora/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ def scope_for_create
@scope_for_create ||= where_values_hash.merge(create_with_value)
end

def each
if loaded?
@records.each { |item| yield item } if block_given?
@records.to_enum
else
find_each(where_values) { |item| yield item } if block_given?
enum_for(:find_each, where_values)
end
end

private

VALID_FIND_OPTIONS = [:order, :limit, :start, :conditions, :cast].freeze
Expand Down
3 changes: 2 additions & 1 deletion lib/active_fedora/relation/delegation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module Delegation # :nodoc:
:keep_if, :pop, :shift, :delete_at, :select!
].to_set

delegate :length, :collect, :map, :each, :all?, :include?, :to_ary, to: :to_a
delegate :length, :map, :to_ary, to: :to_a
delegate :all?, :blank?, :collect, :include?, :present?, to: :each

protected

Expand Down
34 changes: 34 additions & 0 deletions spec/integration/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ class Book < ActiveFedora::Base
expect_any_instance_of(ActiveFedora::Relation).to_not receive :find_each
libraries[0]
end

it "does not reload" do
expect_any_instance_of(ActiveFedora::Relation).to_not receive :find_each
libraries.each { |l| l.id }
end
end

describe '#each' do
before { Book.create }

it 'returns an enumerator' do
expect(libraries.each).to be_a Enumerator
end

it 'yields the items' do
expect { |b| libraries.each(&b) }
.to yield_successive_args(*Library.all.to_a)
end

it 'when called from Base yields all items' do
expect { |b| ActiveFedora::Base.all.each(&b) }
.to yield_successive_args(*(Library.all.to_a + Book.all.to_a))
end

context 'when cached' do
it 'returns an enumerator' do
expect(libraries.each).to be_a Enumerator
end

it 'yields the items' do
expect { |b| libraries.each(&b) }
.to yield_successive_args(*Library.all.to_a)
end
end
end

describe "#find" do
Expand Down

0 comments on commit 139ebe0

Please sign in to comment.