Add hidden_at on procedure

This commit is contained in:
Mathieu Magnin 2017-06-27 14:22:43 +02:00
parent e9491da5c5
commit 760efcdad3
4 changed files with 32 additions and 1 deletions

View file

@ -31,12 +31,17 @@ class Procedure < ActiveRecord::Base
mount_uploader :logo, ProcedureLogoUploader
default_scope { where(hidden_at: nil) }
scope :not_archived, -> { where(archived: false) }
scope :by_libelle, -> { order(libelle: :asc) }
validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :description, presence: true, allow_blank: false, allow_nil: false
def hide!
self.update_attributes(hidden_at: DateTime.now)
end
def path
procedure_path.path unless procedure_path.nil?
end

View file

@ -0,0 +1,6 @@
class AddHiddenAtToProcedures < ActiveRecord::Migration[5.0]
def change
add_column :procedures, :hidden_at, :datetime
add_index :procedures, :hidden_at
end
end

View file

@ -381,7 +381,9 @@ ActiveRecord::Schema.define(version: 20170627143701) do
t.string "lien_notice"
t.boolean "for_individual", default: false
t.boolean "individual_with_siret", default: false
t.datetime "auto_archive_on"
t.date "auto_archive_on"
t.datetime "hidden_at"
t.index ["hidden_at"], name: "index_procedures_on_hidden_at", using: :btree
t.datetime "published_at"
t.datetime "archived_at"
end

View file

@ -302,4 +302,22 @@ describe Procedure do
it { is_expected.to eq('a-long-libelle-with-accents-blabla-coucou-hello-un') }
end
describe ".default_scope" do
let!(:procedure) { create(:procedure, hidden_at: hidden_at) }
context "when hidden_at is nil" do
let(:hidden_at) { nil }
it { expect(Procedure.count).to eq(1) }
it { expect(Procedure.all).to include(procedure) }
end
context "when hidden_at is not nil" do
let(:hidden_at) { 2.days.ago }
it { expect(Procedure.count).to eq(0) }
it { expect { Procedure.find(procedure.id) }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
end