aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/referentials_controller.rb11
-rw-r--r--app/models/referential.rb37
-rw-r--r--app/models/referential_metadata.rb11
-rw-r--r--app/models/workbench.rb2
-rw-r--r--app/views/referential_lines/show.html.slim2
-rw-r--r--app/views/referentials/_form.html.slim6
-rw-r--r--app/views/workbenches/show.html.slim7
-rw-r--r--config/locales/referentials.fr.yml2
-rw-r--r--spec/features/vehicle_journey_imports_spec.rb24
-rw-r--r--spec/models/referential_spec.rb2
10 files changed, 73 insertions, 31 deletions
diff --git a/app/controllers/referentials_controller.rb b/app/controllers/referentials_controller.rb
index ae3411dab..b7e6d8031 100644
--- a/app/controllers/referentials_controller.rb
+++ b/app/controllers/referentials_controller.rb
@@ -11,7 +11,7 @@ class ReferentialsController < BreadcrumbController
new! do
@referential.data_format = current_organisation.data_format
- @referential.workbench_id = params[:workbench_id]
+ @referential.workbench_id ||= params[:workbench_id]
if @referential.in_workbench?
@referential.init_metadatas first_period_begin: Date.today, first_period_end: Date.today.advance(months: 1)
@@ -53,8 +53,13 @@ class ReferentialsController < BreadcrumbController
redirect_to workbench_path(referential.workbench_id), notice: t('notice.referential.archived')
end
def unarchive
- referential.unarchive!
- redirect_to workbench_path(referential.workbench_id), notice: t('notice.referential.unarchived')
+ if referential.unarchive!
+ flash[:notice] = t('notice.referential.unarchived')
+ else
+ flash[:alert] = t('notice.referential.unarchived_failed')
+ end
+
+ redirect_to workbench_path(referential.workbench_id)
end
protected
diff --git a/app/models/referential.rb b/app/models/referential.rb
index 604181c0a..be7c15535 100644
--- a/app/models/referential.rb
+++ b/app/models/referential.rb
@@ -133,6 +133,7 @@ class Referential < ActiveRecord::Base
stop_area_referential: from.stop_area_referential,
workbench: from.workbench,
created_from: from,
+ metadatas: from.metadatas.map { |m| ReferentialMetadata.new_from(m) }
})
end
@@ -176,7 +177,6 @@ class Referential < ActiveRecord::Base
before_validation :clone_associations, :on => :create, if: :created_from
before_create :create_schema
- before_validation :clone_metadatas, on: :create, if: :created_from
after_create :clone_schema, if: :created_from
before_destroy :destroy_schema
@@ -216,6 +216,36 @@ class Referential < ActiveRecord::Base
metadatas.present? ? metadatas.first.lines : []
end
+ def overlapped_referential_ids
+ return [] unless metadatas.present?
+
+ line_ids = metadatas.first.line_ids
+ period = metadatas.first.periodes.first
+
+ not_myself = "and referential_id != #{id}" if persisted?
+
+ query = "SELECT distinct(referential_id) FROM
+ (SELECT unnest(public.referential_metadata.line_ids) as line, unnest(public.referential_metadata.periodes) as period, public.referential_metadata.referential_id
+ FROM public.referential_metadata
+ INNER JOIN public.referentials ON public.referential_metadata.referential_id = public.referentials.id
+ WHERE public.referentials.workbench_id = 1 and public.referentials.archived_at is null) as metadatas
+ WHERE line in (#{line_ids.join(',')}) and period && '#{ActiveRecord::ConnectionAdapters::PostgreSQLColumn.range_to_string(period)}' #{not_myself};"
+
+ self.class.connection.select_values(query).map(&:to_i)
+ end
+
+ def metadatas_overlap?
+ overlapped_referential_ids.present?
+ end
+
+ validate :detect_overlapped_referentials
+
+ def detect_overlapped_referentials
+ self.class.where(id: overlapped_referential_ids).each do |referential|
+ errors.add :metadatas, I18n.t("referentials.errors.overlapped_referential", :referential => referential.name)
+ end
+ end
+
def clone_schema
ReferentialCloning.create(source_referential: self.created_from, target_referential: self)
end
@@ -287,8 +317,13 @@ class Referential < ActiveRecord::Base
touch :archived_at
end
def unarchive!
+ return false unless can_unarchive?
# self.archived = false
update_column :archived_at, nil
end
+ def can_unarchive?
+ not metadatas_overlap?
+ end
+
end
diff --git a/app/models/referential_metadata.rb b/app/models/referential_metadata.rb
index 3f5ed4db8..d1300b690 100644
--- a/app/models/referential_metadata.rb
+++ b/app/models/referential_metadata.rb
@@ -63,17 +63,6 @@ class ReferentialMetadata < ActiveRecord::Base
end
end
- # def lines_with_ids_support=(values)
- # if String === values.first
- # self.line_values = values
- # else
- # self.lines_without_values_support = values
- # end
- # end
- # alias_method_chain :lines, :ids_support
-
- #delegate :begin, :begin=, :end, :end=, to: :first_period, prefix: :first_period
-
def self.new_from from
from.dup.tap do |metadata|
metadata.referential_id = nil
diff --git a/app/models/workbench.rb b/app/models/workbench.rb
index b5ab37222..419790edb 100644
--- a/app/models/workbench.rb
+++ b/app/models/workbench.rb
@@ -13,5 +13,5 @@ class Workbench < ActiveRecord::Base
validates :organisation, presence: true
has_many :referentials
- has_many :referential_metadatas, through: :referentials
+ has_many :referential_metadatas, through: :referentials, source: :metadatas
end
diff --git a/app/views/referential_lines/show.html.slim b/app/views/referential_lines/show.html.slim
index afb5903e0..e32978b20 100644
--- a/app/views/referential_lines/show.html.slim
+++ b/app/views/referential_lines/show.html.slim
@@ -51,7 +51,7 @@ h2
= " " + t("enumerize.line.transport_mode.#{@line.transport_mode}")
- else
= " -"
-
+
p
label = "#{@line.human_attribute_name('transport_submode')} : "
- if @line.transport_submode
diff --git a/app/views/referentials/_form.html.slim b/app/views/referentials/_form.html.slim
index 2061d02ff..a4342bc8e 100644
--- a/app/views/referentials/_form.html.slim
+++ b/app/views/referentials/_form.html.slim
@@ -23,6 +23,12 @@
= form.input :lower_corner, input_html: { title: t("formtastic.titles.referential.lower_corner") }
/ = form.input :data_format, label: true, include_blank: false
+
+ - if @referential.errors.has_key? :metadatas
+ ul.errors
+ - @referential.errors[:metadatas].each do |message|
+ li = message
+
= form.semantic_fields_for :metadatas do |subform|
= subform.input :first_period_begin, as: :date_select
= subform.input :first_period_end, as: :date_select
diff --git a/app/views/workbenches/show.html.slim b/app/views/workbenches/show.html.slim
index 045214022..06f64d3c4 100644
--- a/app/views/workbenches/show.html.slim
+++ b/app/views/workbenches/show.html.slim
@@ -52,9 +52,14 @@
li = link_to "Editer", edit_referential_path(referential)
+ li = link_to "Cloner", new_referential_path(from: referential)
+
li
- if referential.archived?
- = link_to "Désarchiver", unarchive_referential_path(referential), method: :put
+ - if referential.can_unarchive?
+ = link_to "Désarchiver", unarchive_referential_path(referential), method: :put
+ - else
+ = link_to "Désarchiver", "#"
- else
= link_to "Archiver", archive_referential_path(referential), method: :put
diff --git a/config/locales/referentials.fr.yml b/config/locales/referentials.fr.yml
index d9a9fc4ce..dc3d796b8 100644
--- a/config/locales/referentials.fr.yml
+++ b/config/locales/referentials.fr.yml
@@ -29,6 +29,7 @@ fr:
pg_excluded: "ne peut pas commencer par pg_ (valeurs réservées)"
public_excluded: "public est une valeur réservée"
user_excluded: "%{user} est une valeur réservée"
+ overlapped_referential: "%{referential} couvre le même périmètre d'offre"
activerecord:
models:
referential:
@@ -83,4 +84,5 @@ fr:
referential:
archived: "L'espace de données a été correctement archivé"
unarchived: "L'espace de données a été correctement désarchivé"
+ unarchived_failed: "L'espace de données ne peut être désarchivé"
deleted: "L'espace de données a été correctement supprimé"
diff --git a/spec/features/vehicle_journey_imports_spec.rb b/spec/features/vehicle_journey_imports_spec.rb
index f18d9baf0..262d585f2 100644
--- a/spec/features/vehicle_journey_imports_spec.rb
+++ b/spec/features/vehicle_journey_imports_spec.rb
@@ -4,7 +4,7 @@ require 'csv'
describe "VehicleJourneyImports", :type => :feature do
login_user
-
+
let!(:route) { create :route }
let(:valid_file_path) {
@@ -24,16 +24,16 @@ describe "VehicleJourneyImports", :type => :feature do
if counter == 0
row2 = []
row.each do |cell|
- cell = "" if cell == "import:VehicleJourney:1"
- cell = "" if cell == "import:VehicleJourney:2"
- cell = "" if cell == "import:VehicleJourney:3"
+ cell = "" if cell == "import:VehicleJourney:1"
+ cell = "" if cell == "import:VehicleJourney:2"
+ cell = "" if cell == "import:VehicleJourney:3"
row2 << cell
end
csv << row2
elsif counter < 8
csv << row
else
- csv << ( row[0] = route.stop_points[counter - 8].id; row)
+ csv << ( row[0] = route.stop_points[counter - 8].id; row)
end
counter += 1
end
@@ -42,14 +42,14 @@ describe "VehicleJourneyImports", :type => :feature do
File.open("/tmp/#{filename}")
end
-
- describe "new" do
+
+ describe "new" do
it "should create vehicle journey file and return to route show page" do
visit new_referential_line_route_vehicle_journey_import_path(referential, route.line, route)
attach_file('Fichier', valid_file_path)
click_button "Lancer l'import"
expect(page).to have_content(I18n.t("vehicle_journey_imports.new.success"))
- expect(page).to have_content("Séquence d'arrêts #{route.name}")
+ expect(page).to have_content("Itinéraire #{route.name}")
end
it "should return error messages when file is invalid" do
@@ -58,12 +58,12 @@ describe "VehicleJourneyImports", :type => :feature do
click_button "Lancer l'import"
expect(page).to have_content(I18n.t("vehicle_journey_imports.errors.import_aborted"))
end
-
+
it "should return error message when file missing on upload" do
visit new_referential_line_route_vehicle_journey_import_path(referential, route.line, route)
click_button "Lancer l'import"
- expect(page).to have_content(I18n.t("vehicle_journey_imports.errors.import_aborted"))
- end
+ expect(page).to have_content(I18n.t("vehicle_journey_imports.errors.import_aborted"))
+ end
end
-
+
end
diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb
index c1205c9f0..4c9338aa6 100644
--- a/spec/models/referential_spec.rb
+++ b/spec/models/referential_spec.rb
@@ -12,7 +12,7 @@ describe Referential, :type => :model do
it { should belong_to(:workbench) }
context "Cloning referential" do
- let(:cloned) { create(:referential, created_from: ref) }
+ let(:cloned) { Referential.new_from(ref).tap(&:save!) }
it 'should create a ReferentialCloning' do
expect { cloned }.to change{ReferentialCloning.count}.by(1)