Sha256: 1b1bab41b19498201ce840ea6dd1d26d85d79b4f117f4a87781faf049954395e

Contents?: true

Size: 1.97 KB

Versions: 69

Compression:

Stored size: 1.97 KB

Contents

module ActiveFedora::Associations
  class FilterAssociation < ::ActiveFedora::Associations::CollectionAssociation
    # @param [Array] records a list of records to replace the current association with
    # @raise [ArgumentError] if one of the records doesn't match the prescribed condition
    def writer(records)
      records.each { |r| validate_assertion!(r) }
      existing_matching_records.each do |r|
        extending_from.delete(r)
      end
      extending_from.concat(records)
    end

    delegate :delete, to: :extending_from

    # @param [Array] records a list of records to append to the current association
    # @raise [ArgumentError] if one of the records doesn't match the prescribed condition
    def concat(records)
      records.flatten.each { |r| validate_assertion!(r) }
      extending_from.concat(records)
    end

    def ids_reader
      load_target
      super
    end

    def count_records
      ids_reader.length
    end

    private

      # target should never be cached as part of this objects state, because
      # extending_from.target could change and we want to reflect those changes
      def target
        find_target
      end

      def find_target?
        true
      end

      def find_target
        existing_matching_records
      end

      # We can't create an association scope on here until we can figure a way to
      # index/query the condition in Solr
      def association_scope
        nil
      end

      def existing_matching_records
        extending_from.reader.to_a.select do |r|
          validate_assertion(r)
        end
      end

      def extending_from
        owner.association(options.fetch(:extending_from))
      end

      def validate_assertion(record)
        record.send(options.fetch(:condition))
      end

      def validate_assertion!(record)
        raise ArgumentError, "#{record.class} with ID: #{record.id} was expected to #{options.fetch(:condition)}, but it was false" unless validate_assertion(record)
      end
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
active-fedora-15.0.1 lib/active_fedora/associations/filter_association.rb
active-fedora-15.0.0 lib/active_fedora/associations/filter_association.rb
active-fedora-14.0.1 lib/active_fedora/associations/filter_association.rb
active-fedora-14.0.0 lib/active_fedora/associations/filter_association.rb
active-fedora-13.3.0 lib/active_fedora/associations/filter_association.rb
active-fedora-12.2.4 lib/active_fedora/associations/filter_association.rb
active-fedora-13.2.7 lib/active_fedora/associations/filter_association.rb
active-fedora-13.2.5 lib/active_fedora/associations/filter_association.rb
active-fedora-13.2.4 lib/active_fedora/associations/filter_association.rb
active-fedora-12.2.3 lib/active_fedora/associations/filter_association.rb
active-fedora-13.2.3 lib/active_fedora/associations/filter_association.rb
active-fedora-13.2.2 lib/active_fedora/associations/filter_association.rb
active-fedora-13.2.0 lib/active_fedora/associations/filter_association.rb
active-fedora-13.1.3 lib/active_fedora/associations/filter_association.rb
active-fedora-11.5.6 lib/active_fedora/associations/filter_association.rb
active-fedora-12.2.2 lib/active_fedora/associations/filter_association.rb
active-fedora-11.2.1 lib/active_fedora/associations/filter_association.rb
active-fedora-12.2.1 lib/active_fedora/associations/filter_association.rb
active-fedora-12.0.3 lib/active_fedora/associations/filter_association.rb
active-fedora-11.5.5 lib/active_fedora/associations/filter_association.rb