diff options
| author | Zog | 2018-03-29 14:38:45 +0200 |
|---|---|---|
| committer | Zog | 2018-03-29 14:38:45 +0200 |
| commit | ef01bea2bef74358e3df3e74cc613d664a9a554d (patch) | |
| tree | 19b375858b37645754198c048841458621d23737 | |
| parent | 1019debe93d776f0d38ae69099e6492a7dd4a87c (diff) | |
| download | chouette-core-ef01bea2bef74358e3df3e74cc613d664a9a554d.tar.bz2 | |
Refs #4658; Remove `current_functional_scope`
| -rw-r--r-- | app/controllers/application_controller.rb | 6 | ||||
| -rw-r--r-- | app/controllers/referentials_controller.rb | 2 | ||||
| -rw-r--r-- | app/models/chouette/line.rb | 8 | ||||
| -rw-r--r-- | app/models/organisation.rb | 4 | ||||
| -rw-r--r-- | app/models/referential.rb | 4 | ||||
| -rw-r--r-- | app/models/referential_metadata.rb | 4 | ||||
| -rw-r--r-- | app/views/referentials/_form.html.slim | 2 | ||||
| -rw-r--r-- | spec/models/referential_metadata_spec.rb | 19 | ||||
| -rw-r--r-- | spec/models/referential_spec.rb | 2 |
9 files changed, 35 insertions, 16 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c4961123d..9a83394e2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -36,12 +36,6 @@ class ApplicationController < ActionController::Base end helper_method :current_organisation - def current_functional_scope - functional_scope = current_organisation.sso_attributes.try(:[], "functional_scope") if current_organisation - JSON.parse(functional_scope) if functional_scope - end - helper_method :current_functional_scope - def collection_name self.class.name.split("::").last.gsub('Controller', '').underscore end diff --git a/app/controllers/referentials_controller.rb b/app/controllers/referentials_controller.rb index 6e3694547..fe661651e 100644 --- a/app/controllers/referentials_controller.rb +++ b/app/controllers/referentials_controller.rb @@ -143,7 +143,7 @@ class ReferentialsController < ChouetteController def build_referential if params[:from] source_referential = Referential.find(params[:from]) - @referential = Referential.new_from(source_referential, current_functional_scope) + @referential = Referential.new_from(source_referential, current_organisation) end @referential.data_format = current_organisation.data_format diff --git a/app/models/chouette/line.rb b/app/models/chouette/line.rb index ae7c25377..c8a02da1f 100644 --- a/app/models/chouette/line.rb +++ b/app/models/chouette/line.rb @@ -51,6 +51,14 @@ module Chouette ) } + scope :for_organisation, ->(organisation){ + if objectids = organisation&.lines_scope + where(objectid: objectids) + else + all + end + } + def self.nullable_attributes [:published_name, :number, :comment, :url, :color, :text_color, :stable_id] end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 745bc0d22..5bef67941 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -86,4 +86,8 @@ class Organisation < ActiveRecord::Base workbenches.default end + def lines_scope + functional_scope = sso_attributes.try(:[], "functional_scope") + JSON.parse(functional_scope) if functional_scope + end end diff --git a/app/models/referential.rb b/app/models/referential.rb index 91a88d02d..0e48be43f 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -233,7 +233,7 @@ class Referential < ActiveRecord::Base end end - def self.new_from(from, functional_scope) + def self.new_from(from, organisation) Referential.new( name: I18n.t("activerecord.copy", name: from.name), slug: "#{from.slug}_clone", @@ -244,7 +244,7 @@ class Referential < ActiveRecord::Base stop_area_referential: from.stop_area_referential, created_from: from, objectid_format: from.objectid_format, - metadatas: from.metadatas.map { |m| ReferentialMetadata.new_from(m, functional_scope) } + metadatas: from.metadatas.map { |m| ReferentialMetadata.new_from(m, organisation) } ) end diff --git a/app/models/referential_metadata.rb b/app/models/referential_metadata.rb index 393dc70d3..017eb1449 100644 --- a/app/models/referential_metadata.rb +++ b/app/models/referential_metadata.rb @@ -155,10 +155,10 @@ class ReferentialMetadata < ActiveRecord::Base end private :clear_periods - def self.new_from(from, functional_scope) + def self.new_from(from, organisation) from.dup.tap do |metadata| metadata.referential_source_id = from.referential_id - metadata.line_ids = from.referential.lines.where(id: metadata.line_ids, objectid: functional_scope).collect(&:id) + metadata.line_ids = from.referential.lines.where(id: metadata.line_ids).for_organisation(organisation).pluck(:id) metadata.referential_id = nil end end diff --git a/app/views/referentials/_form.html.slim b/app/views/referentials/_form.html.slim index 96d847ec1..c378f871e 100644 --- a/app/views/referentials/_form.html.slim +++ b/app/views/referentials/_form.html.slim @@ -49,7 +49,7 @@ .separator .row .col-lg-11 - = subform.input :lines, as: :select, collection: Chouette::Line.includes(:company).order(:name).where(objectid: current_functional_scope), selected: subform.object.line_ids, label_method: :display_name, input_html: { 'data-select2ed': 'true', 'data-select2ed-placeholder': t('simple_form.labels.referential.placeholders.select_lines'), 'multiple': 'multiple', style: 'width: 100%' } + = subform.input :lines, as: :select, collection: Chouette::Line.includes(:company).order(:name).for_organisation(current_organisation), selected: subform.object.line_ids, label_method: :display_name, input_html: { 'data-select2ed': 'true', 'data-select2ed-placeholder': t('simple_form.labels.referential.placeholders.select_lines'), 'multiple': 'multiple', style: 'width: 100%' } .col-lg-1 a.clear-lines.btn.btn-default href='#' .fa.fa-trash diff --git a/spec/models/referential_metadata_spec.rb b/spec/models/referential_metadata_spec.rb index 291ed974a..88a12b2bb 100644 --- a/spec/models/referential_metadata_spec.rb +++ b/spec/models/referential_metadata_spec.rb @@ -12,14 +12,19 @@ RSpec.describe ReferentialMetadata, :type => :model do describe ".new_from" do let(:referential_metadata) { create :referential_metadata, referential_source: create(:referential) } - let(:new_referential_metadata) { ReferentialMetadata.new_from(referential_metadata, []) } + let(:new_referential_metadata) { ReferentialMetadata.new_from(referential_metadata, nil) } + before do + referential_metadata.line_ids.each do |id| + Chouette::Line.find(id).update_attribute :line_referential_id, referential_metadata.referential.line_referential_id + end + end it "should not have an associated referential" do expect(new_referential_metadata).to be_a_new(ReferentialMetadata) end - xit "should have the same lines" do - expect(new_referential_metadata.lines).to eq(referential_metadata.lines) + it "should have the same lines" do + expect(new_referential_metadata.line_ids.sort).to eq(referential_metadata.line_ids.sort) end it "should have the same periods" do @@ -34,6 +39,14 @@ RSpec.describe ReferentialMetadata, :type => :model do expect(new_referential_metadata.referential_source).to eq(referential_metadata.referential) end + context "with a functional scope" do + let(:organisation){ create :organisation, sso_attributes: {"functional_scope" => [referential_metadata.referential.lines.first.objectid]} } + let(:new_referential_metadata) { ReferentialMetadata.new_from(referential_metadata, organisation) } + + it "should scope the lines" do + expect(new_referential_metadata.line_ids).to eq [referential_metadata.referential.lines.first.id] + end + end end describe "Period" do diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb index 1d9b3d78a..ca2caf57f 100644 --- a/spec/models/referential_spec.rb +++ b/spec/models/referential_spec.rb @@ -55,7 +55,7 @@ describe Referential, :type => :model do context "Cloning referential" do let(:clone) do - Referential.new_from(ref, []) + Referential.new_from(ref, nil) end let!(:workbench){ create :workbench } |
