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

Polymorphic filter select #2886

Merged
merged 3 commits into from
May 6, 2019

Conversation

shekibobo
Copy link
Contributor

This was adapted from #2461 and updates it to the latest master, along with a few other fixes.

Polymorphic associations are still belongs_to associations, so they should behave as much the same way as possible. Now, when we update the type field of a polymorphic association, we get a autocomplete field for the association record instead of it loading the entire collection (which causes browser lockup for extremely large collections).

@shekibobo shekibobo force-pushed the polymorphic-filter-select branch 4 times, most recently from 753b48c to 0146a3a Compare May 31, 2017 16:45
@shekibobo
Copy link
Contributor Author

I'm pretty sure the remaining test failure isn't related to this PR. If you rebuild the failing one, it will most likely pass.

@shekibobo shekibobo force-pushed the polymorphic-filter-select branch 2 times, most recently from 3213f86 to 0146a3a Compare June 29, 2017 17:28
@shekibobo
Copy link
Contributor Author

Just merged with master, so this is up to date and passing now.

@arnvald
Copy link
Contributor

arnvald commented Jan 2, 2018

@shekibobo thank you for taking this code and updating it with newest master 🙇 I was going to update my fork and your work saved me a lot of time!

@mshibuya can anything be done to help getting this merged to master?
I've been using this code in production for a long time and it's been working fine. I haven't finished adding support for associated_collection_cache_all or associated_collection_scope, but I feel it's still an improvement over current version which stops working for big databases.

@grillermo
Copy link

Thank you for this, i'm using it on my fork

default_options = { float_left: false }

js_data = type_collection.inject({}) do |options, model|
model_name = model[0].downcase.parameterize.underscore
Copy link

@BMannell BMannell May 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a model has a different label set in the locales the filtering will not work. Seems to break the lookup of the remote source for the filtering javascript widget.
I've changed the line to model_name = model.second.underscore.downcase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why .second?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first string in the model array is the model label that is displayed in the drop-down, the second string is the model class name used in the routing.
Eg. ["Car", "Vehicle"]

Until now, for polymorphic associations, a simple select tag was used, without
filtering option. This commit adds filtering that easily works with switching
association class.

For each association class we need a path to load records from. For simple
association, this is hardcoded in the `select` tag, for polymorphic one, we need
to save them (a div with specific `data` attribute is generated) and switch
whenever object type is switched.

Switching type is a 3 step process:
* update `data-options` attribute of select tag
* destroy filtering object
* create filtering object with new options

Additionally, to avoid weird CSS behaviour, a fix to CSS generation was
introduced. Without it, the object type field was moved to the right (since
object value field had `float: left;` attribute set

Use filter select for polymorphic association fields
@shekibobo shekibobo force-pushed the polymorphic-filter-select branch from 5508737 to 8ad0793 Compare May 3, 2018 11:37
@robwise
Copy link

robwise commented Apr 12, 2019

What steps need to be taken to get this merged?

@mshibuya mshibuya added this to the 2.0.0 milestone May 2, 2019
@mshibuya mshibuya merged commit 53ff249 into railsadminteam:master May 6, 2019
@mshibuya
Copy link
Member

mshibuya commented May 6, 2019

Fantastic, thanks!

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

Successfully merging this pull request may close these issues.

6 participants