aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlban Peignier2018-03-30 11:54:52 +0200
committerGitHub2018-03-30 11:54:52 +0200
commit9e3b1457d737092fc2b220ac9678407dcafcaf93 (patch)
treefdbf74b065aabc36847db68d1779232230199bc3
parentb381eb1844b869958282bccf4f0a2f55ac0d12d0 (diff)
parent5474cd7f1ad4a423d65c78164aca93665ff02756 (diff)
downloadchouette-core-9e3b1457d737092fc2b220ac9678407dcafcaf93.tar.bz2
Merge pull request #428 from af83/4658-remove-current_functional_scope
Remove `current_functional_scope`. Refs #4658
-rw-r--r--app/controllers/application_controller.rb6
-rw-r--r--app/controllers/referentials_controller.rb2
-rw-r--r--app/models/chouette/line.rb8
-rw-r--r--app/models/organisation.rb4
-rw-r--r--app/models/referential.rb4
-rw-r--r--app/models/referential_metadata.rb4
-rw-r--r--app/views/referentials/_form.html.slim2
-rw-r--r--db/schema.rb1
-rw-r--r--spec/models/referential_metadata_spec.rb19
-rw-r--r--spec/models/referential_spec.rb2
10 files changed, 35 insertions, 17 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/db/schema.rb b/db/schema.rb
index d90bf7b6c..77e35f449 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -18,7 +18,6 @@ ActiveRecord::Schema.define(version: 20180319043333) do
enable_extension "hstore"
enable_extension "postgis"
enable_extension "unaccent"
- enable_extension "objectid"
create_table "access_links", id: :bigserial, force: :cascade do |t|
t.integer "access_point_id", limit: 8
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 }