Module: Searchlogic::NamedScopes::Conditions

Included in:
ActiveRecord::Base
Defined in:
lib/searchlogic/named_scopes/conditions.rb

Overview

Handles dynamically creating named scopes for columns. It allows you to do things like:

User.first_name_like("ben")
User.id_lt(10)

Notice the constants in this class, they define which conditions Searchlogic provides.

See the README for a more detailed explanation.

Constant Summary collapse

COMPARISON_CONDITIONS =
{
  :equals => [:is, :eq],
  :does_not_equal => [:not_equal_to, :is_not, :not, :ne],
  :less_than => [:lt, :before],
  :less_than_or_equal_to => [:lte],
  :greater_than => [:gt, :after],
  :greater_than_or_equal_to => [:gte],
}
WILDCARD_CONDITIONS =
{
  :like => [:contains, :includes],
  :not_like => [],
  :begins_with => [:bw],
  :not_begin_with => [:does_not_begin_with],
  :ends_with => [:ew],
  :not_end_with => [:does_not_end_with]
}
BOOLEAN_CONDITIONS =
{
  :null => [:nil],
  :not_null => [:not_nil],
  :empty => [],
  :blank => [],
  :not_blank => [:present]
}
GROUP_CONDITIONS =
{
  :in => [],
  :not_in => []
}
CONDITIONS =
{}
PRIMARY_CONDITIONS =
CONDITIONS.keys
ALIAS_CONDITIONS =
CONDITIONS.values.flatten

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/searchlogic/named_scopes/conditions.rb', line 79

def method_missing(name, *args, &block)
  if details = condition_details(name)
    create_condition(details[:column], details[:condition], args)
    send(name, *args)
  elsif boolean_condition?(name)
    column = name.to_s.gsub(/^not_/, "")
    named_scope name, :conditions => {column => (name.to_s =~ /^not_/).nil?}
    send(name)
  else
    super
  end
end

Instance Method Details

#condition?(name) ⇒ Boolean

Is the name of the method a valid condition that can be dynamically created?

Returns:

  • (Boolean)


63
64
65
# File 'lib/searchlogic/named_scopes/conditions.rb', line 63

def condition?(name)
  local_condition?(name)
end