Merge pull request #1927 from tchak/procedure-state

Refactor procedure state to use AASM
This commit is contained in:
Paul Chavard 2018-05-23 17:53:05 +02:00 committed by GitHub
commit 7bf90e9076
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 139 additions and 107 deletions

View file

@ -109,22 +109,15 @@ class Admin::ProceduresController < AdminController
return redirect_to admin_procedures_path return redirect_to admin_procedures_path
end end
procedure_path = ProcedurePath.find_by(path: params[:procedure_path]) if procedure.may_publish?(params[:procedure_path])
if procedure_path procedure.publish!(params[:procedure_path])
if procedure_path.administrateur_id == current_administrateur.id
procedure_path.procedure.archive flash.notice = "Procédure publiée"
procedure_path.delete redirect_to admin_procedures_path
else else
@mine = false @mine = false
return render '/admin/procedures/publish', formats: 'js' render '/admin/procedures/publish', formats: 'js'
end
end end
procedure.publish!(params[:procedure_path])
flash.notice = "Procédure publiée"
render js: "window.location = '#{admin_procedures_path}'"
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
flash.alert = 'Procédure inexistante' flash.alert = 'Procédure inexistante'
redirect_to admin_procedures_path redirect_to admin_procedures_path
@ -149,7 +142,7 @@ class Admin::ProceduresController < AdminController
def archive def archive
procedure = current_administrateur.procedures.find(params[:procedure_id]) procedure = current_administrateur.procedures.find(params[:procedure_id])
procedure.archive procedure.archive!
flash.notice = "Procédure archivée" flash.notice = "Procédure archivée"
redirect_to admin_procedures_path redirect_to admin_procedures_path

View file

@ -7,7 +7,7 @@ class AutoArchiveProcedureJob < ApplicationJob
dossier.en_instruction! dossier.en_instruction!
end end
procedure.archive procedure.archive!
end end
end end
end end

View file

@ -101,6 +101,10 @@ class Administrateur < ApplicationRecord
save save
end end
def owns?(procedure)
id == procedure.administrateur_id
end
private private
def generate_api_token def generate_api_token

View file

