Sha256: 8ddd55f07cce583b0a938880468aadb2dddbee4f2ce748a30834239ef2422e7b

Contents?: true

Size: 1.32 KB

Versions: 11

Compression:

Stored size: 1.32 KB

Contents

module ThinkingSphinx
  module ActiveRecord
    module CollectionProxy
      def search(*args)
        proxy_association.klass.search(*association_args(args))
      end

      def facets(*args)
        proxy_association.klass.facets(*association_args(args))
      end

      private

      def association_args(args)
        options = args.extract_options!
        options[:with] ||= {}
        options[:with].merge! default_filter

        args + [options]
      end

      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

11 entries across 11 versions & 2 rubygems

Version Path
friendlyfashion-thinking-sphinx-2.0.14.4 lib/thinking_sphinx/active_record/collection_proxy.rb
friendlyfashion-thinking-sphinx-2.0.14.3 lib/thinking_sphinx/active_record/collection_proxy.rb
thinking-sphinx-2.1.0 lib/thinking_sphinx/active_record/collection_proxy.rb
friendlyfashion-thinking-sphinx-2.0.14.2 lib/thinking_sphinx/active_record/collection_proxy.rb
friendlyfashion-thinking-sphinx-2.0.14.1 lib/thinking_sphinx/active_record/collection_proxy.rb
thinking-sphinx-2.0.14 lib/thinking_sphinx/active_record/collection_proxy.rb
friendlyfashion-thinking-sphinx-2.0.13.3 lib/thinking_sphinx/active_record/collection_proxy.rb
friendlyfashion-thinking-sphinx-2.0.13.2 lib/thinking_sphinx/active_record/collection_proxy.rb
friendlyfashion-thinking-sphinx-2.0.13.1 lib/thinking_sphinx/active_record/collection_proxy.rb
friendlyfashion-thinking-sphinx-2.0.13 lib/thinking_sphinx/active_record/collection_proxy.rb
thinking-sphinx-2.0.13 lib/thinking_sphinx/active_record/collection_proxy.rb