Skip to content

Commit

Permalink
feat: BelongsTo should always be searchable
Browse files Browse the repository at this point in the history
We ran into some strange performance projects on a client project that
was later identified as a non-searchable belongs to field timing out
requests for update and creation fields by attempting to serialize the
entire contents of a table with hundreds of thousands of rows in it.
There are very few use cases where a belongs to with a fixed, small
collection of potential relationships is desireable.  This commit
removes that default and adds a warning to the `BelongsTo.searchable`
function.
  • Loading branch information
staylorwr committed May 7, 2021
1 parent c129c97 commit defbda6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 53 deletions.
54 changes: 3 additions & 51 deletions assets/src/components/Form/BelongsTo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<default-field :field="field">
<template slot="field">
<search-input
v-if="isSearchable && !isLocked"
v-if="!isLocked"
:error="hasError"
:value="selectedResource"
:data="availableResources"
Expand Down Expand Up @@ -68,30 +68,6 @@
</div>
</div>
</search-input>
<select
v-else
:class="{ 'border-danger': hasError }"
:disabled="isLocked"
class="form-control form-select mb-3 w-full"
@change="selectResourceFromSelectControl"
>
<option
value=""
selected
>
Choose {{ field.name }}
</option>

<option
v-for="resource in availableResources"
:key="resource.id"
:value="resource.id"
:selected="selectedResourceId == resource.id"
>
{{ resource.title }}
</option>
</select>

<p
v-if="hasError"
class="my-2 text-danger"
Expand Down Expand Up @@ -167,13 +143,6 @@ export default {
);
},
/**
* Determine if the related resources is searchable
*/
isSearchable () {
return this.field.options.searchable;
},
/**
* Get the query params for getting available resources
*/
Expand Down Expand Up @@ -217,21 +186,12 @@ export default {
this.selectedResourceId = this.viaResourceId;
}
if (this.shouldSelectInitialResource && !this.isSearchable) {
// If we should select the initial resource but the field is not
// searchable we should load all of the available resources into the
// field first and select the initial option
this.initializingWithExistingResource = false;
this.getAvailableResources().then(() => this.selectInitialResource());
} else if (this.shouldSelectInitialResource && this.isSearchable) {
if (this.shouldSelectInitialResource) {
// If we should select the initial resource and the field is
// searchable, we won't load all the resources but we will select
// the initial option
this.getAvailableResources().then(() => this.selectInitialResource());
} else if (!this.shouldSelectInitialResource && !this.isSearchable) {
// If we don't need to select an initial resource because the user
// came to create a resource directly and there's no parent resource,
// and the field is searchable we'll just load all of the resources
} else {
this.getAvailableResources();
}
Expand All @@ -245,14 +205,6 @@ export default {
);
},
/**
* Select a resource using the <select> control
*/
selectResourceFromSelectControl (e) {
this.selectedResourceId = e.target.value;
this.selectInitialResource();
},
/**
* Fill the forms formData with details from this field
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_teal/fields/belongs_to.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ defmodule ExTeal.Fields.BelongsTo do
"""
@spec searchable(Field.t()) :: Field.t()
def searchable(field) do
opts = Map.merge(field.options, %{searchable: true})
Map.put(field, :options, opts)
IO.warn("searchable/1 is deprecated. BelongsTo fields are always searchable")
field
end
end

0 comments on commit defbda6

Please sign in to comment.