Module: Minidusen::Util
Instance Method Summary collapse
- #escape_for_like_query(phrase) ⇒ Object
- #escape_with_backslash(phrase, characters) ⇒ Object
- #ilike_operator(scope) ⇒ Object
- #like_expression(phrase) ⇒ Object
- #postgresql?(scope) ⇒ Boolean
- #qualify_column_name(model, column_name) ⇒ Object
- #regexp_operator(scope) ⇒ Object
Instance Method Details
#escape_for_like_query(phrase) ⇒ Object
39 40 41 42 |
# File 'lib/minidusen/util.rb', line 39 def escape_for_like_query(phrase) # phrase.gsub("%", "\\%").gsub("_", "\\_") escape_with_backslash(phrase, ['%', '_']) end |
#escape_with_backslash(phrase, characters) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/minidusen/util.rb', line 30 def escape_with_backslash(phrase, characters) characters << '\\' pattern = /[#{characters.collect(&Regexp.method(:quote)).join('')}]/ # debugger phrase.gsub(pattern) do |match| "\\#{match}" end end |
#ilike_operator(scope) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/minidusen/util.rb', line 14 def ilike_operator(scope) if postgresql?(scope) 'ILIKE' else 'LIKE' end end |
#like_expression(phrase) ⇒ Object
10 11 12 |
# File 'lib/minidusen/util.rb', line 10 def like_expression(phrase) "%#{escape_for_like_query(phrase)}%" end |
#postgresql?(scope) ⇒ Boolean
5 6 7 8 |
# File 'lib/minidusen/util.rb', line 5 def postgresql?(scope) adapter_name = scope.connection.class.name adapter_name =~ /postgres/i end |
#qualify_column_name(model, column_name) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/minidusen/util.rb', line 44 def qualify_column_name(model, column_name) column_name = column_name.to_s unless column_name.include?('.') quoted_table_name = model.connection.quote_table_name(model.table_name) quoted_column_name = model.connection.quote_column_name(column_name) column_name = "#{quoted_table_name}.#{quoted_column_name}" end column_name end |
#regexp_operator(scope) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/minidusen/util.rb', line 22 def regexp_operator(scope) if postgresql?(scope) '~' else 'REGEXP' end end |