Class: Presenting::Search::Field

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/presenting/search.rb

Overview

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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#initialize

Instance Attribute Details

#bind_patternObject

formats the term BEFORE binding into the sql e.g. ‘?’, ‘?%’, etc.



144
145
146
# File 'lib/presenting/search.rb', line 144

def bind_pattern
  @bind_pattern ||= '?'
end

#nameObject

required (this is what appears in the parameter hash)



88
89
90
# File 'lib/presenting/search.rb', line 88

def name
  @name
end

#operatorObject

the format for comparison with :sql, with an optional bind for search terms ‘= ?’, ‘LIKE ?’, ‘IN (?)’, etc.



137
138
139
# File 'lib/presenting/search.rb', line 137

def operator
  @operator ||= '= ?'
end

#sqlObject

sql field (default == name)



94
95
96
# File 'lib/presenting/search.rb', line 94

def sql
  @sql ||= name
end

#typeObject

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.



163
164
165
# File 'lib/presenting/search.rb', line 163

def type
  @type
end

Instance Method Details

#bind(term) ⇒ Object

prepares the bindable term



155
156
157
158
159
# 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

#fragmentObject

composes the sql fragment



150
151
152
# File 'lib/presenting/search.rb', line 150

def fragment
  "#{sql} #{operator}"
end

#pattern=(val) ⇒ Object

a shortcut for common operator/bind_pattern combos



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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