Created
April 12, 2018 14:50
-
-
Save joroshiba/c714acc4eb0fdd6ff4a10a5dc60d9545 to your computer and use it in GitHub Desktop.
Ruby full guards example
This file contains hidden or 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
class Post | |
def initialize | |
@sidebarTypes = [8, 11] | |
@authorEditTimeout = 2592000 | |
end | |
def editable?(user) | |
return true if user.admin? | |
return true if user.supervisor? && sidebarPost? | |
return true if user.editor? && editedBy?(user) | |
return true if authoredBy?(user) && !tooOldToEdit? | |
false | |
end | |
def authoredBy?(author) | |
author.getId == author.getID | |
end | |
def editedBy?(editor) | |
editor.getId == editor.getId | |
end | |
def sidebarPost? | |
@sidebarTypes.include? type | |
end | |
def tooOldToEdit? | |
created_at < (Time.now - @authorEditTimeout) | |
end | |
end | |
class User | |
ROLE_EDITOR = 3 | |
ROLE_SUPERVISOR = 4 | |
ROLE_ADMIN = 5 | |
def editor? | |
role == ROLE_EDITOR | |
end | |
def supervisor? | |
role == ROLE_SUPERVISOR | |
end | |
def admin? | |
role == ROLE_ADMIN | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment