Sha256: b540a3e7ccf03653b2e8fc50f32b544ac0071883cc1a2d41ec136b09530ea508

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

module Arel
  module Predications
    def matches_unaccent(other, escape = nil, case_sensitive = false)
      left = Arel::Nodes::NamedFunction.new('unaccent', [self])
      right = Arel::Nodes::NamedFunction.new('unaccent',
                                             [Arel::Nodes::Quoted.new(other)])
      left.matches(right)
    end

    def matches_unaccent_any(others, escape = nil, case_sensitive = false)
      grouping_any :matches_unaccent, others, escape, case_sensitive
    end

    def matches_unaccent_all(others, escape = nil, case_sensitive = false)
      grouping_all :matches_unaccent, others, escape, case_sensitive
    end

    def does_not_match_unaccent(other)
      left = Arel::Nodes::NamedFunction.new('unaccent', [self])
      right = Arel::Nodes::NamedFunction.new('unaccent',
                                             [Arel::Nodes::Quoted.new(other)])
      left.does_not_match(right)
    end
  end
end

Ransack.configure do |config|
  # Piso predicados cont y not_cont para que usen unaccent
  config.add_predicate 'cont',
    arel_predicate: 'matches_unaccent',
    formatter: proc { |v| "%#{Ransack::Constants.escape_wildcards(v.downcase)}%" },
    case_insensitive: true

  config.add_predicate 'cont_any',
    arel_predicate: 'matches_unaccent_any',
    formatter: proc { |v| v.split(' ').map { "%#{Ransack::Constants.escape_wildcards(_1.downcase)}%" } },
    case_insensitive: true

  config.add_predicate 'cont_all',
    arel_predicate: 'matches_unaccent_all',
    formatter: proc { |v| v.split(' ').map { "%#{Ransack::Constants.escape_wildcards(_1.downcase)}%" } },
    case_insensitive: true

  config.add_predicate 'not_cont',
    arel_predicate: 'does_not_match_unaccent',
    formatter: proc { |v| "%#{Ransack::Constants.escape_wildcards(v.downcase)}%" },
    case_insensitive: true
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pg_rails-7.1.5 pg_engine/config/initializers/ransack.rb
pg_rails-7.1.4 pg_engine/config/initializers/ransack.rb
pg_rails-7.1.3 pg_engine/config/initializers/ransack.rb