Sha256: 0c164abff0962e080d9efbcca66bc19cb7b2e58b34c4288ff12f10e889c7613b

Contents?: true

Size: 1.41 KB

Versions: 24

Compression:

Stored size: 1.41 KB

Contents

module ThinkingSphinx
  module ActiveRecord
    module HasManyAssociation
      def search(*args)
        options   = args.extract_options!
        options[:with] ||= {}
        options[:with].merge! default_filter
        
        args << options
        @reflection.klass.search(*args)
      end
      
      def method_missing(method, *args, &block)
        if responds_to_scope(method)
          @reflection.klass.
            search(:with => default_filter).
            send(method, *args, &block)
        else
          super
        end
      end
      
      private
      
      def attribute_for_foreign_key
        foreign_key = @reflection.primary_key_name
        stack = [@reflection.options[:through]].compact
        
        @reflection.klass.define_indexes
        (@reflection.klass.sphinx_indexes || []).each do |index|
          attribute = index.attributes.detect { |attrib|
            attrib.columns.length == 1 &&
            attrib.columns.first.__name  == 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 => @owner.id}
      end
      
      def responds_to_scope(scope)
        @reflection.klass.respond_to?(:sphinx_scopes)   &&
        @reflection.klass.sphinx_scopes.include?(scope)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 7 rubygems

Version Path
thinking-sphinx-2.0.2 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-1.4.2 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-1.4.1 lib/thinking_sphinx/active_record/has_many_association.rb
joshcutler-thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-2.0.1 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-2.0.0 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-1.4.0 lib/thinking_sphinx/active_record/has_many_association.rb
thorsson_thinking-sphinx-2.0 lib/thinking_sphinx/active_record/has_many_association.rb
angelf-thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-1.3.20 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-2.0.0.rc2 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-1.3.19 lib/thinking_sphinx/active_record/has_many_association.rb
thorsson_thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-allen-1.3.18.4 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-allen-1.3.18.3 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-allen-1.3.18.2 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-2.0.0.rc1 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-allen-1.3.18.1 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-allen-1.3.18 lib/thinking_sphinx/active_record/has_many_association.rb
thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/has_many_association.rb