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_enabled[R]
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]
special_values[R]
validator[R]
value_translation[R]
word_size[R]

Public Class Methods

new(definition, field = nil, aliases: [], complete_enabled: true, complete_value: nil, default_operator: nil, default_order: nil, ext_method: nil, full_text_search: nil, in_key: nil, offset: nil, on: field, on_key: nil, only_explicit: nil, operators: nil, profile: nil, relation: nil, rename: nil, special_values: [], validator: nil, value_translation: nil, word_size: 1, **kwargs) click to toggle source

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

Field name may be given in positional ‘field’ argument or ‘on’ named argument.

   # File lib/scoped_search/definition.rb
27 def initialize(definition,
28                field = nil,
29                aliases: [],
30                complete_enabled: true,
31                complete_value: nil,
32                default_operator: nil,
33                default_order: nil,
34                ext_method: nil,
35                full_text_search: nil,
36                in_key: nil,
37                offset: nil,
38                on: field,
39                on_key: nil,
40                only_explicit: nil,
41                operators: nil,
42                profile: nil,
43                relation: nil,
44                rename: nil,
45                special_values: [],
46                validator: nil,
47                value_translation: nil,
48                word_size: 1,
49                **kwargs)
50 
51   # Prefer 'on' kw arg if given, defaults to the 'field' positional to allow either syntax
52   raise ArgumentError, "Missing field or 'on' keyword argument" if on.nil?
53   @field = on.to_sym
54 
55   raise ArgumentError, "'special_values' must be an Array" unless special_values.kind_of?(Array)
56 
57   # Reserved Ruby keywords so access via kwargs instead, but deprecate them for future versions
58   if kwargs.key?(:in)
59     relation = kwargs.delete(:in)
60     ActiveSupport::Deprecation.warn("'in' argument deprecated, prefer 'relation' since scoped_search 4.0.0", caller(6))
61   end
62   if kwargs.key?(:alias)
63     aliases += [kwargs.delete(:alias)]
64     ActiveSupport::Deprecation.warn("'alias' argument deprecated, prefer aliases: [..] since scoped_search 4.0.0", caller(6))
65   end
66   raise ArgumentError, "Unknown arguments to scoped_search: #{kwargs.keys.join(', ')}" unless kwargs.empty?
67 
68   @definition = definition
69   @definition.profile = profile if profile
70   @definition.default_order ||= generate_default_order(default_order, rename || @field) if default_order
71 
72   # Set attributes from keyword arguments
73   @complete_enabled = complete_enabled
74   @complete_value   = complete_value
75   @default_operator = default_operator
76   @ext_method       = ext_method
77   @full_text_search = full_text_search
78   @key_field        = on_key
79   @key_relation     = in_key
80   @offset           = offset
81   @only_explicit    = !!only_explicit
82   @operators        = operators
83   @relation         = relation
84   @special_values   = special_values
85   @validator        = validator
86   @word_size        = word_size
87   @value_translation = value_translation
88 
89   # Store this field in the field array
90   definition.define_field(rename || @field, self)
91 
92   # Store definition for aliases as well
93   aliases.each { |al| definition.define_field(al, self) }
94 end

Public Instance Methods

column() click to toggle source

Returns the ActiveRecord column definition that corresponds to this field.

    # File lib/scoped_search/definition.rb
122 def column
123   @column ||= begin
124     if klass.columns_hash.has_key?(field.to_s)
125       klass.columns_hash[field.to_s]
126     else
127       raise ActiveRecord::UnknownAttributeError.new(klass, field)
128     end
129   end
130 end
date?() click to toggle source

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

    # File lib/scoped_search/definition.rb
143 def date?
144   type == :date
145 end
datetime?() click to toggle source

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

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

Returns the default search operator for this field.

    # File lib/scoped_search/definition.rb
173 def default_operator
174   @default_operator ||= case type
175     when :string, :text, :citext then :like
176     else :eq
177   end
178 end
generate_default_order(default_order, field) click to toggle source
    # File lib/scoped_search/definition.rb
180 def generate_default_order(default_order, field)
181   order = (default_order.to_s.downcase.include?('desc')) ? "DESC" : "ASC"
182   return "#{field} #{order}"
183 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
106 def key_klass
107   @key_klass ||= if key_relation
108     definition.reflection_by_name(definition.klass, key_relation).klass
109   elsif relation
110     definition.reflection_by_name(definition.klass, relation).klass
111   else
112     definition.klass
113   end
114 end
klass() click to toggle source

The ActiveRecord-based class that belongs to this field.

    # File lib/scoped_search/definition.rb
 97 def klass
 98   @klass ||= if relation
 99     definition.reflection_by_name(definition.klass, relation).klass
100   else
101     definition.klass
102   end
103 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
154 def numerical?
155   [:integer, :double, :float, :decimal].include?(type)
156 end
quoted_field() click to toggle source

Return ‘table’.‘column’ with the correct database quotes.

    # File lib/scoped_search/definition.rb
186 def quoted_field
187   c = klass.connection
188   "#{c.quote_table_name(klass.table_name)}.#{c.quote_column_name(field)}"
189 end
set?() click to toggle source

Returns true if this is a set.

    # File lib/scoped_search/definition.rb
168 def set?
169   complete_value.is_a?(Hash)
170 end
temporal?() click to toggle source

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

    # File lib/scoped_search/definition.rb
148 def temporal?
149   datetime? || date?
150 end
textual?() click to toggle source

Returns true if this is a textual column.

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

Returns the column type of this field.

    # File lib/scoped_search/definition.rb
133 def type
134   @type ||= virtual? ? :virtual : column.type
135 end
uuid?() click to toggle source
    # File lib/scoped_search/definition.rb
163 def uuid?
164   type == :uuid
165 end
virtual?() click to toggle source

Returns true if this is a virtual field.

    # File lib/scoped_search/definition.rb
117 def virtual?
118   !ext_method.nil?
119 end