Sha256: 570645e8f1f7adbca7c2026ed2f85fd178113c88794f5a872440be28b82409c6

Contents?: true

Size: 1.2 KB

Versions: 31

Compression:

Stored size: 1.2 KB

Contents

module ActiveFedora
  module Associations
    class AssociationScope #:nodoc:

      attr_reader :association

      delegate :klass, :owner, :reflection, :interpolate, :to => :association
      delegate :chain, :scope_chain, :options, :source_options, :active_record, :to => :reflection

      def initialize(association)
        @association = association
      end

      def scope
        scope = klass.unscoped
        add_constraints(scope)
      end

      private

      def add_constraints(scope)
        chain.each_with_index do |reflection, i|
          if reflection.source_macro == :belongs_to
            # Create a partial solr query using the ids. We may add additional filters such as class_name later
            scope = scope.where( ActiveFedora::SolrQueryBuilder.construct_query_for_ids([owner[reflection.foreign_key]]))
          elsif reflection.source_macro == :has_and_belongs_to_many
          else
            scope = scope.where( ActiveFedora::SolrQueryBuilder.construct_query_for_rel(association.send(:find_reflection) => owner.id))
          end

          is_first_chain = i == 0
          klass = is_first_chain ? self.klass : reflection.klass
        end

        scope
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
active-fedora-9.0.8 lib/active_fedora/associations/association_scope.rb
active-fedora-9.5.0 lib/active_fedora/associations/association_scope.rb
active-fedora-9.4.3 lib/active_fedora/associations/association_scope.rb
active-fedora-9.4.2 lib/active_fedora/associations/association_scope.rb
active-fedora-9.4.1 lib/active_fedora/associations/association_scope.rb
active-fedora-9.4.0 lib/active_fedora/associations/association_scope.rb
active-fedora-9.3.0 lib/active_fedora/associations/association_scope.rb
active-fedora-9.2.1 lib/active_fedora/associations/association_scope.rb
active-fedora-9.2.0 lib/active_fedora/associations/association_scope.rb
active-fedora-9.2.0.rc2 lib/active_fedora/associations/association_scope.rb
active-fedora-9.2.0.rc1 lib/active_fedora/associations/association_scope.rb
active-fedora-9.1.2 lib/active_fedora/associations/association_scope.rb
active-fedora-9.1.1 lib/active_fedora/associations/association_scope.rb
active-fedora-9.1.0 lib/active_fedora/associations/association_scope.rb
active-fedora-9.1.0.rc1 lib/active_fedora/associations/association_scope.rb
active-fedora-9.0.6 lib/active_fedora/associations/association_scope.rb
active-fedora-9.0.5 lib/active_fedora/associations/association_scope.rb
active-fedora-9.0.4 lib/active_fedora/associations/association_scope.rb
active-fedora-9.0.3 lib/active_fedora/associations/association_scope.rb
active-fedora-9.0.2 lib/active_fedora/associations/association_scope.rb