Sha256: 9030b13805ad9511fced18264a988f2d9c156618510a421ab0571ed21343e595

Contents?: true

Size: 1.27 KB

Versions: 50

Compression:

Stored size: 1.27 KB

Contents

module ActiveFedora
  module Associations
    class AssociationScope #:nodoc:
      def self.scope(association)
        new(association).scope
      end

      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.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.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.zero?
            is_first_chain ? klass : reflection.klass
          end

          scope
        end
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
active-fedora-13.3.0 lib/active_fedora/associations/association_scope.rb
active-fedora-12.2.4 lib/active_fedora/associations/association_scope.rb
active-fedora-13.2.7 lib/active_fedora/associations/association_scope.rb
active-fedora-13.2.5 lib/active_fedora/associations/association_scope.rb
active-fedora-13.2.4 lib/active_fedora/associations/association_scope.rb
active-fedora-12.2.3 lib/active_fedora/associations/association_scope.rb
active-fedora-13.2.3 lib/active_fedora/associations/association_scope.rb
active-fedora-13.2.2 lib/active_fedora/associations/association_scope.rb
active-fedora-13.2.0 lib/active_fedora/associations/association_scope.rb
active-fedora-13.1.3 lib/active_fedora/associations/association_scope.rb
active-fedora-11.5.6 lib/active_fedora/associations/association_scope.rb
active-fedora-12.2.2 lib/active_fedora/associations/association_scope.rb
active-fedora-11.2.1 lib/active_fedora/associations/association_scope.rb
active-fedora-12.2.1 lib/active_fedora/associations/association_scope.rb
active-fedora-12.0.3 lib/active_fedora/associations/association_scope.rb
active-fedora-11.5.5 lib/active_fedora/associations/association_scope.rb
active-fedora-13.1.2 lib/active_fedora/associations/association_scope.rb
active-fedora-13.1.1 lib/active_fedora/associations/association_scope.rb
active-fedora-13.1.0 lib/active_fedora/associations/association_scope.rb
active-fedora-13.0.0 lib/active_fedora/associations/association_scope.rb