Add Follow.unfollowed_at
The active scopes is used indirectly in the dossier<->gestionnaire associations: the existing tests in dossier and gestionnaire just work™.
This commit is contained in:
parent
d417907f36
commit
be4c575622
5 changed files with 29 additions and 6 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
17
db/migrate/20190607124156_add_follow_unfollowed_at.rb
Normal file
17
db/migrate/20190607124156_add_follow_unfollowed_at.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue