Sha256: d2b9c20fb85fbaac763a24e171eb9d4538fc877d20aae5bfd43ca49b7273eec9

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

module ThinkingSphinx
  module ActiveRecord
    module CollectionProxy
      def search(*args)
        options   = args.extract_options!
        options[:with] ||= {}
        options[:with].merge! default_filter

        args << options
        proxy_association.klass.search(*args)
      end

      private

      def attribute_for_foreign_key
        if proxy_association.reflection.through_reflection
          foreign_key = proxy_association.reflection.through_reflection.foreign_key
        else
          foreign_key = proxy_association.reflection.foreign_key
        end

        proxy_association.klass.define_indexes
        (proxy_association.klass.sphinx_indexes || []).each do |index|
          attribute = index.attributes.detect { |attrib|
            attrib.columns.length == 1 &&
            attrib.columns.first.__name  == foreign_key.to_sym ||
            attrib.alias == foreign_key.to_sym
          }
          return attribute unless attribute.nil?
        end

        raise "Missing Attribute for Foreign Key #{foreign_key}"
      end

      def default_filter
        {attribute_for_foreign_key.unique_name => proxy_association.owner.id}
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
thinking-sphinx-2.0.12 lib/thinking_sphinx/active_record/collection_proxy.rb
thinking-sphinx-2.0.11 lib/thinking_sphinx/active_record/collection_proxy.rb
thinking-sphinx-2.0.10 lib/thinking_sphinx/active_record/collection_proxy.rb
thinking-sphinx-2.0.9 lib/thinking_sphinx/active_record/collection_proxy.rb
thinking-sphinx-2.0.8 lib/thinking_sphinx/active_record/collection_proxy.rb
thinking-sphinx-2.0.7 lib/thinking_sphinx/active_record/collection_proxy.rb