class ScopedSearch::Definition::Field

The Field class specifies a field of a model that is available for searching, in what cases this field should be searched and its default search behavior.

Instances of this class are created when calling scoped_search in your model class, so you should not create instances of this class yourself.

Attributes

complete_value[R]
definition[R]
ext_method[R]
field[R]
key_field[R]
key_relation[R]
offset[R]
only_explicit[R]
operators[R]
relation[R]
word_size[R]

Public Instance Methods

column() click to toggle source

Returns the ActiveRecord column definition that corresponds to this field.

# File lib/scoped_search/definition.rb, line 81
def column
  @column ||= begin
    if klass.columns_hash.has_key?(field.to_s)
      klass.columns_hash[field.to_s]
    else
      raise ActiveRecord::UnknownAttributeError, "#{klass.inspect} doesn't have column #{field.inspect}."
    end
  end
end
date?() click to toggle source

Returns true if this field is a date-like column

# File lib/scoped_search/definition.rb, line 102
def date?
  type == :date
end
datetime?() click to toggle source

Returns true if this field is a datetime-like column

# File lib/scoped_search/definition.rb, line 97
def datetime?
  [:datetime, :time, :timestamp].include?(type)
end
default_operator() click to toggle source

Returns the default search operator for this field.

# File lib/scoped_search/definition.rb, line 128
def default_operator
  @default_operator ||= case type
    when :string, :text then :like
    else :eq
  end
end
default_order(options) click to toggle source
# File lib/scoped_search/definition.rb, line 135
def default_order(options)
  return nil if options[:default_order].nil?
  field_name = options[:on] unless options[:rename]
  field_name = options[:rename] if options[:rename]
  order = (options[:default_order].to_s.downcase.include?('desc')) ? "DESC" : "ASC"
  return "#{field_name} #{order}"
end
key_klass() click to toggle source

The ActiveRecord-based class that belongs the key field in a key-value pair.

# File lib/scoped_search/definition.rb, line 70
def key_klass
  @key_klass ||= if key_relation
    definition.klass.reflections[key_relation].klass
  elsif relation
    definition.klass.reflections[relation].klass
  else
    definition.klass
  end
end
klass() click to toggle source

The ActiveRecord-based class that belongs to this field.

# File lib/scoped_search/definition.rb, line 59
def klass
  @klass ||= if relation
    related = definition.klass.reflections[relation]
    raise ScopedSearch::QueryNotSupported, "relation '#{relation}' not one of #{definition.klass.reflections.keys.join(', ')} " if related.nil?
    related.klass
  else
    definition.klass
  end
end
numerical?() click to toggle source

Returns true if this field is numerical. Numerical means either integer, floating point or fixed point.

# File lib/scoped_search/definition.rb, line 113
def numerical?
  [:integer, :double, :float, :decimal].include?(type)
end
set?() click to toggle source

Returns true if this is a set.

# File lib/scoped_search/definition.rb, line 123
def set?
  complete_value.is_a?(Hash)
end
temporal?() click to toggle source

Returns true if this field is a date or datetime-like column.

# File lib/scoped_search/definition.rb, line 107
def temporal?
  datetime? || date?
end
textual?() click to toggle source

Returns true if this is a textual column.

# File lib/scoped_search/definition.rb, line 118
def textual?
  [:string, :text].include?(type)
end
type() click to toggle source

Returns the column type of this field.

# File lib/scoped_search/definition.rb, line 92
def type
  @type ||= column.type
end

Public Class Methods

new(definition, options = {}) click to toggle source

Initializes a Field instance given the definition passed to the scoped_search call on the ActiveRecord-based model class.

# File lib/scoped_search/definition.rb, line 23
def initialize(definition, options = {})
  @definition = definition
  @definition.profile = options[:profile] if options[:profile]
  @definition.default_order ||= default_order(options)

  case options
  when Symbol, String
    @field = field.to_sym
  when Hash
    @field = options.delete(:on)

    # Set attributes from options hash
    @complete_value   = options[:complete_value]
    @relation         = options[:in]
    @key_relation     = options[:in_key]
    @key_field        = options[:on_key]
    @offset           = options[:offset]
    @word_size        = options[:word_size] || 1
    @ext_method       = options[:ext_method]
    @operators        = options[:operators]
    @only_explicit    = !!options[:only_explicit]
    @full_text_search = options[:full_text_search]
    @default_operator = options[:default_operator] if options.has_key?(:default_operator)
  end

  # Store this field is the field array
  definition.fields[@field]                  ||= self unless options[:rename]
  definition.fields[options[:rename].to_sym] ||= self if     options[:rename]
  definition.unique_fields                   << self

  # Store definition for alias / aliases as well
  definition.fields[options[:alias].to_sym]                  ||= self   if options[:alias]
  options[:aliases].each { |al| definition.fields[al.to_sym] ||= self } if options[:aliases]
end