Module SearchApi::Integration::ActiveRecord::Base
In: lib/active_record_integration.rb

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})

Methods

Public Class methods

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.

[Source]

    # File lib/active_record_integration.rb, line 35
35:         def self.append_features(base)
36:           super
37:           base.alias_method_chain(:find, :search_support)
38:           base.alias_method_chain(:count, :search_support)
39:           base.alias_method_chain(:with_scope, :search_support)
40:         end

Public Instance methods

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

[Source]

    # File lib/active_record_integration.rb, line 55
55:         def count_with_search_support(*args)
56:           options = if args.last.is_a?(Hash) then args.last else {} end
57:           if options[:conditions].nil? || options[:conditions].is_a?(Hash)
58:             with_scope(:find => search_class.new(options.delete(:conditions)).find_options) do
59:               count_without_search_support(*args)
60:             end
61:           else
62:             count_without_search_support(*args)
63:           end
64:         end

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

[Source]

    # File lib/active_record_integration.rb, line 43
43:         def find_with_search_support(*args)
44:           options = if args.last.is_a?(Hash) then args.last else {} end
45:           if options[:conditions].nil? || options[:conditions].is_a?(Hash)
46:             with_scope(:find => search_class.new(options.delete(:conditions)).find_options) do
47:               find_without_search_support(*args)
48:             end
49:           else
50:             find_without_search_support(*args)
51:           end
52:         end

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.

[Source]

     # File lib/active_record_integration.rb, line 113
113:         def search(name, options={}, &block)
114:           search_class.search_accessor(name, options, &block)
115:         end

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

[Source]

    # File lib/active_record_integration.rb, line 67
67:         def with_scope_with_search_support(method_scoping = {}, action = :merge, &block)
68:           if method_scoping[:find] && method_scoping[:find][:conditions] && method_scoping[:find][:conditions].is_a?(Hash)
69:             with_scope_without_search_support(:find => search_class.new(method_scoping[:find].delete(:conditions)).find_options) do
70:               with_scope_without_search_support(method_scoping, action, &block)
71:             end
72:           else
73:             with_scope_without_search_support(method_scoping, action, &block)
74:           end
75:         end

[Validate]