@ -45,6 +45,77 @@ class Procedure < ApplicationRecord
validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :description, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false
include AASM
aasm whiny_persistence: true do
state :brouillon, initial: true
state :publiee
state :archivee
state :hidden
event :publish, after: :after_publish, guard: :can_publish? do
transitions from: :brouillon, to: :publiee
transitions from: :archivee, to: :publiee
end
event :archive, after: :after_archive do
transitions from: :publiee, to: :archivee
end
event :hide, after: :after_hide do
transitions from: :brouillon, to: :hidden
transitions from: :publiee, to: :hidden
transitions from: :archivee, to: :hidden
end
end
def after_publish(path)
now = Time.now
update(
test_started_at: now,
archived_at: nil,
published_at: now
)
procedure_path = ProcedurePath.find_by(path: path)
if procedure_path.present?
if procedure_path.procedure != self
procedure_path.procedure.archive!
procedure_path.update(procedure: self)
end
else
ProcedurePath.create(procedure: self, administrateur: administrateur, path: path)
end
end
def after_archive
update(archived_at: Time.now)
end
def after_hide
now = Time.now
update(hidden_at: now)
procedure_path&.hide!(self)
dossiers.update_all(hidden_at: now)
end
def locked?
publiee_ou_archivee?
end
def publiee_ou_archivee?
publiee? || archivee?
end
def can_publish?(path)
procedure_path = ProcedurePath.find_by(path: path)
if procedure_path.present?
administrateur.owns?(procedure_path)
else
true
end
end
# Warning: dossier after_save build_default_champs must be removed # Warning: dossier after_save build_default_champs must be removed
# to save a dossier created from this method # to save a dossier created from this method
def new_dossier def new_dossier
@ -63,13 +134,6 @@ class Procedure < ApplicationRecord
ProcedurePath.find_with_procedure(self) ProcedurePath.find_with_procedure(self)
end end
def hide!
now = DateTime.now
update(hidden_at: now, aasm_state: :hidden)
procedure_path&.hide!(self)
dossiers.update_all(hidden_at: now)
end
def path def path
procedure_path.path if procedure_path.present? procedure_path.path if procedure_path.present?
end end
@ -120,10 +184,6 @@ class Procedure < ApplicationRecord
end end
end end
def locked?
publiee_ou_archivee?
end
def clone(admin, from_library) def clone(admin, from_library)
procedure = self.deep_clone(include: procedure = self.deep_clone(include:
{ {
@ -161,32 +221,6 @@ class Procedure < ApplicationRecord
procedure procedure
end end
def brouillon?
published_at.nil?
end
def publish!(path)
now = Time.now
self.update!({ test_started_at: now, published_at: now, archived_at: nil, aasm_state: :publiee })
ProcedurePath.create!(path: path, procedure: self, administrateur: self.administrateur)
end
def publiee?
published_at.present? && archived_at.nil?
end
def archive
self.update!(archived_at: Time.now, aasm_state: :archivee)
end
def archivee?
published_at.present? && archived_at.present?
end
def publiee_ou_archivee?
publiee? || archivee?
end
def whitelisted? def whitelisted?
whitelisted_at.present? whitelisted_at.present?
end end

View file

@ -2,8 +2,7 @@ require 'spec_helper'
describe Admin::PiecesJustificativesController, type: :controller do describe Admin::PiecesJustificativesController, type: :controller do
let(:admin) { create(:administrateur) } let(:admin) { create(:administrateur) }
let(:published_at) { nil } let(:procedure) { create(:procedure, administrateur: admin) }
let(:procedure) { create(:procedure, administrateur: admin, published_at: published_at) }
before do before do
sign_in admin sign_in admin
end end
@ -19,7 +18,7 @@ describe Admin::PiecesJustificativesController, type: :controller do
end end
context 'when procedure is published' do context 'when procedure is published' do
let(:published_at) { Time.now } let(:procedure) { create(:procedure, :published, administrateur: admin) }
it { is_expected.to redirect_to admin_procedure_path id: procedure_id } it { is_expected.to redirect_to admin_procedure_path id: procedure_id }
end end

View file

@ -92,8 +92,8 @@ describe Admin::ProceduresController, type: :controller do
describe 'DELETE #destroy' do describe 'DELETE #destroy' do
let(:procedure_draft) { create :procedure, administrateur: admin, published_at: nil, archived_at: nil } let(:procedure_draft) { create :procedure, administrateur: admin, published_at: nil, archived_at: nil }
let(:procedure_published) { create :procedure, administrateur: admin, published_at: Time.now, archived_at: nil } let(:procedure_published) { create :procedure, administrateur: admin, aasm_state: :publiee, published_at: Time.now, archived_at: nil }
let(:procedure_archived) { create :procedure, administrateur: admin, published_at: nil, archived_at: Time.now } let(:procedure_archived) { create :procedure, administrateur: admin, aasm_state: :archivee, published_at: nil, archived_at: Time.now }
subject { delete :destroy, params: { id: procedure.id } } subject { delete :destroy, params: { id: procedure.id } }
@ -344,7 +344,7 @@ describe Admin::ProceduresController, type: :controller do
it 'publish the given procedure' do it 'publish the given procedure' do
expect(procedure.publiee?).to be_truthy expect(procedure.publiee?).to be_truthy
expect(procedure.path).to eq(procedure_path) expect(procedure.path).to eq(procedure_path)
expect(response.status).to eq 200 expect(response.status).to eq 302
expect(flash[:notice]).to have_content 'Procédure publiée' expect(flash[:notice]).to have_content 'Procédure publiée'
end end
end end
@ -355,7 +355,7 @@ describe Admin::ProceduresController, type: :controller do
it 'publish the given procedure' do it 'publish the given procedure' do
expect(procedure.publiee?).to be_truthy expect(procedure.publiee?).to be_truthy
expect(procedure.path).to eq(procedure_path) expect(procedure.path).to eq(procedure_path)
expect(response.status).to eq 200 expect(response.status).to eq 302
expect(flash[:notice]).to have_content 'Procédure publiée' expect(flash[:notice]).to have_content 'Procédure publiée'
end end
@ -432,8 +432,8 @@ describe Admin::ProceduresController, type: :controller do
procedure.reload procedure.reload
end end
it { expect(procedure.archivee?).to be_falsey } it { expect(procedure.publiee?).to be_truthy }
it { expect(response.status).to eq 200 } it { expect(response.status).to eq 302 }
it { expect(flash[:notice]).to have_content 'Procédure publiée' } it { expect(flash[:notice]).to have_content 'Procédure publiée' }
end end
end end

View file

@ -9,8 +9,7 @@ describe Admin::TypesDeChampController, type: :controller do
end end
describe 'GET #show' do describe 'GET #show' do
let(:published_at) { nil } let(:procedure) { create(:procedure, :published, administrateur: admin) }
let(:procedure) { create(:procedure, administrateur: admin, published_at: published_at) }
let(:procedure_id) { procedure.id } let(:procedure_id) { procedure.id }
subject { get :show, params: { procedure_id: procedure_id } } subject { get :show, params: { procedure_id: procedure_id } }
@ -21,7 +20,6 @@ describe Admin::TypesDeChampController, type: :controller do
end end
context 'when procedure is published' do context 'when procedure is published' do
let(:published_at) { Time.now }
it { is_expected.to redirect_to admin_procedure_path id: procedure_id } it { is_expected.to redirect_to admin_procedure_path id: procedure_id }
end end

View file

@ -9,8 +9,7 @@ describe Admin::TypesDeChampPrivateController, type: :controller do
end end
describe 'GET #show' do describe 'GET #show' do
let(:published_at) { nil } let(:procedure) { create(:procedure, :published, administrateur: admin) }
let(:procedure) { create(:procedure, administrateur: admin, published_at: published_at) }
let(:procedure_id) { procedure.id } let(:procedure_id) { procedure.id }
subject { get :show, params: { procedure_id: procedure_id } } subject { get :show, params: { procedure_id: procedure_id } }
@ -21,7 +20,6 @@ describe Admin::TypesDeChampPrivateController, type: :controller do
end end
context 'when procedure is published' do context 'when procedure is published' do
let(:published_at) { Time.now }
it { is_expected.to redirect_to admin_procedure_path id: procedure_id } it { is_expected.to redirect_to admin_procedure_path id: procedure_id }
end end

View file

@ -99,7 +99,7 @@ describe NewGestionnaire::ProceduresController, type: :controller do
context "with procedures assigned" do context "with procedures assigned" do
let(:procedure1) { create(:procedure, :published) } let(:procedure1) { create(:procedure, :published) }
let(:procedure2) { create(:procedure, :published, :archived) } let(:procedure2) { create(:procedure, :archived) }
let(:procedure3) { create(:procedure) } let(:procedure3) { create(:procedure) }
before do before do

View file

@ -26,7 +26,7 @@ shared_examples 'description_controller_spec' do
context 'procedure is archived' do context 'procedure is archived' do
render_views render_views
let(:archived_at) { Time.now } let(:archived) { true }
it { expect(response).to have_http_status(:success) } it { expect(response).to have_http_status(:success) }
it { expect(response.body).to_not have_content(I18n.t('errors.messages.procedure_archived')) } it { expect(response.body).to_not have_content(I18n.t('errors.messages.procedure_archived')) }
@ -329,7 +329,7 @@ shared_examples 'description_controller_spec' do
end end
context 'La procédure est archivée' do context 'La procédure est archivée' do
let(:archived_at) { Time.now } let(:archived) { true }
before do before do
post :update, params: { dossier_id: dossier.id } post :update, params: { dossier_id: dossier.id }

View file

@ -5,10 +5,15 @@ require 'controllers/users/description_controller_shared_example'
describe Users::DescriptionController, type: :controller, vcr: { cassette_name: 'controllers_users_description_controller' } do describe Users::DescriptionController, type: :controller, vcr: { cassette_name: 'controllers_users_description_controller' } do
let(:owner_user) { create(:user) } let(:owner_user) { create(:user) }
let(:invite_by_user) { create :user, email: 'invite@plop.com' } let(:invite_by_user) { create :user, email: 'invite@plop.com' }
let(:archived_at) { nil } let(:archived) { false }
let(:state) { 'en_construction' } let(:state) { 'en_construction' }
let(:procedure) do
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true, published_at: Time.now, archived_at: archived_at) } if archived
create(:procedure, :archived, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true)
else
create(:procedure, :published, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true)
end
end
let(:dossier) { create(:dossier, procedure: procedure, user: owner_user, state: state) } let(:dossier) { create(:dossier, procedure: procedure, user: owner_user, state: state) }
let(:dossier_id) { dossier.id } let(:dossier_id) { dossier.id }

View file

@ -159,17 +159,13 @@ describe Users::DossiersController, type: :controller do
it { expect(subject).to redirect_to new_users_dossier_path(procedure_id: procedure.id) } it { expect(subject).to redirect_to new_users_dossier_path(procedure_id: procedure.id) }
context 'when procedure is archived' do context 'when procedure is archived' do
let(:procedure) { create(:procedure, :published, archived_at: Time.now) } let(:procedure) { create(:procedure, :archived) }
before do
procedure.update_column :archived_at, Time.now
end
it { expect(subject.status).to eq 200 } it { expect(subject.status).to eq 200 }
end end
context 'when procedure is hidden' do context 'when procedure is hidden' do
let(:procedure) { create(:procedure, :published, hidden_at: DateTime.now) } let(:procedure) { create(:procedure, :hidden) }
it { expect(subject).to redirect_to(root_path) } it { expect(subject).to redirect_to(root_path) }
end end

View file

@ -100,14 +100,22 @@ FactoryBot.define do
end end
trait :published do trait :published do
after(:create) do |procedure, _evaluator| after(:build) do |procedure, _evaluator|
procedure.publish!(generate(:published_path)) procedure.publish!(generate(:published_path))
end end
end end
trait :archived do trait :archived do
after(:build) do |procedure, _evaluator| after(:build) do |procedure, _evaluator|
procedure.archived_at = Time.now procedure.publish!(generate(:published_path))
procedure.archive!
end
end
trait :hidden do
after(:build) do |procedure, _evaluator|
procedure.publish!(generate(:published_path))
procedure.hide!
end end
end end

View file

@ -1,10 +1,10 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe AutoArchiveProcedureJob, type: :job do RSpec.describe AutoArchiveProcedureJob, type: :job do
let!(:procedure) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: nil) } let!(:procedure) { create(:procedure, :published, auto_archive_on: nil) }
let!(:procedure_hier) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: 1.day.ago) } let!(:procedure_hier) { create(:procedure, :published, auto_archive_on: 1.day.ago) }
let!(:procedure_aujourdhui) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: Date.today) } let!(:procedure_aujourdhui) { create(:procedure, :published, auto_archive_on: Date.today) }
let!(:procedure_demain) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: 1.day.from_now) } let!(:procedure_demain) { create(:procedure, :published, auto_archive_on: 1.day.from_now) }
subject { AutoArchiveProcedureJob.new.perform } subject { AutoArchiveProcedureJob.new.perform }

