diff options
| author | Teddy Wing | 2018-02-22 16:49:02 +0100 | 
|---|---|---|
| committer | Johan Van Ryseghem | 2018-03-07 13:19:41 +0100 | 
| commit | 0baeff6da40e5f631ef32ebe553f881dfb31a0c2 (patch) | |
| tree | b7f5c8221cf68f1d5bf48b73367961b9928ed514 | |
| parent | a53eb22c144b12cf7b662db38ddbbba20af89d3a (diff) | |
| download | chouette-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
| -rw-r--r-- | app/controllers/autocomplete_lines_controller.rb | 24 | ||||
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 1 | ||||
| -rw-r--r-- | app/views/autocomplete_lines/index.rabl | 8 | ||||
| -rw-r--r-- | app/views/referential_vehicle_journeys/_filters.html.slim | 11 | ||||
| -rw-r--r-- | config/routes.rb | 1 | 
5 files changed, 40 insertions, 5 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 diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 60279422c..cdeac56dd 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -409,6 +409,7 @@ module Chouette          .where('"time_tables_vehicle_journeys"."vehicle_journey_id" IS NULL')      end +    # TODO: delete this      def self.lines        lines_query = joins(:route).select("routes.line_id").reorder(nil).except(:group).pluck(:'routes.line_id')        Chouette::Line.where(id: lines_query) diff --git a/app/views/autocomplete_lines/index.rabl b/app/views/autocomplete_lines/index.rabl new file mode 100644 index 000000000..1d235ef94 --- /dev/null +++ b/app/views/autocomplete_lines/index.rabl @@ -0,0 +1,8 @@ +collection @lines + +node do |line| +  { +    id: line.id, +    text: line.display_name +  } +end diff --git a/app/views/referential_vehicle_journeys/_filters.html.slim b/app/views/referential_vehicle_journeys/_filters.html.slim index 5f102ae1b..d650225b2 100644 --- a/app/views/referential_vehicle_journeys/_filters.html.slim +++ b/app/views/referential_vehicle_journeys/_filters.html.slim @@ -24,14 +24,15 @@          = f.input :route_line_id_eq,              as: :select,              include_blank: t(".all"), -            collection: @vehicle_journeys.lines, -            selected: params[:q] && params[:q][:route_line_id_eq],              input_html: { \ -              'data-select2ed': 'true', -              'data-select2ed-placeholder': t('referentials.filters.line') \ +              data: { \ +                'select2-ajax': 'true', +                'select2ed-placeholder': t('referentials.filters.line'), +                url: referential_autocomplete_lines_path(@referential, format: :json), +                'select2ed-allow-clear': true \ +              } \              },              label: false, -            label_method: :display_name,              wrapper_html: { class: 'filter_menu-item select2ed' }      .form-group.togglable.name-filter class=filter_item_class(params[:q], :published_journey_name_gteq) diff --git a/config/routes.rb b/config/routes.rb index a3a21511c..dc75bec11 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,7 @@ ChouetteIhm::Application.routes.draw do        post :validate      end +    resources :autocomplete_lines, only: :index      resources :autocomplete_stop_areas, only: [:show, :index] do        get 'around', on: :member      end | 
