Merge pull request #1930 from tchak/procedure-path-test-procedure

Add test_procedure to procedure_path
This commit is contained in:
Paul Chavard 2018-05-23 12:09:13 +02:00 committed by GitHub
commit b421f48b21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 62 additions and 34 deletions

View file

@ -63,11 +63,6 @@ class Admin::ProceduresController < AdminController
def hide
procedure = current_administrateur.procedures.find(params[:id])
procedure.hide!
# procedure should no longer be reachable so we delete its procedure_path
# that way it is also available for another procedure
# however, sometimes the path has already been deleted (ex: stolen by another procedure),
# so we're not certain the procedure has a procedure_path anymore
procedure.procedure_path.try(:destroy)
flash.notice = "Procédure supprimée, en cas d'erreur contactez nous : contact@demarches-simplifiees.fr"
redirect_to admin_procedures_draft_path

View file

@ -41,28 +41,34 @@ class Users::DossiersController < UsersController
array: true
end
def commencer_test
procedure_path = ProcedurePath.find_by(path: params[:procedure_path])
procedure = procedure_path.test_procedure
if procedure.present?
redirect_to new_users_dossier_path(procedure_id: procedure.id)
else
flash.alert = "Procédure inconnue"
redirect_to root_path
end
end
def commencer
if params[:procedure_path].present?
procedure_path = ProcedurePath.where(path: params[:procedure_path]).last
procedure_path = ProcedurePath.find_by(path: params[:procedure_path])
procedure = procedure_path.procedure
if procedure_path.nil? || procedure_path.procedure.nil?
flash.alert = "Procédure inconnue"
return redirect_to root_path
if procedure.present?
if procedure.archivee?
@dossier = Dossier.new(procedure: procedure)
render 'commencer/archived'
else
procedure = procedure_path.procedure
redirect_to new_users_dossier_path(procedure_id: procedure.id)
end
else
flash.alert = "Procédure inconnue"
redirect_to root_path
end
if procedure.archivee?
@dossier = Dossier.new(procedure: procedure)
return render 'commencer/archived'
end
redirect_to new_users_dossier_path(procedure_id: procedure.id)
rescue ActiveRecord::RecordNotFound
error_procedure
end
def new

View file

@ -12,7 +12,6 @@ class ProcedureDashboard < Administrate::BaseDashboard
types_de_champ: TypesDeChampCollectionField,
path: ProcedureLinkField,
dossiers: Field::HasMany,
procedure_path: Field::HasOne,
administrateur: Field::BelongsTo,
id: Field::Number,
libelle: Field::String,

View file

@ -4,8 +4,6 @@ class Procedure < ApplicationRecord
has_many :types_de_champ_private, -> { private_only }, class_name: 'TypeDeChamp', dependent: :destroy
has_many :dossiers
has_one :procedure_path, dependent: :destroy
has_one :module_api_carto, dependent: :destroy
has_one :attestation_template, dependent: :destroy
@ -61,10 +59,15 @@ class Procedure < ApplicationRecord
Dossier.new(procedure: self, champs: champs, champs_private: champs_private)
end
def procedure_path
ProcedurePath.find_with_procedure(self)
end
def hide!
now = DateTime.now
self.update(hidden_at: now, aasm_state: :hidden)
self.dossiers.update_all(hidden_at: now)
update(hidden_at: now, aasm_state: :hidden)
procedure_path&.hide!(self)
dossiers.update_all(hidden_at: now)
end
def path

View file

@ -3,6 +3,23 @@ class ProcedurePath < ApplicationRecord
validates :administrateur_id, presence: true, allow_blank: false, allow_nil: false
validates :procedure_id, presence: true, allow_blank: false, allow_nil: false
belongs_to :test_procedure, class_name: 'Procedure'
belongs_to :procedure
belongs_to :administrateur
def self.find_with_procedure(procedure)
where(procedure: procedure).or(where(test_procedure: procedure)).last
end
def hide!(procedure)
if self.procedure == procedure
update(procedure: nil)
end
if self.test_procedure == procedure
update(test_procedure: nil)
end
if procedure.nil? && test_procedure.nil?
destroy
end
end
end

View file

@ -211,6 +211,7 @@ Rails.application.routes.draw do
end
namespace :commencer do
get '/test/:procedure_path' => '/users/dossiers#commencer_test', as: :test
get '/:procedure_path' => '/users/dossiers#commencer'
end

View file

@ -0,0 +1,5 @@
class AddTestProcedureToProcedurePaths < ActiveRecord::Migration[5.2]
def change
add_reference :procedure_paths, :test_procedure
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_05_15_135415) do
ActiveRecord::Schema.define(version: 2018_05_16_155238) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -425,7 +425,9 @@ ActiveRecord::Schema.define(version: 2018_05_15_135415) do
t.integer "administrateur_id"
t.datetime "created_at"
t.datetime "updated_at"
t.bigint "test_procedure_id"
t.index ["path"], name: "index_procedure_paths_on_path"
t.index ["test_procedure_id"], name: "index_procedure_paths_on_test_procedure_id"
end
create_table "procedure_presentations", id: :serial, force: :cascade do |t|

View file

@ -547,7 +547,7 @@ describe Admin::ProceduresController, type: :controller do
end
end
describe 'POST transfer' do
describe 'POST #transfer' do
let!(:procedure) { create :procedure, administrateur: admin }
subject { post :transfer, params: { email_admin: email_admin, procedure_id: procedure.id } }
@ -587,17 +587,17 @@ describe Admin::ProceduresController, type: :controller do
end
end
describe "POST hide" do
describe "POST #hide" do
subject { post :hide, params: { id: procedure.id } }
context "when procedure is not owned by administrateur" do
let!(:procedure) { create :procedure, administrateur: create(:administrateur) }
let(:procedure) { create :procedure, administrateur: create(:administrateur) }
it { expect{ subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
context "when procedure is owned by administrateur" do
let!(:procedure) { create :procedure, :published, administrateur: admin }
let(:procedure) { create :procedure, :published, administrateur: admin }
before do
subject
@ -605,11 +605,11 @@ describe Admin::ProceduresController, type: :controller do
end
it { expect(procedure.hidden_at).not_to be_nil }
it { expect(procedure.procedure_path).to be_nil }
it { expect(procedure.procedure_path.procedure).to be_nil }
end
context "when procedure has no path" do
let!(:procedure) { create :procedure, administrateur: admin }
let(:procedure) { create :procedure, administrateur: admin }
it { expect{ subject }.not_to raise_error }
it do