aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/autocomplete_lines_controller.rb
diff options
context:
space:
mode:
authorTeddy Wing2018-02-22 16:49:02 +0100
committerJohan Van Ryseghem2018-03-07 13:19:41 +0100
commit0baeff6da40e5f631ef32ebe553f881dfb31a0c2 (patch)
treeb7f5c8221cf68f1d5bf48b73367961b9928ed514 /app/controllers/autocomplete_lines_controller.rb
parenta53eb22c144b12cf7b662db38ddbbba20af89d3a (diff)
downloadchouette-core-0baeff6da40e5f631ef32ebe553f881dfb31a0c2.tar.bz2
ReferentialVehicleJourneys#index: Make lines filter asynchronous
Instead of doing a complex query to get the lines available to filter on, just get all the lines in the current referential. We then filter those by what was typed into the select2 box and asynchronously respond with line options in a new JSON autocomplete controller. Here we're only providing the option to filter by `number` and `name` field on the line. I'd like to filter by everything in the `#display_name`. I don't think the objectid is going to be easy to integrate into the SQL query but I'd like to add the company part. It's possible we could do the objectid filter in Ruby instead of the database query. Refs #5889
Diffstat (limited to 'app/controllers/autocomplete_lines_controller.rb')
-rw-r--r--app/controllers/autocomplete_lines_controller.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/app/controllers/autocomplete_lines_controller.rb b/app/controllers/autocomplete_lines_controller.rb
new file mode 100644
index 000000000..945736ab9
--- /dev/null
+++ b/app/controllers/autocomplete_lines_controller.rb
@@ -0,0 +1,24 @@
+class AutocompleteLinesController < ChouetteController
+ include ReferentialSupport
+
+ respond_to :json, only: :index
+
+ protected
+
+ def collection
+ @lines = referential.line_referential.lines
+
+ filter = <<~SQL
+ number LIKE ?
+ OR name LIKE ?
+ SQL
+ @lines = @lines
+ .where(
+ filter,
+ *Array.new(2, "#{params[:q]}%")
+ )
+ .search(params[:q])
+ .result
+ .paginate(page: params[:page])
+ end
+end