diff options
| -rw-r--r-- | lib/stif/my_workbench_scopes.rb | 32 | ||||
| -rw-r--r-- | spec/models/workbench_spec.rb | 16 |
2 files changed, 32 insertions, 16 deletions
diff --git a/lib/stif/my_workbench_scopes.rb b/lib/stif/my_workbench_scopes.rb index 7e4500a7d..89c4e659c 100644 --- a/lib/stif/my_workbench_scopes.rb +++ b/lib/stif/my_workbench_scopes.rb @@ -1,21 +1,23 @@ -class Stif::MyWorkbenchScopes - attr_accessor :workbench +module Stif + class MyWorkbenchScopes + attr_accessor :workbench - def initialize(workbench) - @workbench = workbench - end + def initialize(workbench) + @workbench = workbench + end - def line_scope(initial_scope) - ids = self.parse_functional_scope - ids ? initial_scope.where(objectid: ids) : initial_scope - end + def line_scope(initial_scope) + ids = self.parse_functional_scope + ids ? initial_scope.where(objectid: ids) : initial_scope + end - def parse_functional_scope - return false unless @workbench.organisation.sso_attributes - begin - JSON.parse @workbench.organisation.sso_attributes['functional_scope'] - rescue Exception => e - Rails.logger.error "MyWorkbenchScopes : #{e}" + def parse_functional_scope + return false unless @workbench.organisation.sso_attributes + begin + JSON.parse @workbench.organisation.sso_attributes['functional_scope'] + rescue Exception => e + Rails.logger.error "MyWorkbenchScopes : #{e}" + end end end end diff --git a/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb index 2b9bb3d18..84149ddb0 100644 --- a/spec/models/workbench_spec.rb +++ b/spec/models/workbench_spec.rb @@ -1,7 +1,6 @@ require 'rails_helper' RSpec.describe Workbench, :type => :model do - it 'should have a valid factory' do expect(FactoryGirl.build(:workbench)).to be_valid end @@ -19,4 +18,19 @@ RSpec.describe Workbench, :type => :model do it { should have_many(:group_of_lines).through(:line_referential) } it { should have_many(:stop_areas).through(:stop_area_referential) } + + context '.lines' do + let!(:ids) { ['STIF:CODIFLIGNE:Line:C00840', 'STIF:CODIFLIGNE:Line:C00086'] } + let!(:organisation) { create :organisation, sso_attributes: { functional_scope: ids.to_json } } + let(:workbench) { create :workbench, organisation: organisation } + + it 'should filter lines based on my organisation functional_scope' do + ids.insert('STIF:CODIFLIGNE:Line:0000').each do |id| + create :line, objectid: id, line_referential: workbench.line_referential + end + lines = workbench.lines + expect(lines.count).to eq 2 + expect(lines.map(&:objectid)).to include(*ids) + end + end end |
