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
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 validator: nil, 46 word_size: 1, 47 **kwargs) 48 49 # Prefer 'on' kw arg if given, defaults to the 'field' positional to allow either syntax 50 raise ArgumentError, "Missing field or 'on' keyword argument" if on.nil? 51 @field = on.to_sym 52 53 # Reserved Ruby keywords so access via kwargs instead, but deprecate them for future versions 54 if kwargs.key?(:in) 55 relation = kwargs.delete(:in) 56 ActiveSupport::Deprecation.warn("'in' argument deprecated, prefer 'relation' since scoped_search 4.0.0", caller(6)) 57 end 58 if kwargs.key?(:alias) 59 aliases += [kwargs.delete(:alias)] 60 ActiveSupport::Deprecation.warn("'alias' argument deprecated, prefer aliases: [..] since scoped_search 4.0.0", caller(6)) 61 end 62 raise ArgumentError, "Unknown arguments to scoped_search: #{kwargs.keys.join(', ')}" unless kwargs.empty? 63 64 @definition = definition 65 @definition.profile = profile if profile 66 @definition.default_order ||= generate_default_order(default_order, rename || @field) if default_order 67 68 # Set attributes from keyword arguments 69 @complete_enabled = complete_enabled 70 @complete_value = complete_value 71 @default_operator = default_operator 72 @ext_method = ext_method 73 @full_text_search = full_text_search 74 @key_field = on_key 75 @key_relation = in_key 76 @offset = offset 77 @only_explicit = !!only_explicit 78 @operators = operators 79 @relation = relation 80 @validator = validator 81 @word_size = word_size 82 83 # Store this field in the field array 84 definition.define_field(rename || @field, self) 85 86 # Store definition for aliases as well 87 aliases.each { |al| definition.define_field(al, self) } 88 end
Public Instance Methods
Returns the ActiveRecord column definition that corresponds to this field.
# File lib/scoped_search/definition.rb 111 def column 112 @column ||= begin 113 if klass.columns_hash.has_key?(field.to_s) 114 klass.columns_hash[field.to_s] 115 else 116 raise ActiveRecord::UnknownAttributeError.new(klass, field) 117 end 118 end 119 end
Returns true if this field is a date-like column
# File lib/scoped_search/definition.rb 132 def date? 133 type == :date 134 end
Returns true if this field is a datetime-like column
# File lib/scoped_search/definition.rb 127 def datetime? 128 [:datetime, :time, :timestamp].include?(type) 129 end
Returns the default search operator for this field.
# File lib/scoped_search/definition.rb 158 def default_operator 159 @default_operator ||= case type 160 when :string, :text then :like 161 else :eq 162 end 163 end
# File lib/scoped_search/definition.rb 165 def generate_default_order(default_order, field) 166 order = (default_order.to_s.downcase.include?('desc')) ? "DESC" : "ASC" 167 return "#{field} #{order}" 168 end
The ActiveRecord-based class that belongs the key field in a key-value pair.
# File lib/scoped_search/definition.rb 100 def key_klass 101 @key_klass ||= if key_relation 102 definition.reflection_by_name(definition.klass, key_relation).klass 103 elsif relation 104 definition.reflection_by_name(definition.klass, relation).klass 105 else 106 definition.klass 107 end 108 end
The ActiveRecord-based class that belongs to this field.
# File lib/scoped_search/definition.rb 91 def klass 92 @klass ||= if relation 93 definition.reflection_by_name(definition.klass, relation).klass 94 else 95 definition.klass 96 end 97 end
Returns true if this field is numerical. Numerical means either integer, floating point or fixed point.
# File lib/scoped_search/definition.rb 143 def numerical? 144 [:integer, :double, :float, :decimal].include?(type) 145 end
Return 'table'.'column' with the correct database quotes
# File lib/scoped_search/definition.rb 171 def quoted_field 172 c = klass.connection 173 "#{c.quote_table_name(klass.table_name)}.#{c.quote_column_name(field)}" 174 end
Returns true if this is a set.
# File lib/scoped_search/definition.rb 153 def set? 154 complete_value.is_a?(Hash) 155 end
Returns true if this field is a date or datetime-like column.
# File lib/scoped_search/definition.rb 137 def temporal? 138 datetime? || date? 139 end
Returns true if this is a textual column.
# File lib/scoped_search/definition.rb 148 def textual? 149 [:string, :text].include?(type) 150 end
Returns the column type of this field.
# File lib/scoped_search/definition.rb 122 def type 123 @type ||= column.type 124 end