From adff72c7c3f257d315e55208df97868c0a1db898 Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Tue, 21 Mar 2017 09:40:10 +0100 Subject: Find Referential in Organisation#find_referential. Use it in ReferentialSupport and ReferentialsController. Refs #2900 --- app/controllers/concerns/referential_support.rb | 14 ++------------ app/controllers/referentials_controller.rb | 2 +- app/models/organisation.rb | 13 +++++++++++++ 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/app/controllers/concerns/referential_support.rb b/app/controllers/concerns/referential_support.rb index 34080d991..fe75e3579 100644 --- a/app/controllers/concerns/referential_support.rb +++ b/app/controllers/concerns/referential_support.rb @@ -16,17 +16,7 @@ module ReferentialSupport end def find_referential - organisation_referential = current_organisation.referentials.find_by id: params[:referential_id] - return organisation_referential if organisation_referential - - current_organisation.workbenches.each do |workbench| - workbench_referential = workbench.all_referentials.find_by id: params[:referential_id] - return workbench_referential if workbench_referential - end - - raise ActiveRecord::RecordNotFound + current_organisation.find_referential params[:referential_id] end -end - - +end diff --git a/app/controllers/referentials_controller.rb b/app/controllers/referentials_controller.rb index 9a9e9b151..ed20a6d17 100644 --- a/app/controllers/referentials_controller.rb +++ b/app/controllers/referentials_controller.rb @@ -73,7 +73,7 @@ class ReferentialsController < BreadcrumbController def resource - @referential ||= current_organisation.referentials.find_by_id(params[:id]) + @referential ||= current_organisation.find_referential(params[:id]) end def collection diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 8547ce5e1..d0742bda6 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -63,4 +63,17 @@ class Organisation < ActiveRecord::Base puts "✓ Organisation #{org.name} has been updated" unless Rails.env.test? end end + + def find_referential(referential_id) + organisation_referential = referentials.find_by id: referential_id + return organisation_referential if organisation_referential + + workbenches.each do |workbench| + workbench_referential = workbench.all_referentials.find_by id: referential_id + return workbench_referential if workbench_referential + end + + raise ActiveRecord::RecordNotFound + end + end -- cgit v1.2.3