models: refactor search visibility methods.

Refactor how model visibility works in index actions:

* Call `visible` in the controller instead of in model `search`
  methods. This decouples model visibility from model searching.

* Explicitly pass CurrentUser when calling `visible`. This reduces
  hidden dependencies on the current user inside models.

* Standardize on calling the method `visible`. In some places it was
  called `permitted` instead.

* Add a `visible` base method to ApplicationModel.
This commit is contained in:
evazion
2020-02-19 16:28:10 -06:00
parent bd6d896ee0
commit 0ad42d23c9
24 changed files with 49 additions and 42 deletions

View File

@@ -21,7 +21,6 @@ class Dmail < ApplicationRecord
scope :deleted, -> { where(is_deleted: true) }
scope :read, -> { where(is_read: true) }
scope :unread, -> { where(is_read: false) }
scope :visible, -> { where(owner: CurrentUser.user) }
scope :sent, -> { where("dmails.owner_id = dmails.from_id") }
scope :received, -> { where("dmails.owner_id = dmails.to_id") }
@@ -85,6 +84,10 @@ class Dmail < ApplicationRecord
end
module SearchMethods
def visible(user)
where(owner: user)
end
def sent_by(user)
where("dmails.from_id = ? AND dmails.owner_id != ?", user.id, user.id)
end