Module: Searchlogic::NamedScopes::ColumnConditions

Defined in:
lib/searchlogic/named_scopes/column_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

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 => [: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

- (Object) method_missing(name, *args, &block) (private)



77
78
79
80
81
82
83
# File 'lib/searchlogic/named_scopes/column_conditions.rb', line 77

def method_missing(name, *args, &block)
  if create_condition(name)
    send(name, *args, &block)
  else
    super
  end
end

Instance Method Details

- (Boolean) condition?(name)

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

Returns:

  • (Boolean)


56
57
58
# File 'lib/searchlogic/named_scopes/column_conditions.rb', line 56

def condition?(name)
  super || column_condition?(name)
end

- (Boolean) respond_to_missing?(*args)

We want to return true for any conditions that can be called, and while we're at it. We might as well create the condition so we don't have to do it again.

Returns:

  • (Boolean)


62
63
64
# File 'lib/searchlogic/named_scopes/column_conditions.rb', line 62

def respond_to_missing?(*args)
  super || (self != ::ActiveRecord::Base && !self.abstract_class? && !create_condition(args.first).blank?)
end