View file

@ -213,17 +213,17 @@ describe Procedure do
end end
describe 'locked?' do describe 'locked?' do
let(:procedure) { create(:procedure, published_at: published_at) } let(:procedure) { create(:procedure, aasm_state: aasm_state) }
subject { procedure.locked? } subject { procedure.locked? }
context 'when procedure is in draft status' do context 'when procedure is in brouillon status' do
let(:published_at) { nil } let(:aasm_state) { :brouillon }
it { is_expected.to be_falsey } it { is_expected.to be_falsey }
end end
context 'when procedure is in draft status' do context 'when procedure is in publiee status' do
let(:published_at) { Time.now } let(:aasm_state) { :publiee }
it { is_expected.to be_truthy } it { is_expected.to be_truthy }
end end
end end
@ -379,8 +379,8 @@ describe Procedure do
describe "#brouillon?" do describe "#brouillon?" do
let(:procedure_brouillon) { Procedure.new() } let(:procedure_brouillon) { Procedure.new() }
let(:procedure_publiee) { Procedure.new(published_at: Time.now) } let(:procedure_publiee) { Procedure.new(aasm_state: :publiee, published_at: Time.now) }
let(:procedure_archivee) { Procedure.new(published_at: Time.now, archived_at: Time.now) } let(:procedure_archivee) { Procedure.new(aasm_state: :archivee, published_at: Time.now, archived_at: Time.now) }
it { expect(procedure_brouillon.brouillon?).to be_truthy } it { expect(procedure_brouillon.brouillon?).to be_truthy }
it { expect(procedure_publiee.brouillon?).to be_falsey } it { expect(procedure_publiee.brouillon?).to be_falsey }
@ -389,8 +389,8 @@ describe Procedure do
describe "#publiee?" do describe "#publiee?" do
let(:procedure_brouillon) { Procedure.new() } let(:procedure_brouillon) { Procedure.new() }
let(:procedure_publiee) { Procedure.new(published_at: Time.now) } let(:procedure_publiee) { Procedure.new(aasm_state: :publiee, published_at: Time.now) }
let(:procedure_archivee) { Procedure.new(published_at: Time.now, archived_at: Time.now) } let(:procedure_archivee) { Procedure.new(aasm_state: :archivee, published_at: Time.now, archived_at: Time.now) }
it { expect(procedure_brouillon.publiee?).to be_falsey } it { expect(procedure_brouillon.publiee?).to be_falsey }
it { expect(procedure_publiee.publiee?).to be_truthy } it { expect(procedure_publiee.publiee?).to be_truthy }
@ -399,20 +399,18 @@ describe Procedure do
describe "#archivee?" do describe "#archivee?" do
let(:procedure_brouillon) { Procedure.new() } let(:procedure_brouillon) { Procedure.new() }
let(:procedure_publiee) { Procedure.new(published_at: Time.now) } let(:procedure_publiee) { Procedure.new(aasm_state: :publiee, published_at: Time.now) }
let(:procedure_archivee) { Procedure.new(published_at: Time.now, archived_at: Time.now) } let(:procedure_archivee) { Procedure.new(aasm_state: :archivee, published_at: Time.now, archived_at: Time.now) }
let(:procedure_batarde) { Procedure.new(published_at: nil, archived_at: Time.now) }
it { expect(procedure_brouillon.archivee?).to be_falsey } it { expect(procedure_brouillon.archivee?).to be_falsey }
it { expect(procedure_publiee.archivee?).to be_falsey } it { expect(procedure_publiee.archivee?).to be_falsey }
it { expect(procedure_archivee.archivee?).to be_truthy } it { expect(procedure_archivee.archivee?).to be_truthy }
it { expect(procedure_batarde.archivee?).to be_falsey }
end end
describe "#publiee_ou_archivee?" do describe "#publiee_ou_archivee?" do
let(:procedure_brouillon) { Procedure.new() } let(:procedure_brouillon) { Procedure.new() }
let(:procedure_publiee) { Procedure.new(published_at: Time.now) } let(:procedure_publiee) { Procedure.new(aasm_state: :publiee, published_at: Time.now) }
let(:procedure_archivee) { Procedure.new(published_at: Time.now, archived_at: Time.now) } let(:procedure_archivee) { Procedure.new(aasm_state: :archivee, published_at: Time.now, archived_at: Time.now) }
it { expect(procedure_brouillon.publiee_ou_archivee?).to be_falsey } it { expect(procedure_brouillon.publiee_ou_archivee?).to be_falsey }
it { expect(procedure_publiee.publiee_ou_archivee?).to be_truthy } it { expect(procedure_publiee.publiee_ou_archivee?).to be_truthy }
@ -425,7 +423,7 @@ describe Procedure do
let(:now) { Time.now.beginning_of_minute } let(:now) { Time.now.beginning_of_minute }
before do before do
Timecop.freeze(now) Timecop.freeze(now)
procedure.archive procedure.archive!
procedure.reload procedure.reload
end end
after { Timecop.return } after { Timecop.return }

View file

@ -1,8 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe 'admin/_closed_mail_template_attestation_inconsistency_alert.html.haml', type: :view do describe 'admin/_closed_mail_template_attestation_inconsistency_alert.html.haml', type: :view do
let(:procedure) { create(:procedure, closed_mail: closed_mail, published_at: published_at) } let(:procedure) { create(:procedure, closed_mail: closed_mail) }
let(:published_at) { nil }
def alert def alert
assign(:procedure, procedure) assign(:procedure, procedure)
@ -32,7 +31,7 @@ describe 'admin/_closed_mail_template_attestation_inconsistency_alert.html.haml'
it { expect(alert).to include(edit_admin_procedure_mail_template_path(procedure, Mails::ClosedMail::SLUG)) } it { expect(alert).to include(edit_admin_procedure_mail_template_path(procedure, Mails::ClosedMail::SLUG)) }
context 'when the procedure has been published, the attestation cannot be deactivated' do context 'when the procedure has been published, the attestation cannot be deactivated' do
let(:published_at) { Time.now } let(:procedure) { create(:procedure, :published, closed_mail: closed_mail) }
it { expect(procedure.locked?).to be_truthy } it { expect(procedure.locked?).to be_truthy }
it { expect(alert).not_to include(edit_admin_procedure_attestation_template_path(procedure)) } it { expect(alert).not_to include(edit_admin_procedure_attestation_template_path(procedure)) }

View file

@ -61,7 +61,7 @@ describe 'admin/procedures/show.html.haml', type: :view do
describe 'procedure is archived' do describe 'procedure is archived' do
before do before do
procedure.publish!('fake_path') procedure.publish!('fake_path')
procedure.archive procedure.archive!
procedure.reload procedure.reload
render render
end end