thoughtbot / thoughtbot/administrate

Allow searchable on multiple inherited classes

Open
#2,147 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug search
Dominant language
JavaScript
Stars
6k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

  • What were you trying to do?
    I have multiple classes that inherit from a User class and I want to use searchable Field::BelongsTo for those classes.

The problem:
When using the same table on multiple classes rails left_joins creates an alias for the second join this way when filtering the data we should match against the alias and not the original table, I did not find any way to configure this alias on the administrate config.

# model/user.rb
class User
end

# model/editor.rb
class Editor < User
end

# model/client.rb
class Client < User
end

# model/project.rb
class Project
  belongs_to :editor, optional: true
  belongs_to :client
end

# dashboard/project_dashboard.rb
class ProjectDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    client:                    Field::BelongsTo.with_options(
      searchable: true,
      searchable_fields: %w[name],
    ),
    editor:                    Field::BelongsTo.with_options(
      searchable: true,
      searchable_fields: %w[name],
    ),
  }

  COLLECTION_ATTRIBUTES = %i[
    client
    editor
  ]
end

With this code I can filter by client name, but not by editor name.
SQL Generated:

SELECT "projects".*
FROM "projects"
         LEFT OUTER JOIN "users" ON "users"."id" = "projects"."client_id" AND "users"."type" IN ('Client')
         LEFT OUTER JOIN "users" "editors_projects"
                         ON "editors_projects"."id" = "projects"."editor_id" AND "editors_projects"."type" IN ('Editor')
WHERE (LOWER(CAST("users"."name" AS CHAR(256))) LIKE '%test%' OR
       LOWER(CAST("users"."name" AS CHAR(256))) LIKE '%test%')

Where it should be:

SELECT "projects".*
FROM "projects"
         LEFT OUTER JOIN "users" ON "users"."id" = "projects"."client_id" AND "users"."type" IN ('Client')
         LEFT OUTER JOIN "users" "editors_projects"
                         ON "editors_projects"."id" = "projects"."editor_id" AND "editors_projects"."type" IN ('Editor')
WHERE (LOWER(CAST("users"."name" AS CHAR(256))) LIKE '%test%' OR
       LOWER(CAST("editors_projects"."name" AS CHAR(256))) LIKE '%test%')

Note the editors_projects change on where.

It would be great if I could pass an alias on the searchable configuration

  • What versions are you running?
    • Rails: 5.2.6
    • administrate: Latest

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the searchable configuration in dashboard/project_dashboard.rb and the Field::BelongsTo behavior used by the client and editor fields. Reproduce the generated SQL from the issue and trace how each association's search condition is built. Done means the editor search uses the editors_projects alias while client search continues to use users.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.