aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/autocomplete_time_tables_controller.rb
blob: dded24a36dfc3e30812f4e4750e67e7de8a31011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class AutocompleteTimeTablesController < ChouetteController
  respond_to :json, :only => [:index]
  before_action :switch_referential

  include ReferentialSupport

  def switch_referential
    Apartment::Tenant.switch!(referential.slug)
  end

  def referential
    @referential ||= Referential.find params[:referential_id]
  end

  protected

  def select_time_tables
    scope = params[:source_id] ? referential.time_tables.where("time_tables.id != ?", params[:source_id]) : referential.time_tables
    if params[:route_id]
      scope = scope.joins(vehicle_journeys: :route).where( "routes.id IN (#{params[:route_id]})")
    end
    scope.distinct
  end

  def split_params! search
    params[:q][search] = params[:q][search].split(" ") if params[:q] && params[:q][search]
  end

  def collection
    split_params! :comment_or_objectid_cont_any
    @time_tables = select_time_tables.search(params[:q]).result.paginate(page: params[:page])
  end
end