From 03fd7ae58db8f6338583b7d63c500273ca04925c Mon Sep 17 00:00:00 2001 From: Santosh Prasad Sah Date: Wed, 29 Jan 2025 15:25:30 +0545 Subject: [PATCH 1/4] chore: add @rails/request.js --- package.json | 1 + yarn.lock | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/package.json b/package.json index bcf7c1c065..6a0b89cc3a 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@hotwired/stimulus": "^3.2.2", "@hotwired/turbo-rails": "^8.0.12", "@rails/activestorage": "^6.1.710", + "@rails/request.js": "^0.0.11", "@stimulus-components/clipboard": "^5.0.0", "@stimulus-components/password-visibility": "^3.0.0", "@tailwindcss/forms": "^0.5.10", diff --git a/yarn.lock b/yarn.lock index 8aaff3b01b..6504b2c732 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1515,6 +1515,11 @@ dependencies: spark-md5 "^3.0.0" +"@rails/request.js@^0.0.11": + version "0.0.11" + resolved "https://registry.yarnpkg.com/@rails/request.js/-/request.js-0.0.11.tgz#4d9be25a49d97911c64ccd0f00b79d57fca4c3b4" + integrity sha512-2U3uYS0kbljt+pAstN+LIlZOl7xmOKig5N6FrvtUWO1wq0zR1Hf90fHfD2SYiyV8yH1nyKpoTmbLqWT0xe1zDg== + "@remirror/core-constants@3.0.0": version "3.0.0" resolved "https://registry.yarnpkg.com/@remirror/core-constants/-/core-constants-3.0.0.tgz#96fdb89d25c62e7b6a5d08caf0ce5114370e3b8f" From 8c7331b4a8c6b4977f715799d6a82bdcb69b9acc Mon Sep 17 00:00:00 2001 From: Santosh Prasad Sah Date: Wed, 29 Jan 2025 15:26:48 +0545 Subject: [PATCH 2/4] feat: add resource search controller --- .../views/resource_index_component.html.erb | 11 ++++++++- app/javascript/js/controllers.js | 2 ++ .../controllers/resource_search_controller.js | 24 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 app/javascript/js/controllers/resource_search_controller.js diff --git a/app/components/avo/views/resource_index_component.html.erb b/app/components/avo/views/resource_index_component.html.erb index 25e56dc0f1..a1f6f700b2 100644 --- a/app/components/avo/views/resource_index_component.html.erb +++ b/app/components/avo/views/resource_index_component.html.erb @@ -36,7 +36,16 @@
">
<% if show_search_input %> - <%= render partial: "avo/partials/resource_search", locals: {resource: @resource.route_key, via_reflection: via_reflection} %> + <%#= render partial: "avo/partials/resource_search", locals: {resource: @resource.route_key, via_reflection: via_reflection} %> + + <% else %> <%# Offset for the space-y-2 property when the search is missing %>
diff --git a/app/javascript/js/controllers.js b/app/javascript/js/controllers.js index 48ad154aee..103f434aca 100644 --- a/app/javascript/js/controllers.js +++ b/app/javascript/js/controllers.js @@ -33,6 +33,7 @@ import RecordSelectorController from './controllers/record_selector_controller' import ReloadBelongsToFieldController from './controllers/fields/reload_belongs_to_field_controller' import ResourceEditController from './controllers/resource_edit_controller' import ResourceIndexController from './controllers/resource_index_controller' +import ResourceSearchController from './controllers/resource_search_controller' import ResourceShowController from './controllers/resource_show_controller' import SearchController from './controllers/search_controller' import SelectController from './controllers/select_controller' @@ -76,6 +77,7 @@ application.register('preview', PreviewController) application.register('record-selector', RecordSelectorController) application.register('resource-edit', ResourceEditController) application.register('resource-index', ResourceIndexController) +application.register('resource-search', ResourceSearchController) application.register('resource-show', ResourceShowController) application.register('search', SearchController) application.register('select', SelectController) diff --git a/app/javascript/js/controllers/resource_search_controller.js b/app/javascript/js/controllers/resource_search_controller.js new file mode 100644 index 0000000000..17f328b425 --- /dev/null +++ b/app/javascript/js/controllers/resource_search_controller.js @@ -0,0 +1,24 @@ +import { Controller } from "@hotwired/stimulus" +import { get } from "@rails/request.js" + +export default class extends Controller { + static targets = ['input'] + + connect() { + console.log('Resource search controller connected') + } + + async search() { + const query = this.inputTarget.value + const currentUrl = new URL(window.location.href) + currentUrl.searchParams.set('q', query) + + try { + await get(currentUrl.pathname + currentUrl.search, { + responseKind: 'turbo-stream' + }) + } catch (error) { + console.error('Error performing search:', error) + } + } +} From 3fb45170a38710cac2fd235e04bd38d256b512ef Mon Sep 17 00:00:00 2001 From: Santosh Prasad Sah Date: Wed, 29 Jan 2025 15:27:45 +0545 Subject: [PATCH 3/4] feat: add apply search query method to search resource items --- app/controllers/avo/base_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/avo/base_controller.rb b/app/controllers/avo/base_controller.rb index a7ec077b2f..375a60e677 100644 --- a/app/controllers/avo/base_controller.rb +++ b/app/controllers/avo/base_controller.rb @@ -25,6 +25,8 @@ def index end add_breadcrumb @resource.plural_name.humanize + # Apply the search query if configured on the resource + apply_search_query if params[:q].present? && @resource.search.present? set_index_params set_filters set_actions @@ -643,5 +645,12 @@ def set_pagination_params def set_query @query ||= @resource.class.query_scope end + + def apply_search_query + search_query = @resource.search[:query] + return unless search_query.present? + + @query = instance_exec(@query, &search_query) + end end end From a18e3d3cc328a160506aeac92e6d35b2f4bbbf21 Mon Sep 17 00:00:00 2001 From: Santosh Prasad Sah Date: Wed, 29 Jan 2025 16:13:58 +0545 Subject: [PATCH 4/4] feat: set search param to index params also try to use ExecutionContext --- app/controllers/avo/base_controller.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/controllers/avo/base_controller.rb b/app/controllers/avo/base_controller.rb index 375a60e677..72ae01e894 100644 --- a/app/controllers/avo/base_controller.rb +++ b/app/controllers/avo/base_controller.rb @@ -26,8 +26,8 @@ def index add_breadcrumb @resource.plural_name.humanize # Apply the search query if configured on the resource - apply_search_query if params[:q].present? && @resource.search.present? set_index_params + apply_search set_filters set_actions set_query @@ -307,6 +307,9 @@ def set_index_params set_pagination_params + # Search + @index_params[:q] = params[:q] if params[:q].present? + # Sorting @index_params[:sort_by] = params[:sort_by] || @resource.sort_by_param @@ -646,11 +649,18 @@ def set_query @query ||= @resource.class.query_scope end - def apply_search_query + def apply_search + return if @resource.class.search_query.nil? + return if @index_params[:q].nil? + search_query = @resource.search[:query] return unless search_query.present? - @query = instance_exec(@query, &search_query) + @query = Avo::ExecutionContext.new( + target: @resource.class.search_query, + params: params, + query: @query + ).handle end end end