administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Enum value is not searchable

Open khrisnagunanasurya-iav opened this issue 3 years ago • 6 comments

  • What were you trying to do? I have a model that has a CEFR level {a1: 0, a2: 1, b1: 2, b2: 3, c1: 4, c2: 5}, and I want to make it to be searchable from the dashboard.

Here is my model dashboard

class ClassroomDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    ...
    level: Field::Select.with_options(searchable: true, collection: ->(field) { field.resource.class.send(field.attribute.to_s.pluralize).keys }),
    ...
  }
...
  • What did you end up with (logs, or, even better, example apps are great!)? On the search bar, I tried using these
  • level:a1
  • level:0
  • a1

but none of them are working, is this a bug or feature?

  • What versions are you running?
    • Rails 6.1.1
    • administrate 0.15.0

khrisnagunanasurya-iav avatar Mar 15 '21 14:03 khrisnagunanasurya-iav

Do you have SQL logs, to see what the search is actually trying to do?

pablobm avatar Mar 25 '21 17:03 pablobm

For anyone who got here later, as of 0.16.0, it seems to be doing "%<term>%" in WHERE LIKE clause. In above case, meaning it's searching with actual value. In above case, simply 0 would result in records for a1: 0.

And remember, if you define the enum without values assigned, e.g. enum [:a1, :a2, :b1, :b2, :c1, :c2] would generate values according to the ordinal; a1 would have 0, a2 would have 1, etc.

hirokihokari avatar Mar 16 '22 15:03 hirokihokari

The SQL is put together around these lines:

https://github.com/thoughtbot/administrate/blob/3739c6436feee531153f56b5d5650f9f6a6e8940/lib/administrate/search.rb#L87-L95

I think it should be possible to alter this to generate a different string in the case of an enum. This will probably require reading @scoped_resource in order to figure out which attribute is an enum and then provide the required SQL... I think!

Any brave soul willing to have a go at this and provide a PR? :slightly_smiling_face:

pablobm avatar Mar 17 '22 18:03 pablobm

Well, this is probably related to https://github.com/thoughtbot/administrate/issues/1939, and how Enums are a bit special.

pablobm avatar Apr 06 '23 21:04 pablobm

Here is an example of a customized search with enum where the enum field cannot be null:

# app/models/source_file.rb
class SourceFile < ApplicationRecord
  enum :status, [:pending, :completed]
end
# app/controllers/admin/source_files_controller.rb

module Admin
  class SourceFilesController < Admin::ApplicationController
    def filter_resources(resources, search_term:)
      Administrate::SourceFileSearch.new(
        resources,
        dashboard,
        search_term
      ).run
    end
  end
end

module Administrate
  class SourceFileSearch < Search
    private

    def query_template
      super
        .split(" OR ")
        .push("status = ?")
        .join(" OR ")
    end

    def query_values
      array = super
      term = array.first
      array + [SourceFile.statuses[term.parameterize(separator: "_")]]
    end
  end
end

littleforest avatar May 15 '23 21:05 littleforest