Module: SearchApi::Integration::ActiveRecord::Base

Defined in:
lib/search_api/active_record_integration.rb

Overview

This module allows the ActiveRecord::Base classes to transparently integrate SearchApi::Search::Base features.

It is included in a ActiveRecord::Base subclass by calling has_search_api:

class People < ActiveRecord::Base
  has_search_api

  # define age search key
  search :age do |search|
    { :conditions => ['birth_date BETWEEN ? AND ?',
                      (Date.today-search.age.years),
                      (Date.today-(search.age-1).years+1.day)]}
  end
end

People.find(:all, :conditions => {:first_name => 'Roger', :age => 30})

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object

Modifies the class including that module so that :find, :count and :with_scope methods have support for search keys added by the search method.

Don’t include yourself this module ! Instead, use ActiveRecord::Base.has_search_api method.



35
36
37
38
39
40
# File 'lib/search_api/active_record_integration.rb', line 35

def self.append_features(base)
  super
  base.alias_method_chain(:find, :search_support)
  base.alias_method_chain(:count, :search_support)
  base.alias_method_chain(:with_scope, :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.



55
56
57
58
59
60
61
62
63
64
# File 'lib/search_api/active_record_integration.rb', line 55

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

#find_with_search_support(*args) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
# File 'lib/search_api/active_record_integration.rb', line 43

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

#search(name, options = {}, &block) ⇒ Object

Extends the keys that conditions hashes can hold.

ActiveRecord::Base.find can take a :conditions option. This option can be raw SQL, a SQL fragment such as ['a=?',1], or a condition hash such as {:column1 => value, ;column2 => value}.

The search method allows you to extend the keys that condition hash can hold.

For instance, assuming a :birth_date column exists in your table, you can define the :age search key:

class People < ActiveRecord::Base
  has_search_api

  # define age search key
  search :age do |search|
    { :conditions => ['birth_date BETWEEN ? AND ?',
                      (Date.today-search.age.years),
                      (Date.today-(search.age-1).years+1.day)]}
  end
end

The options parameter allows you to define some search keys without providing a block:

class People < ActiveRecord::Base
  has_search_api

  search :keyword, :operator => :full_text, :columns => [:first_name, :last_name, :email]
  search :email_domain, :operator => :ends_with, :column => :email
end

For further details, see:

  • how search attributes are defined: SearchApi::Search::Base.search_accessor;

  • which options are understood: SearchApi::Bridge::ActiveRecord#rewrite_search_attribute_builder method.



113
114
115
# File 'lib/search_api/active_record_integration.rb', line 113

def search(name, options={}, &block)
  search_class.search_accessor(name, options, &block)
end

#with_scope_with_search_support(method_scoping = {}, action = :merge, &block) ⇒ Object

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



67
68
69
70
71
72
73
74
75
# File 'lib/search_api/active_record_integration.rb', line 67

def with_scope_with_search_support(method_scoping = {}, action = :merge, &block)
  if method_scoping[:find] && method_scoping[:find][:conditions] && method_scoping[:find][:conditions].is_a?(Hash)
    with_scope_without_search_support(:find => search_class.new(method_scoping[:find].delete(:conditions)).find_options) do
      with_scope_without_search_support(method_scoping, action, &block)
    end
  else
    with_scope_without_search_support(method_scoping, action, &block)
  end
end