Skip to content
This repository
file 100 lines (96 sloc) 2.656 kb
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
module Ransack
  module Constants
    TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
    FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set

    AREL_PREDICATES = %w(eq not_eq matches does_not_match lt lteq gt gteq in not_in)

    DERIVED_PREDICATES = [
      ['cont', {
        arel_predicate: 'matches',
        formatter: proc { |v| "%#{escape_wildcards(v)}%" }
        }
      ],
      ['not_cont', {
        arel_predicate: 'does_not_match',
        formatter: proc { |v| "%#{escape_wildcards(v)}%" }
        }
      ],
      ['start', {
        arel_predicate: 'matches',
        formatter: proc { |v| "#{escape_wildcards(v)}%" }
        }
      ],
      ['not_start', {
        arel_predicate: 'does_not_match',
        formatter: proc { |v| "#{escape_wildcards(v)}%" }
        }
      ],
      ['end', {
        arel_predicate: 'matches',
        formatter: proc { |v| "%#{escape_wildcards(v)}" }
        }
      ],
      ['not_end', {
        arel_predicate: 'does_not_match',
        formatter: proc { |v| "%#{escape_wildcards(v)}" }
        }
      ],
      ['true', {
        arel_predicate: 'eq',
        compounds: false,
        type: :boolean,
        validator: proc { |v| TRUE_VALUES.include?(v) }
        }
      ],
      ['false', {
        arel_predicate: 'eq',
        compounds: false,
        type: :boolean,
        validator: proc { |v| TRUE_VALUES.include?(v) },
        formatter: proc { |v| !v }
        }
      ],
      ['present', {
        arel_predicate: 'not_eq_all',
        compounds: false,
        type: :boolean,
        validator: proc { |v| TRUE_VALUES.include?(v) },
        formatter: proc { |v| [nil, ''] }
        }
      ],
      ['blank', {
        arel_predicate: 'eq_any',
        compounds: false,
        type: :boolean,
        validator: proc { |v| TRUE_VALUES.include?(v) },
        formatter: proc { |v| [nil, ''] }
        }
      ],
      ['null', {
        arel_predicate: 'eq',
        compounds: false,
        type: :boolean,
        validator: proc { |v| TRUE_VALUES.include?(v)},
        formatter: proc { |v| nil }
        }
      ],
      ['not_null', {
        arel_predicate: 'not_eq',
        compounds: false,
        type: :boolean,
        validator: proc { |v| TRUE_VALUES.include?(v) },
        formatter: proc { |v| nil } }
      ]
    ]

    module_function
    # replace % \ to \% \\
    def escape_wildcards(unescaped)
      case ActiveRecord::Base.connection.adapter_name
      when "SQLite"
        unescaped
      else
        # Necessary for PostgreSQL and MySQL
        unescaped.to_s.gsub(/([\\|\%|.])/, '\\\\\\1')
      end
    end
  end
end
Something went wrong with that request. Please try again.