lib/scoped_search/definition.rb in scoped_search-4.1.4 vs lib/scoped_search/definition.rb in scoped_search-4.1.5
- old
+ new
@@ -105,10 +105,15 @@
else
definition.klass
end
end
+ # Returns true if this is a virtual field.
+ def virtual?
+ !ext_method.nil?
+ end
+
# Returns the ActiveRecord column definition that corresponds to this field.
def column
@column ||= begin
if klass.columns_hash.has_key?(field.to_s)
klass.columns_hash[field.to_s]
@@ -118,19 +123,19 @@
end
end
# Returns the column type of this field.
def type
- @type ||= column.type
+ @type ||= virtual? ? :virtual : column.type
end
- # Returns true if this field is a datetime-like column
+ # Returns true if this field is a datetime-like column.
def datetime?
[:datetime, :time, :timestamp].include?(type)
end
- # Returns true if this field is a date-like column
+ # Returns true if this field is a date-like column.
def date?
type == :date
end
# Returns true if this field is a date or datetime-like column.
@@ -165,11 +170,11 @@
def generate_default_order(default_order, field)
order = (default_order.to_s.downcase.include?('desc')) ? "DESC" : "ASC"
return "#{field} #{order}"
end
- # Return 'table'.'column' with the correct database quotes
+ # Return 'table'.'column' with the correct database quotes.
def quoted_field
c = klass.connection
"#{c.quote_table_name(klass.table_name)}.#{c.quote_column_name(field)}"
end
end
@@ -229,24 +234,25 @@
# this method is used by the syntax auto completer to suggest operators.
def operator_by_field_name(name)
field = field_by_name(name)
return [] if field.nil?
- return field.operators if field.operators
- return ['= ', '!= '] if field.set?
- return ['= ', '> ', '< ', '<= ', '>= ','!= ', '^ ', '!^ '] if field.numerical?
- return ['= ', '!= ', '~ ', '!~ ', '^ ', '!^ '] if field.textual?
- return ['= ', '> ', '< '] if field.temporal?
+ return field.operators if field.operators
+ return ['=', '!=', '>', '<', '<=', '>=', '~', '!~', '^', '!^'] if field.virtual?
+ return ['=', '!='] if field.set?
+ return ['=', '>', '<', '<=', '>=', '!=', '^', '!^'] if field.numerical?
+ return ['=', '!=', '~', '!~', '^', '!^'] if field.textual?
+ return ['=', '>', '<'] if field.temporal?
raise ScopedSearch::QueryNotSupported, "Unsupported type '#{field.type.inspect}')' for field '#{name}'. This can be a result of a search definition problem."
end
NUMERICAL_REGXP = /^\-?\d+(\.\d+)?$/
INTEGER_REGXP = /^\-?\d+$/
# Returns a list of appropriate fields to search in given a search keyword and operator.
def default_fields_for(value, operator = nil)
- column_types = []
+ column_types = [:virtual]
column_types += [:string, :text] if [nil, :like, :unlike, :ne, :eq].include?(operator)
column_types += [:double, :float, :decimal] if value =~ NUMERICAL_REGXP
column_types += [:integer] if value =~ INTEGER_REGXP
column_types += [:datetime, :date, :timestamp] if (parse_temporal(value))