Skip to content

Instantly share code, notes, and snippets.

@chuckbergeron
Created February 9, 2012 04:04
Show Gist options
  • Save chuckbergeron/1777188 to your computer and use it in GitHub Desktop.
Save chuckbergeron/1777188 to your computer and use it in GitHub Desktop.
CanCan is a gobbledigook.
# VERSION 1:
can [ :create, :read, :download ], Attachment do |attachment|
user.can? :read, attachment.attachable
end
can [ :update, :destroy ], Attachment do |attachment|
user.administrator? or attachment.try( :user_id ) == user.id
end
# VERSION 2:
can do |action, subject_class, subject|
if subject and subject_class == Attachment
if action == :update or action == :destroy
user.administrator? or subject.try( :user_id ) == user.id
end
# AtoD!
if action == :create or action == :read or action == :download
user.can? :read, subject.attachable
end
end
end
# VERSION 3:
can do |action, subject_class, subject|
if subject and subject_class == Attachment
can [ :create, :read, :download ] if user.can? :read, subject.attachable
can [ :update, :destroy ] if user.administrator? or subject.try( :user_id ) == user.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment