diff options
| author | Zog | 2017-12-26 11:23:53 +0100 |
|---|---|---|
| committer | Zog | 2017-12-26 11:23:53 +0100 |
| commit | d3445d085b607544dc6355956cd228835258d612 (patch) | |
| tree | 62588979f26e42e1cbbffab91690256281008b1c | |
| parent | 9fe0ca13d7caba486561f52557c1fa3d2f9f0a52 (diff) | |
| download | chouette-core-d3445d085b607544dc6355956cd228835258d612.tar.bz2 | |
Refs #5382 @1h; Enable all area types for Stopareas in Routes
Given that the organisation has the matching feature.
| -rw-r--r-- | app/controllers/autocomplete_stop_areas_controller.rb | 9 | ||||
| -rw-r--r-- | app/javascript/routes/components/BSelect2.js | 2 | ||||
| -rw-r--r-- | app/policies/stop_area_policy.rb | 8 | ||||
| -rw-r--r-- | spec/controllers/autocomplete_stop_areas_controller_spec.rb | 24 | ||||
| -rw-r--r-- | spec/support/controller_spec_helper.rb | 15 |
5 files changed, 56 insertions, 2 deletions
diff --git a/app/controllers/autocomplete_stop_areas_controller.rb b/app/controllers/autocomplete_stop_areas_controller.rb index be1badff0..d82fa316a 100644 --- a/app/controllers/autocomplete_stop_areas_controller.rb +++ b/app/controllers/autocomplete_stop_areas_controller.rb @@ -17,13 +17,20 @@ class AutocompleteStopAreasController < ChouetteController scope = scope.possible_parents if relation_parent? scope = scope.possible_parents if relation_children? end + if search_scope.present? + scope = StopAreaPolicy::Scope.new(current_user, scope).search_scope(search_scope) + end args = [].tap{|arg| 4.times{arg << "%#{params[:q]}%"}} @stop_areas = scope.where("unaccent(name) ILIKE unaccent(?) OR unaccent(city_name) ILIKE unaccent(?) OR registration_number ILIKE ? OR objectid ILIKE ?", *args).limit(50) @stop_areas end def target_type? - params.has_key?( :target_type) + params.has_key?(:target_type) + end + + def search_scope + params[:scope] end def relation_parent? diff --git a/app/javascript/routes/components/BSelect2.js b/app/javascript/routes/components/BSelect2.js index 340d9df95..29983dd58 100644 --- a/app/javascript/routes/components/BSelect2.js +++ b/app/javascript/routes/components/BSelect2.js @@ -96,7 +96,7 @@ class BSelect2 extends Component{ data: function(params) { return { q: params.term, - target_type: 'zdep' + scope: 'route_editor' }; }, processResults: function(data, params) { diff --git a/app/policies/stop_area_policy.rb b/app/policies/stop_area_policy.rb index e5921ef61..6db48b702 100644 --- a/app/policies/stop_area_policy.rb +++ b/app/policies/stop_area_policy.rb @@ -1,5 +1,13 @@ class StopAreaPolicy < ApplicationPolicy class Scope < Scope + def search_scope scope_name + scope = resolve + if scope_name&.to_s == "route_editor" + scope = scope.where(area_type: 'zdep') unless user.organisation.has_feature?("route_stop_areas_all_types") + end + scope + end + def resolve scope end diff --git a/spec/controllers/autocomplete_stop_areas_controller_spec.rb b/spec/controllers/autocomplete_stop_areas_controller_spec.rb index 50fc877dd..2af471361 100644 --- a/spec/controllers/autocomplete_stop_areas_controller_spec.rb +++ b/spec/controllers/autocomplete_stop_areas_controller_spec.rb @@ -5,6 +5,8 @@ RSpec.describe AutocompleteStopAreasController, type: :controller do let(:referential) { Referential.first } let!(:stop_area) { create :stop_area, name: 'écolà militaire' } + let!(:zdep_stop_area) { create :stop_area, area_type: "zdep" } + let!(:not_zdep_stop_area) { create :stop_area, area_type: "lda" } describe 'GET #index' do it 'should be successful' do @@ -26,4 +28,26 @@ RSpec.describe AutocompleteStopAreasController, type: :controller do end end end + + context "when searching from the route editor" do + let(:scope) { :route_editor } + let(:request){ + get :index, referential_id: referential.id, scope: scope + } + it "should filter stop areas based on type" do + request + expect(assigns(:stop_areas)).to include(zdep_stop_area) + expect(assigns(:stop_areas)).to_not include(not_zdep_stop_area) + end + + with_feature :route_stop_areas_all_types do + it "should not filter stop areas based on type" do + request + expect(assigns(:stop_areas)).to include(zdep_stop_area) + expect(assigns(:stop_areas)).to include(not_zdep_stop_area) + end + end + end + + end diff --git a/spec/support/controller_spec_helper.rb b/spec/support/controller_spec_helper.rb index 1d0288dea..dbc7d582b 100644 --- a/spec/support/controller_spec_helper.rb +++ b/spec/support/controller_spec_helper.rb @@ -11,6 +11,21 @@ module ControllerSpecHelper end end + def with_feature feature, &block + context "with feature #{feature}" do + login_user + before(:each) do + organisation = @user.organisation + unless organisation.has_feature?(feature) + organisation.features << feature + organisation.save! + end + sign_in @user + end + context('', &block) if block_given? + end + end + end RSpec.configure do |config| |
