From 9772ead99ea485b03113885c96115922e81712b6 Mon Sep 17 00:00:00 2001 From: Bruno Perles Date: Tue, 10 Nov 2015 15:38:48 +0100 Subject: Add autocomplete on Timeband --- .../autocomplete_time_tables_controller.rb | 2 +- .../autocomplete_timebands_controller.rb | 31 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/controllers/autocomplete_timebands_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/autocomplete_time_tables_controller.rb b/app/controllers/autocomplete_time_tables_controller.rb index d7e6ec885..ee0f8a0cb 100644 --- a/app/controllers/autocomplete_time_tables_controller.rb +++ b/app/controllers/autocomplete_time_tables_controller.rb @@ -4,7 +4,7 @@ class AutocompleteTimeTablesController < InheritedResources::Base before_action :switch_referential def switch_referential - Apartment::Database.switch(referential.slug) + Apartment::Tenant.switch!(referential.slug) end def referential diff --git a/app/controllers/autocomplete_timebands_controller.rb b/app/controllers/autocomplete_timebands_controller.rb new file mode 100644 index 000000000..3915bd69f --- /dev/null +++ b/app/controllers/autocomplete_timebands_controller.rb @@ -0,0 +1,31 @@ +class AutocompleteTimebandsController < InheritedResources::Base + respond_to :json, :only => [:index] + + before_action :switch_referential + + def switch_referential + Apartment::Tenant.switch!(referential.slug) + end + + def referential + @referential ||= current_organisation.referentials.find params[:referential_id] + end + + protected + + def select_timebands + if params[:route_id] + referential.timebands.joins( vehicle_journeys: :route).where( "routes.id IN (#{params[:route_id]})") + else + referential.timebands + end + end + + def referential_timebands + @referential_timebands ||= select_timebands + end + + def collection + @timebands = referential_timebands.select{ |p| p.fullname =~ /#{params[:q]}/i } + end +end -- cgit v1.2.3