Module: SearchApi::Integration::ActiveRecord::Associations::Count

Defined in:
lib/search_api/active_record_integration.rb

Overview

Module that holds integration of SearchApi::Search::Base into ActiveRecord::Associations::HasManyAssociation.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object

:nodoc:



166
167
168
169
# File 'lib/search_api/active_record_integration.rb', line 166

def self.append_features(base) #:nodoc:
  super
  base.alias_method_chain(:count, :search_support)
end

Instance Method Details

#count_with_search_support(*args) ⇒ Object

Alteration of the :count method that has support for search keys added by the search method.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/search_api/active_record_integration.rb', line 172

def count_with_search_support(*args)
  if @reflection.klass.respond_to?(:search_class)
    options = if args.last.is_a?(Hash) then args.last else {} end
    if options[:conditions].nil? || options[:conditions].is_a?(Hash)
      @reflection.klass.send(:with_scope, :find => @reflection.klass.search_class.new(options.delete(:conditions)).find_options) do
        count_without_search_support(*args)
      end
    else
      count_without_search_support(*args)
    end
  else
    count_without_search_support(*args)
  end
end