aboutsummaryrefslogtreecommitdiffstats
path: root/config/initializers/ransack.rb
blob: 09d895e474976a6f3982732b6a5d0e7c27d3d8af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Ransack.configure do |config|
  config.add_predicate 'between',
                       arel_predicate: 'between',
                       formatter: proc { |v| v.split(' to ') },
                       type: :string
end

module Arel
  module Predications
    def between other
      gteq(other[0]).and(lt(other[1]))
    end
  end
end

module Ransack
  module Constants
    module_function
    # replace % \ to \% \\
    def escape_wildcards(unescaped)
      case ActiveRecord::Base.connection.adapter_name
      when "Mysql2".freeze, "PostgreSQL".freeze, "PostGIS".freeze 
        # Necessary for PostgreSQL and MySQL
        unescaped.to_s.gsub(/([\\|\%|_|.])/, '\\\\\\1')
      else
        unescaped
      end
    end
  end
end