Class: ScopedSearch::Definition::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/scoped_search/definition.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(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) ⇒ Field

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.

Raises:

  • (ArgumentError)

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/scoped_search/definition.rb', line 27

def initialize(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)

  # Prefer 'on' kw arg if given, defaults to the 'field' positional to allow either syntax
  raise ArgumentError, "Missing field or 'on' keyword argument" if on.nil?
  @field = on.to_sym

  raise ArgumentError, "'special_values' must be an Array" unless special_values.kind_of?(Array)

  # Reserved Ruby keywords so access via kwargs instead, but deprecate them for future versions
  if kwargs.key?(:in)
    relation = kwargs.delete(:in)
    ActiveSupport::Deprecation.warn("'in' argument deprecated, prefer 'relation' since scoped_search 4.0.0", caller(6))
  end
  if kwargs.key?(:alias)
    aliases += [kwargs.delete(:alias)]
    ActiveSupport::Deprecation.warn("'alias' argument deprecated, prefer aliases: [..] since scoped_search 4.0.0", caller(6))
  end
  raise ArgumentError, "Unknown arguments to scoped_search: #{kwargs.keys.join(', ')}" unless kwargs.empty?

  @definition = definition
  @definition.profile = profile if profile
  @definition.default_order ||= generate_default_order(default_order, rename || @field) if default_order

  # Set attributes from keyword arguments
  @complete_enabled = complete_enabled
  @complete_value   = complete_value
  @default_operator = default_operator
  @ext_method       = ext_method
  @full_text_search = full_text_search
  @key_field        = on_key
  @key_relation     = in_key
  @offset           = offset
  @only_explicit    = !!only_explicit
  @operators        = operators
  @relation         = relation
  @special_values   = special_values
  @validator        = validator
  @word_size        = word_size
  @value_translation = value_translation

  # Store this field in the field array
  definition.define_field(rename || @field, self)

  # Store definition for aliases as well
  aliases.each { |al| definition.define_field(al, self) }
end

Instance Attribute Details

#complete_enabledObject (readonly)

Returns the value of attribute complete_enabled


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def complete_enabled
  @complete_enabled
end

#complete_valueObject (readonly)

Returns the value of attribute complete_value


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def complete_value
  @complete_value
end

#definitionObject (readonly)

Returns the value of attribute definition


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def definition
  @definition
end

#ext_methodObject (readonly)

Returns the value of attribute ext_method


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def ext_method
  @ext_method
end

#fieldObject (readonly)

Returns the value of attribute field


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def field
  @field
end

#full_text_searchObject (readonly)

Returns the value of attribute full_text_search


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def full_text_search
  @full_text_search
end

#key_fieldObject (readonly)

Returns the value of attribute key_field


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def key_field
  @key_field
end

#key_relationObject (readonly)

Returns the value of attribute key_relation


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def key_relation
  @key_relation
end

#offsetObject (readonly)

Returns the value of attribute offset


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def offset
  @offset
end

#only_explicitObject (readonly)

Returns the value of attribute only_explicit


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def only_explicit
  @only_explicit
end

#operatorsObject (readonly)

Returns the value of attribute operators


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def operators
  @operators
end

#relationObject (readonly)

Returns the value of attribute relation


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def relation
  @relation
end

#special_valuesObject (readonly)

Returns the value of attribute special_values


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def special_values
  @special_values
end

#validatorObject (readonly)

Returns the value of attribute validator


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def validator
  @validator
end

#value_translationObject (readonly)

Returns the value of attribute value_translation


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def value_translation
  @value_translation
end

#word_sizeObject (readonly)

Returns the value of attribute word_size


18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def word_size
  @word_size
end

Instance Method Details

#columnObject

Returns the ActiveRecord column definition that corresponds to this field.


122
123
124
125
126
127
128
129
130
# File 'lib/scoped_search/definition.rb', line 122

def column
  @column ||= begin
    if klass.columns_hash.has_key?(field.to_s)
      klass.columns_hash[field.to_s]
    else
      raise ActiveRecord::UnknownAttributeError.new(klass, field)
    end
  end
end

#date?Boolean

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

Returns:

  • (Boolean)

143
144
145
# File 'lib/scoped_search/definition.rb', line 143

def date?
  type == :date
end

#datetime?Boolean

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

Returns:

  • (Boolean)

138
139
140
# File 'lib/scoped_search/definition.rb', line 138

def datetime?
  [:datetime, :time, :timestamp].include?(type)
end

#default_operatorObject

Returns the default search operator for this field.


173
174
175
176
177
178
# File 'lib/scoped_search/definition.rb', line 173

def default_operator
  @default_operator ||= case type
    when :string, :text then :like
    else :eq
  end
end

#generate_default_order(default_order, field) ⇒ Object


180
181
182
183
# File 'lib/scoped_search/definition.rb', line 180

def generate_default_order(default_order, field)
  order = (default_order.to_s.downcase.include?('desc')) ? "DESC" : "ASC"
  return "#{field} #{order}"
end

#key_klassObject

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


106
107
108
109
110
111
112
113
114
# File 'lib/scoped_search/definition.rb', line 106

def key_klass
  @key_klass ||= if key_relation
    definition.reflection_by_name(definition.klass, key_relation).klass
  elsif relation
    definition.reflection_by_name(definition.klass, relation).klass
  else
    definition.klass
  end
end

#klassObject

The ActiveRecord-based class that belongs to this field.


97
98
99
100
101
102
103
# File 'lib/scoped_search/definition.rb', line 97

def klass
  @klass ||= if relation
    definition.reflection_by_name(definition.klass, relation).klass
  else
    definition.klass
  end
end

#numerical?Boolean

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

Returns:

  • (Boolean)

154
155
156
# File 'lib/scoped_search/definition.rb', line 154

def numerical?
  [:integer, :double, :float, :decimal].include?(type)
end

#quoted_fieldObject

Return 'table'.'column' with the correct database quotes.


186
187
188
189
# File 'lib/scoped_search/definition.rb', line 186

def quoted_field
  c = klass.connection
  "#{c.quote_table_name(klass.table_name)}.#{c.quote_column_name(field)}"
end

#set?Boolean

Returns true if this is a set.

Returns:

  • (Boolean)

168
169
170
# File 'lib/scoped_search/definition.rb', line 168

def set?
  complete_value.is_a?(Hash)
end

#temporal?Boolean

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

Returns:

  • (Boolean)

148
149
150
# File 'lib/scoped_search/definition.rb', line 148

def temporal?
  datetime? || date?
end

#textual?Boolean

Returns true if this is a textual column.

Returns:

  • (Boolean)

159
160
161
# File 'lib/scoped_search/definition.rb', line 159

def textual?
  [:string, :text].include?(type)
end

#typeObject

Returns the column type of this field.


133
134
135
# File 'lib/scoped_search/definition.rb', line 133

def type
  @type ||= virtual? ? :virtual : column.type
end

#uuid?Boolean

Returns:

  • (Boolean)

163
164
165
# File 'lib/scoped_search/definition.rb', line 163

def uuid?
  type == :uuid
end

#virtual?Boolean

Returns true if this is a virtual field.

Returns:

  • (Boolean)

117
118
119
# File 'lib/scoped_search/definition.rb', line 117

def virtual?
  !ext_method.nil?
end