Add a scope to Dossier to check if procedure is not hidden

This commit is contained in:
Mathieu Magnin 2017-06-27 15:26:40 +02:00
parent 760efcdad3
commit be3304f71e
6 changed files with 48 additions and 2 deletions

View file

@ -42,6 +42,7 @@ class Dossier < ActiveRecord::Base
belongs_to :procedure
belongs_to :user
default_scope { where(hidden_at: nil) }
scope :state_brouillon, -> { where(state: BROUILLON) }
scope :state_not_brouillon, -> { where.not(state: BROUILLON) }
scope :state_nouveaux, -> { where(state: NOUVEAUX) }

View file

@ -39,7 +39,9 @@ class Procedure < ActiveRecord::Base
validates :description, presence: true, allow_blank: false, allow_nil: false
def hide!
self.update_attributes(hidden_at: DateTime.now)
now = DateTime.now
self.update_attributes(hidden_at: now)
self.dossiers.update_all(hidden_at: now)
end
def path

View file

@ -0,0 +1,6 @@
class AddHiddenAtToDossiers < ActiveRecord::Migration[5.0]
def change
add_column :dossiers, :hidden_at, :datetime
add_index :dossiers, :hidden_at
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: 20170627143701) do
ActiveRecord::Schema.define(version: 20170627144046) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -175,6 +175,8 @@ ActiveRecord::Schema.define(version: 20170627143701) do
t.datetime "received_at"
t.datetime "processed_at"
t.text "motivation"
t.datetime "hidden_at"
t.index ["hidden_at"], name: "index_dossiers_on_hidden_at", using: :btree
t.index ["procedure_id"], name: "index_dossiers_on_procedure_id", using: :btree
t.index ["user_id"], name: "index_dossiers_on_user_id", using: :btree
end

View file

@ -928,4 +928,21 @@ describe Dossier do
end
end
end
describe ".default_scope" do
let!(:dossier) { create(:dossier, hidden_at: hidden_at) }
context "when dossier is not hidden" do
let(:hidden_at) { nil }
it { expect(Dossier.count).to eq(1) }
it { expect(Dossier.all).to include(dossier) }
end
context "when dossier is hidden" do
let(:hidden_at) { 1.day.ago }
it { expect(Dossier.count).to eq(0) }
end
end
end

View file

@ -320,4 +320,22 @@ describe Procedure do
it { expect { Procedure.find(procedure.id) }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
describe "#hide!" do
let(:procedure) { create(:procedure) }
let!(:dossier) { create(:dossier, procedure: procedure) }
let!(:dossier2) { create(:dossier, procedure: procedure) }
it { expect(Dossier.count).to eq(2) }
it { expect(Dossier.all).to include(dossier, dossier2) }
context "when hidding procedure" do
before do
procedure.hide!
end
it { expect(procedure.dossiers.count).to eq(0) }
it { expect(Dossier.count).to eq(0) }
end
end
end