Skip to content

Instantly share code, notes, and snippets.

@joroshiba
Created April 12, 2018 14:50
Show Gist options
  • Save joroshiba/c714acc4eb0fdd6ff4a10a5dc60d9545 to your computer and use it in GitHub Desktop.
Save joroshiba/c714acc4eb0fdd6ff4a10a5dc60d9545 to your computer and use it in GitHub Desktop.
Ruby full guards example
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