Class | ScopedSearch::Definition::Field |
In: |
lib/scoped_search/definition.rb
|
Parent: | Object |
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.
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] |
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 100 100: def initialize(definition, options = {}) 101: @definition = definition 102: @definition.profile = options[:profile] if options[:profile] 103: @definition.default_order ||= default_order(options) 104: 105: case options 106: when Symbol, String 107: @field = field.to_sym 108: when Hash 109: @field = options.delete(:on) 110: 111: # Set attributes from options hash 112: @complete_value = options[:complete_value] 113: @relation = options[:in] 114: @key_relation = options[:in_key] 115: @key_field = options[:on_key] 116: @offset = options[:offset] 117: @word_size = options[:word_size] || 1 118: @ext_method = options[:ext_method] 119: @operators = options[:operators] 120: @only_explicit = !!options[:only_explicit] 121: @default_operator = options[:default_operator] if options.has_key?(:default_operator) 122: end 123: 124: # Store this field is the field array 125: definition.fields[@field] ||= self unless options[:rename] 126: definition.fields[options[:rename].to_sym] ||= self if options[:rename] 127: definition.unique_fields << self 128: 129: # Store definition for alias / aliases as well 130: definition.fields[options[:alias].to_sym] ||= self if options[:alias] 131: options[:aliases].each { |al| definition.fields[al.to_sym] ||= self } if options[:aliases] 132: end
Returns the default search operator for this field.
# File lib/scoped_search/definition.rb, line 84 84: def default_operator 85: @default_operator ||= case type 86: when :string, :text then :like 87: else :eq 88: end 89: end
# File lib/scoped_search/definition.rb, line 91 91: def default_order(options) 92: return nil if options[:default_order].nil? 93: field_name = options[:on] unless options[:rename] 94: field_name = options[:rename] if options[:rename] 95: order = (options[:default_order].to_s.downcase.include?('desc')) ? "DESC" : "ASC" 96: return "#{field_name} #{order}" 97: end
The ActiveRecord-based class that belongs the key field in a key-value pair.
# File lib/scoped_search/definition.rb, line 32 32: def key_klass 33: if key_relation 34: definition.klass.reflections[key_relation].klass 35: elsif relation 36: definition.klass.reflections[relation].klass 37: else 38: definition.klass 39: end 40: end
The ActiveRecord-based class that belongs to this field.
# File lib/scoped_search/definition.rb, line 22 22: def klass 23: if relation 24: related = definition.klass.reflections[relation] 25: raise ScopedSearch::QueryNotSupported, "relation '#{relation}' not one of #{definition.klass.reflections.keys.join(', ')} " if related.nil? 26: related.klass 27: else 28: definition.klass 29: end 30: end
Returns true if this field is numerical. Numerical means either integer, floating point or fixed point.
# File lib/scoped_search/definition.rb, line 69 69: def numerical? 70: [:integer, :double, :float, :decimal].include?(type) 71: end
Returns true if this is a set.
# File lib/scoped_search/definition.rb, line 79 79: def set? 80: complete_value.is_a?(Hash) 81: end