diff options
4 files changed, 76 insertions, 1 deletions
diff --git a/app/controllers/referential_vehicle_journeys_controller.rb b/app/controllers/referential_vehicle_journeys_controller.rb index 217fb9629..f93de29cc 100644 --- a/app/controllers/referential_vehicle_journeys_controller.rb +++ b/app/controllers/referential_vehicle_journeys_controller.rb @@ -10,9 +10,12 @@ class ReferentialVehicleJourneysController < ChouetteController private def collection - @q ||= end_of_association_chain.ransack(params[:q]) + @q ||= end_of_association_chain + @q = @q.with_stop_area_ids(params[:q][:stop_area_ids]) if params[:q] && params[:q][:stop_area_ids] + @q = @q.ransack(params[:q]) @vehicle_journeys ||= @q.result.order(:published_journey_name).includes(:vehicle_journey_at_stops).paginate page: params[:page], per_page: 10 @all_companies = Chouette::Company.where("id IN (#{@referential.vehicle_journeys.select(:company_id).to_sql})").distinct + @all_stop_areas = Chouette::StopArea.where("id IN (#{@referential.vehicle_journeys.joins(:stop_areas).select("stop_areas.id").to_sql})").distinct end end diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 11da77948..a8c9c1824 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -22,6 +22,7 @@ module Chouette belongs_to :company belongs_to :route belongs_to :journey_pattern + has_many :stop_areas, through: :journey_pattern has_and_belongs_to_many :footnotes, :class_name => 'Chouette::Footnote' has_and_belongs_to_many :purchase_windows, :class_name => 'Chouette::PurchaseWindow' @@ -42,6 +43,18 @@ module Chouette before_validation :set_default_values, :calculate_vehicle_journey_at_stop_day_offset + scope :with_stop_area_ids, ->(ids){ + _ids = ids.select(&:present?).map(&:to_i) + if _ids.present? + where("array(SELECT stop_points.stop_area_id::integer FROM stop_points INNER JOIN journey_patterns_stop_points ON journey_patterns_stop_points.stop_point_id = stop_points.id WHERE journey_patterns_stop_points.journey_pattern_id = vehicle_journeys.journey_pattern_id) @> array[?]", _ids) + else + all + end + } + + # We need this for the ransack object in the filters + ransacker :stop_area_ids + # TODO: Remove this validator # We've eliminated this validation because it prevented vehicle journeys # from being saved with at-stops having a day offset greater than 0, diff --git a/app/views/referential_vehicle_journeys/_filters.html.slim b/app/views/referential_vehicle_journeys/_filters.html.slim index 4506251c3..609927615 100644 --- a/app/views/referential_vehicle_journeys/_filters.html.slim +++ b/app/views/referential_vehicle_journeys/_filters.html.slim @@ -16,6 +16,10 @@ .form-group.w10.to= I18n.t('vehicle_journeys.form.to') = f.input :published_journey_name_lteq, label: false, wrapper_html: { class: 'w45'} + .form-group.togglable + = f.label Chouette::StopArea.model_name.human.pluralize, required: false, class: 'control-label' + = f.input :stop_area_ids, collection: @all_stop_areas.select(:id, :name).order(name: :asc), checked: params[:q] && params[:q][:stop_area_ids], as: :check_boxes, label: false, label_method: lambda{|l| ("<span>" + l.name + "</span>").html_safe}, required: false, wrapper_html: { class: 'checkbox_list'}, multiple: true + .actions = link_to 'Effacer', referential_vehicle_journeys_path(@referential), class: 'btn btn-link' = f.submit 'Filtrer', class: 'btn btn-default' diff --git a/spec/controllers/referential_vehicle_journeys_controller_spec.rb b/spec/controllers/referential_vehicle_journeys_controller_spec.rb index 842a6665e..cc6b44b9d 100644 --- a/spec/controllers/referential_vehicle_journeys_controller_spec.rb +++ b/spec/controllers/referential_vehicle_journeys_controller_spec.rb @@ -38,6 +38,61 @@ RSpec.describe ReferentialVehicleJourneysController, type: :controller do get :index, referential_id: referential expect(assigns[:vehicle_journeys].total_entries).to be(1) end + + context "when filtered on stop areas" do + let!(:request){ + get :index, referential_id: referential, q: q + } + + let(:stop_area_ids){ [] } + + def create_journey_pattern_with_stop_areas(*stop_areas) + j = create(:journey_pattern) + stop_areas.each do |area| + sp = create(:stop_point, stop_area: area) + j.stop_points << sp + end + j.save + j + end + + let(:q){ {stop_area_ids: stop_area_ids}} + let(:stop_area_1){ create :stop_area } + let(:stop_area_2){ create :stop_area } + let!(:journey_1){ create_journey_pattern_with_stop_areas(stop_area_1)} + let!(:journey_2){ create_journey_pattern_with_stop_areas(stop_area_2)} + let!(:journey_1_and_2){ create_journey_pattern_with_stop_areas(stop_area_1, stop_area_2)} + let!(:vehicle_journey_1){ create(:vehicle_journey, journey_pattern: journey_1)} + let!(:vehicle_journey_2){ create(:vehicle_journey, journey_pattern: journey_2)} + let!(:vehicle_journey_1_and_2){ create(:vehicle_journey, journey_pattern: journey_1_and_2)} + + context "with one stop" do + let(:stop_area_ids){[stop_area_1.id]} + it "should apply filters" do + expect(vehicle_journey_1.stop_areas).to include stop_area_1 + expect(vehicle_journey_2.stop_areas).to_not include stop_area_1 + expect(vehicle_journey_1_and_2.stop_areas).to include stop_area_1 + expect(assigns[:vehicle_journeys]).to include(vehicle_journey_1) + expect(assigns[:vehicle_journeys]).to_not include(vehicle_journey_2) + expect(assigns[:vehicle_journeys]).to include(vehicle_journey_1_and_2) + end + end + + context "with 2 stops" do + let(:stop_area_ids){[stop_area_1.id, stop_area_2.id]} + it "should apply filters" do + expect(vehicle_journey_1.stop_areas).to include stop_area_1 + expect(vehicle_journey_1.stop_areas).to_not include stop_area_2 + expect(vehicle_journey_2.stop_areas).to include stop_area_2 + expect(vehicle_journey_2.stop_areas).to_not include stop_area_1 + expect(vehicle_journey_1_and_2.stop_areas).to include stop_area_1 + expect(vehicle_journey_1_and_2.stop_areas).to include stop_area_2 + expect(assigns[:vehicle_journeys]).to_not include(vehicle_journey_1) + expect(assigns[:vehicle_journeys]).to_not include(vehicle_journey_2) + expect(assigns[:vehicle_journeys]).to include(vehicle_journey_1_and_2) + end + end + end end end |
