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],
  :between => [:is_between, :in_range],
  :between_inclusive => [:is_between_inclusive, :in_range_inclusive]
}
WILDCARD_CONDITIONS =
{
  :like => [:contains, :includes],
  :not_like => [:does_not_include],
  :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]
}
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)



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/searchlogic/named_scopes/conditions.rb', line 74

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)


58
59
60
# File 'lib/searchlogic/named_scopes/conditions.rb', line 58

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