| 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] | |
| full_text_search | [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 23
23: def initialize(definition, options = {})
24: @definition = definition
25: @definition.profile = options[:profile] if options[:profile]
26: @definition.default_order ||= default_order(options)
27:
28: case options
29: when Symbol, String
30: @field = field.to_sym
31: when Hash
32: @field = options.delete(:on)
33:
34: # Set attributes from options hash
35: @complete_value = options[:complete_value]
36: @relation = options[:in]
37: @key_relation = options[:in_key]
38: @key_field = options[:on_key]
39: @offset = options[:offset]
40: @word_size = options[:word_size] || 1
41: @ext_method = options[:ext_method]
42: @operators = options[:operators]
43: @only_explicit = !!options[:only_explicit]
44: @full_text_search = options[:full_text_search]
45: @default_operator = options[:default_operator] if options.has_key?(:default_operator)
46: end
47:
48: # Store this field is the field array
49: definition.fields[@field] ||= self unless options[:rename]
50: definition.fields[options[:rename].to_sym] ||= self if options[:rename]
51: definition.unique_fields << self
52:
53: # Store definition for alias / aliases as well
54: definition.fields[options[:alias].to_sym] ||= self if options[:alias]
55: options[:aliases].each { |al| definition.fields[al.to_sym] ||= self } if options[:aliases]
56: end
Returns the ActiveRecord column definition that corresponds to this field.
# File lib/scoped_search/definition.rb, line 81
81: def column
82: @column ||= begin
83: if klass.columns_hash.has_key?(field.to_s)
84: klass.columns_hash[field.to_s]
85: else
86: raise ActiveRecord::UnknownAttributeError, "#{klass.inspect} doesn't have column #{field.inspect}."
87: end
88: end
89: end
Returns the default search operator for this field.
# File lib/scoped_search/definition.rb, line 128
128: def default_operator
129: @default_operator ||= case type
130: when :string, :text then :like
131: else :eq
132: end
133: end
# File lib/scoped_search/definition.rb, line 135
135: def default_order(options)
136: return nil if options[:default_order].nil?
137: field_name = options[:on] unless options[:rename]
138: field_name = options[:rename] if options[:rename]
139: order = (options[:default_order].to_s.downcase.include?('desc')) ? "DESC" : "ASC"
140: return "#{field_name} #{order}"
141: end
The ActiveRecord-based class that belongs the key field in a key-value pair.
# File lib/scoped_search/definition.rb, line 70
70: def key_klass
71: @key_klass ||= if key_relation
72: definition.klass.reflections[key_relation].klass
73: elsif relation
74: definition.klass.reflections[relation].klass
75: else
76: definition.klass
77: end
78: end
The ActiveRecord-based class that belongs to this field.
# File lib/scoped_search/definition.rb, line 59
59: def klass
60: @klass ||= if relation
61: related = definition.klass.reflections[relation]
62: raise ScopedSearch::QueryNotSupported, "relation '#{relation}' not one of #{definition.klass.reflections.keys.join(', ')} " if related.nil?
63: related.klass
64: else
65: definition.klass
66: end
67: end
Returns true if this field is numerical. Numerical means either integer, floating point or fixed point.
# File lib/scoped_search/definition.rb, line 113
113: def numerical?
114: [:integer, :double, :float, :decimal].include?(type)
115: end
Returns true if this is a set.
# File lib/scoped_search/definition.rb, line 123
123: def set?
124: complete_value.is_a?(Hash)
125: end