class Presenting::Search::Field
TODO: a field may require extra joins when it is searched on TODO: support more than just mysql (need access to a Connection for quoting and attribute conditions)
Attributes
bind_pattern[W]
name[R]
required (this is what appears in the parameter hash)
operator[W]
sql[W]
type[RW]
you can set a data type for the field, which will be used to convert parameter values. currently this is mostly useful for :time searches.
Public Instance Methods
bind(term)
click to toggle source
prepares the bindable term
# File lib/presenting/search.rb, line 155 def bind(term) return nil unless operator.include?('?') return bind_pattern unless bind_pattern.is_a? String bind_pattern == '?' ? typecast(term) : bind_pattern.sub('?', typecast(term).to_s) end
bind_pattern()
click to toggle source
formats the term BEFORE binding into the sql e.g. ‘?’, ‘?%’, etc.
# File lib/presenting/search.rb, line 144 def bind_pattern @bind_pattern ||= '?' end
fragment()
click to toggle source
composes the sql fragment
# File lib/presenting/search.rb, line 150 def fragment "#{sql} #{operator}" end
name=(val)
click to toggle source
# File lib/presenting/search.rb, line 89 def name=(val) @name = val.to_s end
operator()
click to toggle source
the format for comparison with :sql, with an optional bind for search terms ‘= ?’, ‘LIKE ?’, ‘IN (?)’, etc.
# File lib/presenting/search.rb, line 137 def operator @operator ||= '= ?' end
pattern=(val)
click to toggle source
a shortcut for common operator/bind_pattern combos
# File lib/presenting/search.rb, line 100 def pattern=(val) case val when :equals self.operator = '= ?' self.bind_pattern = '?' when :begins_with self.operator = 'LIKE ?' self.bind_pattern = '?%' when :ends_with self.operator = 'LIKE ?' self.bind_pattern = '%?' when :contains self.operator = 'LIKE ?' self.bind_pattern = '%?%' when :null self.operator = 'IS NULL' when :not_null self.operator = 'IS NOT NULL' when :true self.operator = '= ?' self.bind_pattern = true when :false self.operator = '= ?' self.bind_pattern = false when :less_than self.operator = '< ?' when :less_than_or_equal_to, :not_greater_than self.operator = '<= ?' when :greater_than self.operator = '> ?' when :greater_than_or_equal_to, :not_less_than self.operator = '>= ?' end end
sql()
click to toggle source
sql field (default == name)
# File lib/presenting/search.rb, line 94 def sql @sql ||= name end
Protected Instance Methods
typecast(val)
click to toggle source
# File lib/presenting/search.rb, line 167 def typecast(val) case type when :date val.is_a?(String) ? (Time.zone ? Time.zone.parse(val) : Time.parse(val)).to_date : val when :time, :datetime val.is_a?(String) ? (Time.zone ? Time.zone.parse(val) : Time.parse(val)) : val else val.to_s.strip end end