Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not compatible with Postgres citext column type #179

Open
constantm opened this issue Jun 14, 2018 · 4 comments
Open

Not compatible with Postgres citext column type #179

constantm opened this issue Jun 14, 2018 · 4 comments

Comments

@constantm
Copy link

It seems that scoped_search ignores any Postgres column using the case-insensitive type of citext in Postgres. When using the citext column type for example an email field along with scoped search:

scoped_search on: %i[email first_name last_name]

The query generated ignores the email column:

[1] pry(main)> User.all.search_for('test@test.com').to_sql
=> "SELECT \"users\".* FROM \"users\" WHERE ((\"users\".\"first_name\" ILIKE '%user%' OR \"users\".\"last_name\" ILIKE '%user%'))"

I haven't spent a lot of time on this, but it seems possibly due to the textual? function not treating these column types properly:

def textual?
  [:string, :text].include?(type)
end

I'll look into this a bit more when I have a minute.

@heinrichhanekom
Copy link

heinrichhanekom commented Jul 29, 2020

Hi
Is there any update regarding this?
I just ran into the same problem.
Thanks for any input

@ares
Copy link
Collaborator

ares commented Jul 29, 2020

not that I know of, I don't hink anyone is actively work on this

@heinrichhanekom
Copy link

Thanks you for the quick response

@gregpardo
Copy link
Contributor

gregpardo commented Apr 17, 2021

Not that this is always a good idea but you can override some methods. This could obviously break some things when the library updates but I was able to get it working with the current version...

# Monkey patch to support citext
module ScopedSearch
  class Definition

    class Field
      def textual?
        %i[string text citext].include?(type)
      end
      def default_operator
        @default_operator ||= case type
                              when :string, :text, :citext then :like
                              else :eq
                              end
      end
    end

    def default_fields_for(value, operator = nil)
      column_types  = [:virtual]
      column_types += [:string, :text, :citext]       if [nil, :like, :unlike, :ne, :eq].include?(operator)
      column_types += [:double, :float, :decimal]     if value =~ NUMERICAL_REGXP
      column_types += [:integer]                      if value =~ INTEGER_REGXP
      column_types += [:uuid]                         if value =~ UUID_REGXP
      column_types += [:datetime, :date, :timestamp]  if (parse_temporal(value))
      default_fields.select { |field| !field.set? && column_types.include?(field.type) }
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants