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.
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 def initialize(definition, options = {}) @definition = definition @definition.profile = options[:profile] if options[:profile] @definition.default_order ||= default_order(options) case options when Symbol, String @field = field.to_sym when Hash @field = options.delete(:on) # Set attributes from options hash @complete_value = options[:complete_value] @relation = options[:in] @key_relation = options[:in_key] @key_field = options[:on_key] @offset = options[:offset] @word_size = options[:word_size] || 1 @ext_method = options[:ext_method] @operators = options[:operators] @only_explicit = !!options[:only_explicit] @full_text_search = options[:full_text_search] @default_operator = options[:default_operator] if options.has_key?(:default_operator) @complete_enabled = options[:complete_enabled].nil? ? true : options[:complete_enabled] end # Store this field is the field array definition.fields[@field] ||= self unless options[:rename] definition.fields[options[:rename].to_sym] ||= self if options[:rename] definition.unique_fields << self # Store definition for alias / aliases as well definition.fields[options[:alias].to_sym] ||= self if options[:alias] options[:aliases].each { |al| definition.fields[al.to_sym] ||= self } if options[:aliases] end
Returns the ActiveRecord column definition that corresponds to this field.
# File lib/scoped_search/definition.rb, line 82 def column @column ||= begin if klass.columns_hash.has_key?(field.to_s) klass.columns_hash[field.to_s] else raise ActiveRecord::UnknownAttributeError, "#{klass.inspect} doesn't have column #{field.inspect}." end end end
Returns true if this field is a date-like column
# File lib/scoped_search/definition.rb, line 103 def date? type == :date end
Returns true if this field is a datetime-like column
# File lib/scoped_search/definition.rb, line 98 def datetime? [:datetime, :time, :timestamp].include?(type) end
Returns the default search operator for this field.
# File lib/scoped_search/definition.rb, line 129 def default_operator @default_operator ||= case type when :string, :text then :like else :eq end end
# File lib/scoped_search/definition.rb, line 136 def default_order(options) return nil if options[:default_order].nil? field_name = options[:rename].nil? ? options[:on] : options[:rename] order = (options[:default_order].to_s.downcase.include?('desc')) ? "DESC" : "ASC" return "#{field_name} #{order}" end
The ActiveRecord-based class that belongs the key field in a key-value pair.
# File lib/scoped_search/definition.rb, line 71 def key_klass @key_klass ||= if key_relation definition.klass.reflections[key_relation].klass elsif relation definition.klass.reflections[relation].klass else definition.klass end end
The ActiveRecord-based class that belongs to this field.
# File lib/scoped_search/definition.rb, line 60 def klass @klass ||= if relation related = definition.klass.reflections[relation] raise ScopedSearch::QueryNotSupported, "relation '#{relation}' not one of #{definition.klass.reflections.keys.join(', ')} " if related.nil? related.klass else definition.klass end end
Returns true if this field is numerical. Numerical means either integer, floating point or fixed point.
# File lib/scoped_search/definition.rb, line 114 def numerical? [:integer, :double, :float, :decimal].include?(type) end
Return 'table'.'column' with the correct database quotes
# File lib/scoped_search/definition.rb, line 144 def quoted_field c = klass.connection "#{c.quote_table_name(klass.table_name)}.#{c.quote_column_name(field)}" end
Returns true if this is a set.
# File lib/scoped_search/definition.rb, line 124 def set? complete_value.is_a?(Hash) end
Returns true if this field is a date or datetime-like column.
# File lib/scoped_search/definition.rb, line 108 def temporal? datetime? || date? end
Generated with the Darkfish Rdoc Generator 2.