Merge pull request #1928 from tchak/procedure-state-scopes

Use aasm_state in scopes
This commit is contained in:
gregoirenovel 2018-05-24 17:53:50 +02:00 committed by GitHub
commit 93b1fd0da9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 22 deletions

View file

@ -34,10 +34,10 @@ class Procedure < ApplicationRecord
mount_uploader :logo, ProcedureLogoUploader
default_scope { where(hidden_at: nil) }
scope :brouillons, -> { where(published_at: nil).where(archived_at: nil) }
scope :publiees, -> { where.not(published_at: nil).where(archived_at: nil) }
scope :archivees, -> { where.not(archived_at: nil) }
scope :publiees_ou_archivees, -> { where.not(published_at: nil) }
scope :brouillons, -> { where(aasm_state: :brouillon) }
scope :publiees, -> { where(aasm_state: :publiee) }
scope :archivees, -> { where(aasm_state: :archivee) }
scope :publiees_ou_archivees, -> { where(aasm_state: [:publiee, :archivee]) }
scope :by_libelle, -> { order(libelle: :asc) }
scope :created_during, -> (range) { where(created_at: range) }
scope :cloned_from_library, -> { where(cloned_from_library: true) }

View file

@ -199,7 +199,7 @@ describe Users::SessionsController, type: :controller do
end
context 'when procedure is not published' do
let(:procedure) { create :procedure, published_at: nil }
let(:procedure) { create :procedure }
before do
session["user_return_to"] = "?procedure_id=#{procedure.id}"
end
@ -209,7 +209,7 @@ describe Users::SessionsController, type: :controller do
end
context 'when procedure_id exist' do
let(:procedure) { create :procedure, published_at: Time.now }
let(:procedure) { create :procedure, :published }
before do
session["user_return_to"] = "?procedure_id=#{procedure.id}"

View file

@ -145,14 +145,14 @@ describe Gestionnaire, type: :model do
after { Timecop.return }
context 'when no procedure published was active last week' do
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published_at: Time.now) }
let!(:procedure) { create(:procedure, :published, gestionnaires: [gestionnaire2], libelle: 'procedure') }
context 'when the gestionnaire has no notifications' do
it { is_expected.to eq(nil) }
end
end
context 'when a procedure published was active' do
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published_at: Time.now) }
let!(:procedure) { create(:procedure, :published, gestionnaires: [gestionnaire2], libelle: 'procedure') }
let(:procedure_overview) { double('procedure_overview', 'had_some_activities?'.to_sym => true) }
before :each do
@ -163,7 +163,7 @@ describe Gestionnaire, type: :model do
end
context 'when a procedure not published was active with no notifications' do
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure', published_at: nil) }
let!(:procedure) { create(:procedure, gestionnaires: [gestionnaire2], libelle: 'procedure') }
let(:procedure_overview) { double('procedure_overview', 'had_some_activities?'.to_sym => true) }
before :each do

View file

@ -229,30 +229,20 @@ describe Procedure do
end
describe 'active' do
let(:procedure) { create(:procedure, published_at: published_at, archived_at: archived_at) }
let(:procedure) { create(:procedure) }
subject { Procedure.active(procedure.id) }
context 'when procedure is in draft status and not archived' do
let(:published_at) { nil }
let(:archived_at) { nil }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
context 'when procedure is published and not archived' do
let(:published_at) { Time.now }
let(:archived_at) { nil }
let(:procedure) { create(:procedure, :published) }
it { is_expected.to be_truthy }
end
context 'when procedure is published and archived' do
let(:published_at) { Time.now }
let(:archived_at) { Time.now }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
context 'when procedure is in draft status and archived' do
let(:published_at) { nil }
let(:archived_at) { Time.now }
let(:procedure) { create(:procedure, :archived) }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
end