From be4c5756221217c00897aeef270f5a3e6a486af6 Mon Sep 17 00:00:00 2001 From: Nicolas Bouilleaud Date: Fri, 7 Jun 2019 14:59:49 +0200 Subject: [PATCH] Add Follow.unfollowed_at MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The active scopes is used indirectly in the dossier<->gestionnaire associations: the existing tests in dossier and gestionnaire just work™. --- app/models/dossier.rb | 2 +- app/models/follow.rb | 4 +++- app/models/gestionnaire.rb | 7 +++++-- .../20190607124156_add_follow_unfollowed_at.rb | 17 +++++++++++++++++ db/schema.rb | 5 +++-- 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20190607124156_add_follow_unfollowed_at.rb diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 0bcb8669a..c17a9ea99 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -26,7 +26,7 @@ class Dossier < ApplicationRecord has_many :champs_private, -> { root.private_only.ordered }, class_name: 'Champ', dependent: :destroy has_many :commentaires, dependent: :destroy has_many :invites, dependent: :destroy - has_many :follows + has_many :follows, -> { active } has_many :followers_gestionnaires, through: :follows, source: :gestionnaire has_many :avis, dependent: :destroy diff --git a/app/models/follow.rb b/app/models/follow.rb index c94bef80e..076594ab1 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -2,10 +2,12 @@ class Follow < ApplicationRecord belongs_to :gestionnaire belongs_to :dossier - validates :gestionnaire_id, uniqueness: { scope: :dossier_id } + validates :gestionnaire_id, uniqueness: { scope: [:dossier_id, :unfollowed_at] } before_create :set_default_date + scope :active, -> { where(unfollowed_at: nil) } + private def set_default_date diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index c18458dd7..7fa9ba43a 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -16,7 +16,7 @@ class Gestionnaire < ApplicationRecord has_many :procedures_with_email_notifications, through: :assign_to_with_email_notifications, source: :procedure has_many :dossiers, -> { state_not_brouillon }, through: :procedures - has_many :follows + has_many :follows, -> { active } has_many :followed_dossiers, through: :follows, source: :dossier has_many :avis has_many :dossiers_from_avis, through: :avis, source: :dossier @@ -40,7 +40,10 @@ class Gestionnaire < ApplicationRecord end def unfollow(dossier) - followed_dossiers.delete(dossier) + f = follows.find_by(dossier: dossier) + if f.present? + f.update(unfollowed_at: Time.zone.now) + end end def follow?(dossier) diff --git a/db/migrate/20190607124156_add_follow_unfollowed_at.rb b/db/migrate/20190607124156_add_follow_unfollowed_at.rb new file mode 100644 index 000000000..befe5830f --- /dev/null +++ b/db/migrate/20190607124156_add_follow_unfollowed_at.rb @@ -0,0 +1,17 @@ +class AddFollowUnfollowedAt < ActiveRecord::Migration[5.2] + # We need up/down migrations because `remove_index` doesn’t allow `unique: true` and can’t be properly rolled back. + def up + add_column :follows, :unfollowed_at, :datetime + + remove_index :follows, [:gestionnaire_id, :dossier_id] + add_index :follows, [:gestionnaire_id, :dossier_id, :unfollowed_at], unique: true, + name: :uniqueness_index # We need a custom name because the autogenerated name would be too long + end + + def down + remove_column :follows, :unfollowed_at + # We don’t need to remove the index: dropping the column automatically deletes it. + + add_index :follows, [:gestionnaire_id, :dossier_id], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index ab731c070..0a29eae67 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_06_07_122941) do +ActiveRecord::Schema.define(version: 2019_06_07_124156) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -333,8 +333,9 @@ ActiveRecord::Schema.define(version: 2019_06_07_122941) do t.datetime "messagerie_seen_at", null: false t.datetime "created_at" t.datetime "updated_at" + t.datetime "unfollowed_at" t.index ["dossier_id"], name: "index_follows_on_dossier_id" - t.index ["gestionnaire_id", "dossier_id"], name: "index_follows_on_gestionnaire_id_and_dossier_id", unique: true + t.index ["gestionnaire_id", "dossier_id", "unfollowed_at"], name: "uniqueness_index", unique: true t.index ["gestionnaire_id"], name: "index_follows_on_gestionnaire_id" end