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
Public Class Methods
Source
# 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
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.
Public Instance Methods
Source
# 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
Returns the ActiveRecord column definition that corresponds to this field.
Source
# File lib/scoped_search/definition.rb 143 def date? 144 type == :date 145 end
Returns true if this field is a date-like column.
Source
# File lib/scoped_search/definition.rb 138 def datetime? 139 [:datetime, :time, :timestamp].include?(type) 140 end
Returns true if this field is a datetime-like column.
Source
# 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
Returns the default search operator for this field.
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
Source
# 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
The ActiveRecord-based class that belongs the key field in a key-value pair.
Source
# 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
The ActiveRecord-based class that belongs to this field.
Source
# File lib/scoped_search/definition.rb 154 def numerical? 155 [:integer, :double, :float, :decimal].include?(type) 156 end
Returns true if this field is numerical. Numerical means either integer, floating point or fixed point.
Source
# 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
Return ‘table’.‘column’ with the correct database quotes.
Source
# File lib/scoped_search/definition.rb 168 def set? 169 complete_value.is_a?(Hash) 170 end
Returns true if this is a set.
Source
# File lib/scoped_search/definition.rb 148 def temporal? 149 datetime? || date? 150 end
Returns true if this field is a date or datetime-like column.
Source
# File lib/scoped_search/definition.rb 159 def textual? 160 [:string, :text, :citext].include?(type) 161 end
Returns true if this is a textual column.
Source
# File lib/scoped_search/definition.rb 133 def type 134 @type ||= virtual? ? :virtual : column.type 135 end
Returns the column type of this field.
Source
# File lib/scoped_search/definition.rb 117 def virtual? 118 !ext_method.nil? 119 end
Returns true if this is a virtual field.