-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(APIv2): introduce the V2::ApplicationPolicy
- Loading branch information
Showing
5 changed files
with
66 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module V2 | ||
# Generic policies for everything, very restrictive. Any model | ||
# should be overriding only the methods that would make sense | ||
# to override. | ||
class ApplicationPolicy | ||
attr_reader :user, :record | ||
|
||
def initialize(user, record) | ||
@user = user | ||
@record = record | ||
end | ||
|
||
def index? | ||
false | ||
end | ||
|
||
def show? | ||
false | ||
end | ||
|
||
def create? | ||
false | ||
end | ||
|
||
def update? | ||
false | ||
end | ||
|
||
def destroy? | ||
false | ||
end | ||
|
||
alias new? create? | ||
alias edit? update? | ||
|
||
private | ||
|
||
def match_account? | ||
record.account_id == user.account_id | ||
end | ||
|
||
# Generic scope for all models - just matching the account ID. | ||
# To be overridden on individual model policies if needed. | ||
class Scope | ||
attr_reader :user, :scope | ||
|
||
def initialize(user, scope) | ||
@user = user | ||
@scope = scope | ||
end | ||
|
||
def resolve | ||
scope.all | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters